欢迎来到山村网

Java基于socket服务实现UDP协议的方法

2019-03-02 13:29:49浏览:155 来源:山村网   
核心摘要:  本文实例讲述了Java基于socket服务实现UDP协议的方法。分享给大家供大家参考。具体如下:  示例1:  接收类:  ? 1234

  本文实例讲述了Java基于socket服务实现UDP协议的方法。分享给大家供大家参考。具体如下:

  示例1:

  接收类:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceiveDemo { public static void main(String[] args) throws IOException{ System.out.println("接收端启动…………"); //udpsocket服务,使用DatagramSocket对象 DatagramSocket ds=new DatagramSocket(10002); //使用DatagramPacket将数据封装到该对象中 byte[] buf=new byte[1024]; DatagramPacket dp=new DatagramPacket(buf, buf.length); //通过udp的socket服务将数据包发送出去,通过send方法 ds.receive(dp); //通过数据包的方法解析数据包中的数据,比如,地址、端口、数据内容等 String ip=dp.getAddress().getHostAddress(); //String name=dp.getAddress().getHostName(); int port=dp.getPort(); String text=new String(dp.getData(),0,dp.getLength()); //System.out.println("-----"+ip+"-----"+name+"-----"+port+"-----"+text); System.out.println("-----"+ip+"----------"+port+"-----"+text); //关闭资源 ds.close(); } }

  发送类:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; public class UDPSendDemo { public static void main(String[] args) throws IOException{ System.out.println("发送端启动…………"); //udpsocket服务,使用DatagramSocket对象 DatagramSocket ds=new DatagramSocket(8888);//监听端口 //将要发送的数据封装到数据包中 String str="udp传输演示,go"; //使用DatagramPacket将数据封装到该对象中 byte[] buf=str.getBytes(); DatagramPacket dp= new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.100"),10002); //通过udp的socket服务将数据包发送出去,通过send方法 ds.send(dp); //关闭资源 ds.close(); } }

  示例2:

  接收类:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceiveDemo2 { public static void main(String[] args) throws IOException{ System.out.println("接收端启动…………"); //udpsocket服务,使用DatagramSocket对象 DatagramSocket ds=new DatagramSocket(10003); while(true){ //使用DatagramPacket将数据封装到该对象中 byte[] buf=new byte[1024]; DatagramPacket dp=new DatagramPacket(buf, buf.length); //通过udp的socket服务将数据包发送出去,通过send方法 ds.receive(dp);//阻塞式的。 //通过数据包的方法解析数据包中的数据,比如,地址、端口、数据内容等 String ip=dp.getAddress().getHostAddress(); //String name=dp.getAddress().getHostName(); int port=dp.getPort(); String text=new String(dp.getData(),0,dp.getLength()); //System.out.println("-----"+ip+"-----"+name+"-----"+port+"-----"+text); System.out.println("-----"+ip+"----------"+port+"-----"+text); } //关闭资源 //ds.close(); } }

  发送类:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package com.socket.demo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSendDemo2 { public static void main(String[] args) throws IOException{ System.out.println("发送端启动…………"); //udpsocket服务,使用DatagramSocket对象 DatagramSocket ds=new DatagramSocket(9999);//监听端口 //将要发送的数据封装到数据包中 //String str="udp传输演示,go"; BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));//键盘输入 String line=null; //使用DatagramPacket将数据封装到该对象中 while((line=bufr.readLine())!=null){ byte[] buf=line.getBytes();// DatagramPacket dp= new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.100"),10003); //通过udp的socket服务将数据包发送出去,通过send方法 ds.send(dp); if("886".equals(line)){ break; } } //关闭资源 ds.close(); } }

  运行效果图如下:

  接收:

  发送:

  希望本文所述对大家的java程序设计有所帮助。

(责任编辑:豆豆)
下一篇:

JAVA中IP和整数相互转化的方法

上一篇:

Java枚举类用法实例

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com