写了一个程序,使用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();
}
}
}
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();
}
}
}
NotifyServerThread t = new NotifyServerThread(client);
不知道为什么没显示颜色。
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的时间,这个不用教了吧?