简体   繁体   中英

Can I send data via TCP before closing Socket?

I'll explain my question better, and I preventively say sorry for my bad English. I'm practising with java.net package (it is argument of my next university exam) and I'm trying to achieve a better control of the communication between a Client and a Server device. More precisely, I tried to send, using a TCP connection, 3 different strings, from server to client, in 3 different times, before calling the close() method to the related Socket. I found out that my script doesn't work as I supposed to. On client side, I receive all of the 3 strings, but only when I close the Socket. I would instead like to receive all of the 3 messages immediately after putting them into the output stream of the server. I'll post the code of the server script.

Before criticizing me, I read this question before writing a new one, but it didn't solve my problem The below script is the body of the run() method of the Thread that manages my connection on server side.

try{
        //Dormi per un pò..

        System.out.println("Il Client con ip "+ind+" ed associato all' Handler #"+id+" è in attesa");
        Thread.sleep(5000);
        PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
        pw.println("Hi "+ind); //ind is the InetAddress of the connected client
        pw.flush();
        Thread.sleep(5000);
        pw.write("what's up?\n");
        pw.flush();
        Thread.sleep(5000);
        pw.write("I gotta go. Bye\n");
        pw.flush();
        pw.close();
        s.close();
        System.out.println("Il Client associato all'Handler #"+id+" si è disconnesso");
    }catch(Exception e){
        e.printStackTrace();
    }

Here is the Client code:

package socket;


import java.net.*;
import java.io.*;
public class MultiThreadClient extends Thread{

   private byte[] address={(byte) 192, (byte)168, (byte) 1, (byte)15};
   private int destPort=2000;

   public MultiThreadClient(){
       super();
   }

   public void run(){
       try{
           InetAddress ip=InetAddress.getByAddress(address);
           Socket s=new Socket(ip,destPort);
           s.setReuseAddress(true);
           BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); 
           String message1=br.readLine();
           String message2=br.readLine();
           String message3=br.readLine();
           System.out.println(message1);
           System.out.println(message2);
           System.out.println(message3);

           s.close();
       }catch(Exception e){
           e.printStackTrace();
       }
  }


  public static void main(String[]args){
  //for(int i=0;i<3;i++){
      MultiThreadClient mtc=new MultiThreadClient();
      mtc.start();
  // }
  }

}

Your code is working just fine - you are just blocking 3 times on the readLine method and then printing your responses (at this time socket closes on server side).

try this:

        String message1 = br.readLine();
        System.out.println(message1);
        String message2 = br.readLine();
        System.out.println(message2);
        String message3 = br.readLine();
        System.out.println(message3);

Another tip from me is to do the clean up in finally block.

try { ... }
catch { ... }
finally {
    s.close();
}

This way resources are cleaned up even if exception is thrown.

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