java发送UDP请求
1、发送数据报
(1)创建DatagramSocket并连接
DatagramSocket client = new DatagramSocket(); InetAddress addr = InetAddress.getByName(reqHost); int port = Integer.valueOf(reqPort); client.connect(addr, port);
(2)构造数据报
(3)发送数据
client.send(sendPacket);
(4)关闭连接
2、接受数据
(1)创建指定监听端口的socket服务
DatagramSocket server = new DatagramSocket(10001);
(2)构建接受数据报容器
byte[] buf = new byte[1024];//接受内容的大小,注意不要溢出DatagramPacket dp = new DatagramPacket(buf,0,buf.length);//定义一个接收的包 </pre>
(3)接受
server.receive(recvPacket);(4)关闭资源
3、示例代码
public class UdpSentUtil { private static DatagramSocket client; private static String reqHost = DOGlobals.getValue("cws.host"); private static String reqPort = DOGlobals.getValue("cws.port"); private static RecMsg recThead; private static UdpSentUtil self = new UdpSentUtil(); public static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10); private UdpSentUtil(){ try { client = new DatagramSocket(); InetAddress addr = InetAddress.getByName(reqHost); int port = Integer.valueOf(reqPort); client.connect(addr, port);recThead = new RecMsg(client); recThead.start(); System.out.println("=========="+recThead.getName()); //reciveMsg(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); client = null; } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); client = null; } }
public static UdpSentUtil getInstance(){
if(self != null && client != null && client.isConnected()){
return self;
}else{
self = new UdpSentUtil();
return self;
}
}
public void sendNoRecive(byte[] sendBuf){
DatagramPacket sendPacket = new DatagramPacket(sendBuf ,sendBuf.length);
if(client != null){
try {
if(!client.isConnected()){
InetAddress addr = InetAddress.getByName(reqHost);
int port = Integer.valueOf(reqPort);
client.connect(addr, port);
}
client.send(sendPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class RecMsg extends Thread{
private DatagramSocket client;
private String sign;public RecMsg(DatagramSocket client){ this.client = client; this.sign = ""; } public RecMsg(DatagramSocket client,String sign){ this.client = client; this.sign = sign; } public void run() { // TODO Auto-generated method stub byte[] recvBuf = new byte[1024*4]; DatagramPacket recvPacket = new DatagramPacket(recvBuf , recvBuf.length); String recvStr = ""; try { while(client != null){ System.out.println(this.sign+"等待接受。。。。。。"); client.receive(recvPacket); recvStr = new String(recvPacket.getData() , 0 ,recvPacket.getLength(),"UTF-8"); System.out.println(this.sign+"收到信息:"+recvStr); fixedThreadPool.execute(new ParseMsg(recvStr)); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 解析信息 * @author user * */ class ParseMsg extends Thread{ private String msg; public ParseMsg(String msg){ this.msg = msg; } public void run() { //解析xml数据 } private void dealMsg(Document doc,String cid){ //处理相应数据 } private Map respToMap(List<Element> lrs){ Map<String,String> m = new HashMap<String, String>(); if(lrs != null){ for (int i = 0; i < lrs.size(); i++) { m.put(lrs.get(i).getName(), lrs.get(i).getText()); } } return m; } public List respToList(List<Element> lrs){ List<Map> lms = new ArrayList<Map>(); if(lrs != null){ for (int i = 0; i < lrs.size(); i++) { lms.add(respToMap((List<Element>) lrs.get(i).selectNodes("./*"))); } } return lms; } }
}
标题:java发送UDP请求
作者:hugh0524
地址:https://blog.uproject.cn/articles/2016/04/29/1461896357315.html
0 0