Naresh
Naresh

Reputation: 31

Java - sending message from one PC to another via UDP connection

When I tested with both client and server on localhost its works. But then I split the client and server to different machines with different IP addresses and now the packets are not being received on the client side. Can anyone spot the problem with my code:

Client:

class Csimudp {
    public static DatagramSocket ds;
    public static byte buffer[] = new byte[1024];

    public static void Myclient() throws Exception {
        while (true) {
            DatagramPacket p = new DatagramPacket(buffer, buffer.length);
            ds.receive(p);
            System.out.println(new String(p.getData(), 0, p.getLength()));
        }
    }

    public static void main(String args[]) throws Exception {
        System.out.println("for quitting client press ctrl+c");
        ds = new DatagramSocket(777);
        Myclient();
    }
}

Server:

class Ssimudp {
    public static DatagramSocket ds;
    public static byte buffer[] = new byte[1024];

    public static void MyServer() throws Exception {
        int pos = 0;
        while (true) {
            int c = System.in.read();
            switch (c) {
            case '~':
                System.out.println("\n Quits");
                return;
            case '\r':
                break;
            case '\n':
                ds.send(new DatagramPacket(buffer, pos, InetAddress
                        .getByName("117.201.5.150"), 777));
                pos = 0;
                break;
            default:
                buffer[pos++] = (byte) c;
            }
        }
    }

    public static void main(String args[]) throws Exception {
        System.out.println("server ready....\n please type here");
        ds = new DatagramSocket(888);
        MyServer();
    }
}

Upvotes: 3

Views: 5568

Answers (1)

thkala
thkala

Reputation: 86413

I would hazard a guess that your packets are being blocked by a firewall somewhere in their way. I'd start by opening the appropriate outgoing and incoming UDP ports in the firewalls of the client and the server respectively.

Or your server might be sitting behind a NAT gateway and you need to set up port forwarding rules for it to receive any packets. For example, most ADSL routers are actually set up as a NAT gateway.

Another potential issue is your port selection:

  1. You are binding your client to a specific local port. There is no need for that - let the OS select a free port on its own. This would also remove the possibility of trying to use a port that is already in use.

  2. You are using ports in the [0-1023] range. This port range is generally reserved for well-known services - as a matter of fact, on most Unix-like systems (e.g. Linux) you cannot bind a listening port in that range without root privileges. As a result, quite a few ISPs will filter that port range in their firewall, supposedly to protect their users.

Without more information on the networks that connect the client to the server, it's quite hard to provide a more concrete answer.

PS: There is no need to recreate the InetAddress object in every iteration of your loop - do it once beforehand...

PS.2: In general the computer that sends the first packet in a UDP session is considered the client, since it's also the one that can exist without a fixed address. Your assignment of the client/server roles is reversed in that respect. So when reading my points above, you will have to reverse the client/server specifications for them to apply to your code...

Upvotes: 3

Related Questions