UDP发字节,接收字节
生活随笔
收集整理的這篇文章主要介紹了
UDP发字节,接收字节
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class MenuBarDemo {public static void main(String[] args) throws Exception{byte[] bytes = "你好".getBytes(StandardCharsets.UTF_8);DatagramPacket dp = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("127.0.0.1"),10000);DatagramSocket ds = new DatagramSocket();ds.send(dp);}
}
public static void main(String[] args)throws Exception {byte[] bytes1 = new byte[1024];//創建數據包對象,接收數組DatagramPacket dap = new DatagramPacket(bytes1,bytes1.length);DatagramSocket das = new DatagramSocket(10000);das.receive(dap);//拆解數據包int lengh = dap.getLength();int port = dap.getPort();InetAddress i = dap.getAddress();String ip = i.getHostAddress();System.out.println(new String(bytes1,0,lengh)+"..."+port+ip);}
總結
以上是生活随笔為你收集整理的UDP发字节,接收字节的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GoEasy小程序即时通讯源码 v1.1
- 下一篇: 第5章 Python 数字图像处理(DI