码迷,mamicode.com
首页 > 编程语言 > 详细

Java UDP

时间:2017-11-27 15:13:07      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:ram   new   throw   time   pre   public   recv   receive   catch   

public static String faceDetect(String itemStr) throws IOException {
    	String sRt;
        DatagramSocket client = new DatagramSocket();
        byte[] sendBuf = itemStr.getBytes("GB2312");
        InetAddress addr = InetAddress.getByName("127.0.0.1");
        int port = 5500;
        DatagramPacket sendPacket = new DatagramPacket(sendBuf ,sendBuf.length , addr , port);
        client.send(sendPacket);
        byte[] recvBuf = new byte[100];
        String recvStr = null;
        DatagramPacket recvPacket= new DatagramPacket(recvBuf , recvBuf.length);
        while(true){
        	client.setSoTimeout(60000);
        	try{
        		client.receive(recvPacket);
	    		recvStr = new String(recvPacket.getData() , 0 ,recvPacket.getLength(),"gb2312").trim();
	            if (recvStr!=null && recvStr.length()>0) {
	            	sRt = recvStr;
	            	break;
	 		}
        	}catch(SocketTimeoutException e){
        		LOGGER.error("UDP线路故障,请确认UDP客户端是否开启");
        		sRt="9999";
			break;
        	}
        }
        client.close();
        return sRt;
    }

  

Java UDP

标签:ram   new   throw   time   pre   public   recv   receive   catch   

原文地址:http://www.cnblogs.com/rvs-2016/p/7903817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!