javanetwork-programmingudpclient-serversocketpair

How to run Client program on fixed port in UDP connectionless client - server pair in java


I am trying to understand UDP connectionless client - server pair. I got some code in the Book Computer Networking: A Top Down Approach. The Programs are as follows:- UDPServer.java:

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

    class UDPServer
   {
       public static void main(String args[]) throws Exception
       {
        DatagramSocket serverSocket = new DatagramSocket(9876);
        byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];
        while(true)
        {
          DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);                   
          serverSocket.receive(receivePacket);
          String sentence = new String( receivePacket.getData());
          System.out.println("RECEIVED: " + sentence);
          InetAddress IPAddress = receivePacket.getAddress();
          int port = receivePacket.getPort();
          String capitalizedSentence = sentence.toUpperCase();
          sendData = capitalizedSentence.getBytes();
          DatagramPacket sendPacket =
          new DatagramPacket(sendData, sendData.length, IPAddress, port);                   
          serverSocket.send(sendPacket);
          }
      }
    }

UDPClient.java

import java.io.*;
import java.net.*;
class UDPClient
{
    public static void main(String args[]) throws Exception
    {
        BufferedReader inFromUser =
        new BufferedReader(new InputStreamReader(System.in));
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");
        byte[] sendData = new byte[1024];
        byte[] receiveData = new byte[1024];
        String sentence = inFromUser.readLine();
        sendData = sentence.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);      
        clientSocket.send(sendPacket);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);       
        clientSocket.receive(receivePacket);
        String modifiedSentence = new String(receivePacket.getData());
        System.out.println("FROM SERVER:" + modifiedSentence);          
        clientSocket.close();
    }
}

In the given code we have fixed the port no for the Server ,i.e., 9876. I am curious to know that how to fix the port for Client, as we did for Server in the given java program, so that message can be returned to Client on the Specific Port.

For example, if the client will send a UDP message to the server, the server will start and run on port number 9876 and return the original message to the client on port 9877. Please help.


Solution

  • You don't need a fixed port at the client, any more than the client needs a fixed IP address. Your own code should already work correctly. However there are other issues:

    String sentence = new String( receivePacket.getData());
    

    Wrong. It should be String sentence = new String( receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());.

          System.out.println("RECEIVED: " + sentence);
          InetAddress IPAddress = receivePacket.getAddress();
          int port = receivePacket.getPort();
          String capitalizedSentence = sentence.toUpperCase();
          sendData = capitalizedSentence.getBytes();
          DatagramPacket sendPacket =
          new DatagramPacket(sendData, sendData.length, IPAddress, port);                   
          serverSocket.send(sendPacket);
    

    This code will send the reply back to from whence the request came. An easier way is to use the same DatagramPacket for both receive and send, and just change the data.