package com.snowcattle.game.common.udp.server;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* Created by jiangwenping on 17/1/22.
*/
public class JavaUDPServer {
public static void main(String[] args)throws Exception{
String str_send = "Hello UDPclient";
byte[] buf = new byte[1024];
//服务端在3000端口监听接收到的数据
DatagramSocket ds = null;
try {
ds = new DatagramSocket(9999);
} catch (SocketException e) {
e.printStackTrace();
}
//接收从客户端发送过来的数据
DatagramPacket dp_receive = new DatagramPacket(buf, 1024);
System.out.println("server is on,waiting for client to send data......");
boolean f = true;
while(f){
//服务器端接收来自客户端的数据
ds.receive(dp_receive);
System.out.println("server received data from client:");
String str_receive = new String(dp_receive.getData(),0,dp_receive.getLength()) +
" from " + dp_receive.getAddress().getHostAddress() + ":" + dp_receive.getPort();
System.out.println(str_receive);
//数据发动到客户端的3000端口
DatagramPacket dp_send= new DatagramPacket(str_send.getBytes(),str_send.length(),dp_receive.getAddress(),9000);
ds.send(dp_send);
//由于dp_receive在接收了数据之后,其内部消息长度值会变为实际接收的消息的字节数,
//所以这里要将dp_receive的内部消息长度重新置为1024
dp_receive.setLength(1024);
}
ds.close();
}
}