title: How to use flush() in java networking using UDP
body: <p>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</p>
  
  <p><strong>SERVER CODE IN UDP</strong></p>
  
  <pre><code>    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;
                 }
  
        }
  }
  </code></pre>
  
  <p><strong>CLIENT CODE IN UDP</strong></p>
  
  <pre><code>    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);
        }
  
     }
  }
  </code></pre>
  
  <p><strong>OUTPUT OF SERVER</strong>
  <a href="https://i.stack.imgur.com/5cIXs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5cIXs.png" alt="This is the output of server"></a></p>
  
  <p><strong>OUTPUT OF CLIENT</strong>
  <a href="https://i.stack.imgur.com/IkkxJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IkkxJ.png" alt="This is the output of client"></a></p>