javanetwork-programming

How to use flush() in java networking using UDP


I'm trying to make a two way server - client communication in java using basic UDP but the problem is that if I enter a string it gets overflowed and so I think that it is good to use flush() to flush the buffer but I don't know how to use this function

SERVER CODE IN UDP

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

class servee
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9876);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
            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 servo = br.readLine();
          if(new String(servo).equals("bye")==true)
            break;
                  sendData = servo.getBytes();
                  DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);
          sendData=null;
               }

      }
}

CLIENT CODE IN UDP

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

class clientee
{
   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];
      while(true)
      {
      String sentence = inFromUser.readLine();
      if(new String(sentence).equals("bye")==true)
    break;
      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);
      }

   }
}

OUTPUT OF SERVER This is the output of server

OUTPUT OF CLIENT This is the output of client


Solution

  • Your code is assuming that DatagramPacket.getBytes() only returns the sent or received data - however it returns the entire array that you used when constructing the DatagramPacket. This becomes an issue when you reuse the same sendData array for many receives, as old data will remain in the array when the new data you receive is shorter than the previous data received.

    Your client code must account for the length of the received data:

    String modifiedSentence = new String(receivePacket.getData(),0, receivePacket.getLength());
    

    Your server code must do the same:

    String sentence = new String( receivePacket.getData(),0, receivePacket.getLength());