Java UDP
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; }
大千世界,就等着你的发掘!