Shubham Singh Shubham Singh -4 years ago 65
Java Question

Java UDP client not receiving from server(simple client server udp code)

I am a newbie in java networking , i made a client-server udp program. The server side is working fine but the client side is not receiving anything from server . Please correct the mistake.The server side is fully running but the client side is not working till its end . Any help would be great.

public class Client {
public static void main (String args[]) throws Exception {
DatagramSocket s = new DatagramSocket();
String msg = "Hello! ,from client. ";
byte[] b = msg.getBytes();
InetAddress ia = InetAddress.getLocalHost();
DatagramPacket dp = new DatagramPacket(b, b.length, ia, 9999);
s.send(dp);

byte[] b2 = new byte[1024]; //byte array
DatagramPacket dip = new DatagramPacket(b2, b2.length);
s.receive(dip);
String str = new String(dip.getData());
System.out.println("From server" + str);
}
}

public class Server {
void run() throws Exception {
DatagramSocket ds = new DatagramSocket(9999);
byte [] b = new byte[1024];

DatagramPacket dp = new DatagramPacket(b, b.length);
ds.receive(dp);
String msg = new String(dp.getData());
System.out.println("Message from client:" + msg);

//giving back to client
String str = "HI!, from server.";
byte[] b2 = str.getBytes();
InetAddress ia = InetAddress.getLocalHost();
DatagramPacket dop = new DatagramPacket(b2, b2.length, ia, 9999);
ds.send(dop);
System.out.println("Message sent back");
ds.close();
}

public static void main(String args[]) throws Exception {
Server server = new Server();
server.run();
}
}

Answer Source

The error is your server and client run on the same host, so they must listen on different ports like below:

The server

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Server {
    void run() throws Exception {
        DatagramSocket ds = new DatagramSocket(9999);
        byte [] b = new byte[1024];

        DatagramPacket dp = new DatagramPacket(b, b.length);
        ds.receive(dp);
        String msg = new String(dp.getData(),0,dp.getLength(),"UTF-8");
        System.out.println("Message from client:" + msg);

        //giving back to client
        String str = "HI!, from server.";
        byte[] b2 = str.getBytes();
        InetAddress ia = InetAddress.getLocalHost();

        DatagramPacket dop = new DatagramPacket(b2, b2.length, ia, 9998);
        ds.send(dop);
        System.out.println("Message sent back");
        ds.close();
    }

    public static void main(String args[]) throws Exception {
        Server server = new Server();
        server.run();
    }
}

and the client

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Client {
    public static void main (String args[]) throws Exception {
        DatagramSocket s = new DatagramSocket(9998);
        String msg = "Hello! ,from client. ";
        byte[] b = msg.getBytes();
        InetAddress ia = InetAddress.getLocalHost();
        DatagramPacket dp = new DatagramPacket(b, b.length, ia, 9999);
        s.send(dp);

        byte[] b2 = new byte[1024]; //byte array
        DatagramPacket dip = new DatagramPacket(b2, b2.length);
        s.receive(dip);
        String str = new String(dip.getData(),0,dip.getLength(),"UTF-8");
        System.out.println("From server" + str);
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download