Click here to Skip to main content
11,925,101 members (55,456 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java Sockets
when i use this code , the sender successfully send the message byt the receiver can't receive this message
package UDP_Socket;
public class PacketReceiver {
    public static void main(String[] args) {
        try {
// Create a datagram socket, bound to the specific port 9999
            DatagramSocket socket = new DatagramSocket(9999);
// Create a datagram packet, containing a maximum buffer of 256 bytes
            DatagramPacket packet = new DatagramPacket(new byte[256], 256);
// Receive a packet - remember by default this is a blocking operation
            System.out.println("Packet received!");
// Display packet information
            InetAddress remote_addr = packet.getAddress();
            System.out.println("Sent by host: " + remote_addr.getHostAddress());
            System.out.println("Sent from port: " + packet.getPort());
// Display packet contents, by reading from byte array
            ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData());
// Display only up to the length of the original UDP packet
            for (int i = 0; i < packet.getLength(); i++) {
                int data =;
                if (data == -1)
        } catch (IOException ioe) {
            System.err.println("Error - " + ioe);

package UDP_Socket;
public class PacketSender {
    public static void main(String[] args) {
        try {
// Hostname/IP address of the remote server  
            String hostname = "localhost";
// Lookup the specified hostname, and get an InetAddress
            System.out.println("Looking up hostname " + hostname);
            InetAddress remote_addr = InetAddress.getByName(hostname);
            System.out.println("Hostname resolved as " + remote_addr.getHostAddress());
// Create a datagram socket, bound to any available local port
            DatagramSocket socket = new DatagramSocket();
// Create a message to send using a UDP packet
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            PrintStream pout = new PrintStream(bout);
// Get the contents of our message as an array of bytes
            byte[] barray = bout.toByteArray();
// Create a datagram packet, containing our byte array
            DatagramPacket packet = new DatagramPacket(barray, barray.length);
// Set the destination address of the packet 
// Set port number to 9999
// Send the packet - remember no guarantee of delivery
            System.out.println("Packet sent!");
        } catch (UnknownHostException uhe) {
            System.err.println("Can't find host " + uhe.getMessage());
        } catch (IOException ioe) {
            System.err.println("Error - " + ioe);
Posted 29-Nov-11 15:37pm
Edited 29-Nov-11 15:42pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

First of all you should try another socket. the sockets 0-10000 are often used by different applications, it's much saver to use a higher value here (and please not again 33333 or 22222 and... you know which...)

Also can I recommend to read This Java Tutorial on Datagrams[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151125.3 | Last Updated 30 Nov 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100