写了一个程序,使用DatagramScoket和多线程进行基本的通信。要求服务器端能接受多个客户端的消息。部分代码如下。蓝色的一行代码会不停的创建线程,但是不在main中加死循环的话,又不能接收多个客户端的消息。不知道怎么改才能达到预期的目的。public class NotifyServerThread extends Thread { protected DatagramSocket client = null; public NotifyServerThread(DatagramSocket c) {
this.client = c;
} public void run() {
try {
while (true) {
byte[] buf = new byte[100];
DatagramPacket dpRecv = new DatagramPacket(buf, buf.length);
client.receive(dpRecv); MsgManager.getInstance().doRecvPacket(dpRecv); String str = "Welcome!";
DatagramPacket dpSend = new DatagramPacket(str.getBytes(), str
.length(), dpRecv.getAddress(), dpRecv.getPort());
client.send(dpSend);
} } catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
client.close();
}
} public static void main(String[] args) throws Exception {
DatagramSocket client = new DatagramSocket(GlobalSetting.PORT);
while (true) {
NotifyServerThread t = new NotifyServerThread(client);
t.start();
}
}
}

解决方案 »

  1.   

    不知道为什么颜色没有加上,有颜色的那一行是倒数第五行,也就是
    NotifyServerThread t = new NotifyServerThread(client);
      

  2.   

    有颜色那一行是NotifyServerThread t = new NotifyServerThread(client);
    不知道为什么没显示颜色。
      

  3.   

    不清楚你想问什么?但是怎么server的程序和client的程序混在一起了?把C/S搞清楚再说吧。以下server的程序,启动:java NotifyServer 8888import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.SocketException;public class NotifyServer implements Runnable {
        final static int MAX_UDP_BUFFER_SIZE = 8192;
        int port;    static public void main(String[] strs) {        new NotifyServer(Integer.parseInt(strs[0])).start();
        }    public NotifyServer(int port) {
            this.port = port;
        }    public void start() {
            new Thread(this).start();    }    public void run() {
            try {
                byte buffer[] = new byte[MAX_UDP_BUFFER_SIZE];
                DatagramSocket socket = new DatagramSocket(port);
                DatagramPacket packet = new DatagramPacket(buffer,
                        MAX_UDP_BUFFER_SIZE);
                while (true) {                socket.receive(packet);
                    System.out.println(new String(packet.getData(), 0, packet
                            .getLength()));
                }        } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }    }}
    一下client的程序,启动:java NotifyClient 127.0.0.1 8888
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;public class NotifyClient {    static public void main(String[] strs) {
            try {
                DatagramSocket socket = new DatagramSocket();
                final String STR = "TestTest\n";
                DatagramPacket packet = new DatagramPacket(STR.getBytes(), STR
                        .length(), InetAddress.getByName(strs[0]), Integer
                        .parseInt(strs[1]));
                socket.send(packet);
                socket.close();
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }    }}server的程序在【socket.receive(packet);】处是阻塞的,要想不阻塞,用setSoTimeout函数设一个不等于0的时间,这个不用教了吧?