62,623
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}