Java UDP Connection

I am using the Netbeans IDE trying to make a UDP connection between a client and a server, it is a simple program that UDPClient sends a String to a UDPServer , and the server uses and sends it back to the client. I made client and server sides in separated projects.

my class code for 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 the counter that the program is working properly and does not produce errors. the server does not receive the packet at all, it does not interact with the client. why did this happen?

+4
source share
2

, .

DatagramSocket socket = new DatagramSocket(9876);
+3

, - , 9876. , :

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

( Linux-), netstat :

netstat -ln | grep 9876

, 9876. netcat, TCP UDP:

nc -u localhost 9876

UDP .

, .

+2

Source: https://habr.com/ru/post/1584610/


All Articles