50,530
社区成员
发帖
与我相关
我的任务
分享
public class UDPServer {
public static void main(String[] args) {
byte[] bufferIn=new byte[26];
byte[] bufferOut;
try {
DatagramSocket ds=new DatagramSocket(13);
System.out.println("UDP Server actived on port 13..." );
int num=0;
while(num<100){
num++;
DatagramPacket packetIn=new DatagramPacket(bufferIn,bufferIn.length);
ds.receive(packetIn);
InetAddress ia=packetIn.getAddress();
int port=packetIn.getPort();
String str=new String(bufferIn);
str=str.toUpperCase();
bufferOut=str.getBytes();
System.out.println("["+num+"] "+ia.toString()+":"+port);
DatagramPacket packetOut=new DatagramPacket(bufferOut,bufferOut.length,ia,port);
ds.send(packetOut);
}
ds.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}