简体   繁体   中英

Java UDP Connection

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 ?

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);

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.

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