• 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();
    			}
    
    		}
    	}
    	
    
    }
    

      

     
  • 相关阅读:
    【特别推荐】12款最佳的网站速度和性能测试工具
    Skeljs – 用于构建响应式网站的前端开发框架
    EpicEditor – 可嵌入的 JavaScript Markdown 编辑器
    12个学习 CSS3 网站布局设计的优秀案例
    15套流行的扁平化设计的界面素材【免费下载】
    JavaScriptOO.com – 快速找到你需要的 JS 框架
    15款最好的 jQuery Modal(模态窗口)插件
    开眼了!20个极具创意的移动界面动画效果
    ShareDrop – 苹果 AirDrop 服务的 HTML5 实现
    Furatto – 轻量,友好的响应式前端开发框架
  • 原文地址:https://www.cnblogs.com/ttqi/p/12204433.html
Copyright © 2020-2023  润新知