问题描述
- JAVA udp协议 服务器端无法接受数据
-
我用UDP协议,写了客服端和服务器端的demo,可是服务器端一直无法receive DatagramPacket。
我在防火墙和360等杀毒软件都关的情况下,任然无法解决。
服务器代码:```public static void main(String[] args) throws IOException {
// TODO Auto-generated method stubDatagramSocket ds = new DatagramSocket(10086); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, 1024); ds.receive(dp); byte[] data = dp.getData(); int length =dp.getLength(); String receiveddata= new String(data, 0, length); System.out.println("UdpServerDemo.main()"+receiveddata); ds.close(); }
客户端代码:
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stubDatagramSocket dS = new DatagramSocket() ; String string = "你好"; byte[] buf =string.getBytes(); int length = buf.length; InetAddress address = InetAddress.getLocalHost(); int port= 10086; DatagramPacket p = new DatagramPacket(buf, length, address, port); dS.send(p); dS.close(); } }
解决方案
http://blog.csdn.net/awj3584/article/details/24714515
解决方案二:
服务端有没有收到消息,是否正确侦听端口了。用抓包工具看看客户端发送的数据包
时间: 2024-09-19 02:54:08