简体   繁体   中英

udp sending packet failling

I'm try to send a message through a udp connection but it fails at

 socket.send(packet);  

here is how I set up the server and send the packet

                InetAddress iadd=InetAddress.getByName(SERVERIP); 
                DatagramSocket socket=new DatagramSocket(); 
                byte[] buff="AAHHHH it worked".getBytes(); 
                DatagramPacket packet=new  DatagramPacket(buff, buff.length); 
                socket.send(packet);          

Based on the code you've pasted, you don't appear to ever be associating your InetAddress with your DatagramSocket.

Try: DatagramSocket socket = new DatagramSocket(portNumber, iadd); to specify the address and port of where the datagrams should be going.

Your client should be listening on the same port on the other end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM