简体   繁体   English

关闭套接字之前可以通过TCP发送数据吗?

[英]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. 我正在使用java.net软件包进行练习(这是我的下一次大学考试的论据),并且我试图更好地控制客户端和服务器设备之间的通信。 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. 更准确地说,在尝试对相关的Socket调用close()方法之前,我尝试使用TCP连接在3个不同的时间从服务器到客户端发送3个不同的字符串。 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. 在客户端,我会收到所有3个字符串,但是只有在关闭Socket时才可以。 I would instead like to receive all of the 3 messages immediately after putting them into the output stream of the server. 相反,我希望在将它们放入服务器的输出流后立即接收所有3条消息。 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. 在批评我之前,我在写一个新问题之前先阅读了这个问题,但是并没有解决我的问题。下面的脚本是在服务器端管理我的连接的Thread的run()方法的主体。

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). 您的代码运行良好-您仅在readLine方法上阻塞了3次,然后打印了响应(此时套接字在服务器端关闭)。

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. 我的另一个建议是在“ finally块中进行清理。

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

This way resources are cleaned up even if exception is thrown. 这样,即使抛出异常也可以清理资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM