5

I'm using Netbeans IDE trying to make a UDP connection between client and server, it's a simple program that UDPClient send a String to UDPServer and the server capitalize the string and sends it back to the client.I made the client side and server side in a separated projects.

my class code for the client UDPClient :

    package udpclient;


    import java.io.*;
    import java.net.*;

    public class UDPClient {

        public static void main(String[] args) throws IOException{

            //get input from user
            BufferedReader user_in = new BufferedReader(
                    new InputStreamReader(System.in));

            //create udp socket connection
            DatagramSocket socket = new DatagramSocket();

            //creat buffers to process data
            byte[] inData = new byte[1024];
            byte[] outData = new byte[1024];

            //get ip destination wanted
            InetAddress IP = InetAddress.getByName("localhost");

            //read data from user
            System.out.println("Enter Data to send to server: ");
            outData = user_in.readLine().getBytes();


            /*
             * make pkts for interaction
             */
            //send pkts
            DatagramPacket sendPkt = new DatagramPacket(outData, outData.length, IP, 9876);
            socket.send(sendPkt);

            //receive pkts
            DatagramPacket recievePkt = new DatagramPacket(inData, inData.length);
            socket.receive(recievePkt);

            System.out.println("Replay from Server: "+recievePkt.getData());

        }
    }

and my server side class UDPServer:

    package udpserver;

    import java.io.*;
    import java.net.*;

    public class UDPServer {


        public static void main(String[] args) throws IOException{
            // TODO code application logic 
            //connection
            DatagramSocket socket = new DatagramSocket();

            //pkt buffers
            byte[] inServer = new byte[1024];
            byte[] outServer = new byte[1024];

            //receive pkt
            DatagramPacket rcvPkt = new DatagramPacket(inServer,inServer.length);
            socket.receive(rcvPkt);
            //display receive
            System.out.println("Packet Received!");


            //retrive pkt info to send response to same sender
            InetAddress IP = rcvPkt.getAddress();
            int port = rcvPkt.getPort();

            //process data
            String temp = new String(rcvPkt.getData());
            temp = temp.toUpperCase();
            outServer = temp.getBytes();

            //send response packet to sender
            DatagramPacket sndPkt = new DatagramPacket(outServer, outServer.length, IP, port);
            socket.send(sndPkt);

        }
    }

make in count that the program runs normally and outputs no error. the server doesn't receive the packet at all , it didn't interact with the client. why does that happened ?

3
  • then you probably have bad IP an/or port in your client.
    – libik
    Commented Apr 25, 2015 at 8:31
  • I've tried to change localhost to a certain ip address same problem, but i got ur idea. probably my PC machine network ip setting needs to be configured. thanQ
    – Poula Adel
    Commented Apr 25, 2015 at 8:39
  • 3
    There is no such thing as a UDP connection. It is a connectionless protocol.
    – user207421
    Commented Apr 25, 2015 at 9:52

2 Answers 2

4

You haven't specified any listening port in your server so the server listen on a random available port.

Try with this on server side

DatagramSocket socket = new DatagramSocket(9876);
2

The problem is that your server code doesn't specify a port - it will listen to a random available port, whereas the client is sending to 9876. To correct this, use:

DatagramSocket socket = new DatagramSocket(9876, InetAddress.getByName("localhost"));

(If you're on a linux system) I'd highly recommend using netstat to debug this kind of code:

netstat -ln | grep 9876

will tell you if a process is listening to port 9876. Another useful tool is netcat, which can be used to send and receive TCP and UDP:

nc -u localhost 9876

allows you to send messages over UDP to the server.

In your client code, you also need to turn the received bytes back into a string to get meaningful output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.