java UDP服务端

public class ISSIhttpserverThread extends Thread {
    private int port = 23024;
    DatagramSocket server = null;
    DatagramPacket packets = null;

    private int requestLen = 0;
    private ArrayList<String> headList = new ArrayList<String>();
    private String bodyStr;
    int position = 0;

    public ISSIhttpserverThread() {
    
    }

    @Override
    public void run() {
        //receive();
        
            try {
                 this.port =AgentGlobals.getXMLProperty("pocudpport", 23024);
                    //this.port = 23034;
                
                server = new DatagramSocket(port);
                Log.info("UDP登录port:" + port);
            } catch (Exception e) {
                // TODO: handle exception
            }
        while (true) {
            try {
                // �μ�һ�����ݱ���
                byte[] b = new byte[1024];

                packets = new DatagramPacket(b, b.length);
                server.receive(packets);
                UDPServerThread udpserverthread=new UDPServerThread(packets);
                udpserverthread.start();
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("e.getMessage():" + e.getMessage());
                Log.info("udp fail");
                try {
                    sleep(5000);
                    new ISSIhttpserverThread().start();
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            }

    }

    
}
UDPServerThread:
public class UDPServerThread extends Thread {
	DatagramPacket datagrampackets = null;


	// int superport=1024;
	public UDPServerThread(DatagramPacket packetd) {
		this.datagrampackets = packetd;
		Log.info("UDPServerThread");
		Log.info("datagrampacket" + datagrampackets.getPort());

	}

	@Override
	public void run() {
		Log.info("ten");
		
		int ten = datagrampackets.getLength();
		Log.info("ten" + ten);
		if (ten > 0) {

			String msg = new String(datagrampackets.getData(), 0,datagrampackets.getLength());
			InetAddress ip = datagrampackets.getAddress();
			int ports = datagrampackets.getPort();
			Log.info("客户端" + ip + ports + "发来消息:" + msg);
			Document document = null;
			try {
				document = DocumentHelper.parseText(msg);
			} catch (DocumentException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}// 转换document对象
			Element root = document.getRootElement();
			
			//返回客户端消息
			DatagramSocket sockets=null;
			try {
				sockets = new DatagramSocket();
				byte[] buf = null;
				buf = respxml.getBytes();
				DatagramPacket packets = new DatagramPacket(buf, buf.length,
						new InetSocketAddress(ip, ports));
				sockets.send(packets);
				sockets.close();
			} catch (SocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
	

}

  

 
posted @ 2020-01-17 10:01  哎呀呀呀呀~~  阅读(1177)  评论(0编辑  收藏  举报