简体   繁体   English

Java套接字TCP拆卸

[英]Java socket TCP teardown

After I have created a socket and established a connection I use BufferedReader and PrintWriter to write/read from the socket. 创建套接字并建立连接后,我使用BufferedReader和PrintWriter从套接字进行写入/读取。

Socket s = new Socket(ip, port);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream());

When I'm done using the socket I close it. 使用完套接字后,我将其关闭。

s.close();

This will cause a TCP fin to be sent and leave a half-open TCP connection. 这将导致发送TCP鳍并留下半开的TCP连接。 Is there any way to detect if a client/server closed it's end of the connection? 有什么方法可以检测客户端/服务器是否关闭了连接的末端?

This will cause a TCP fin to be sent and leave a half-open TCP connection. 这将导致发送TCP鳍并留下半开的TCP连接。

Not quite. 不完全的。 It will leave a half-open TCP connection that will issue resets if it receives any more data. 它将留下一个半开的TCP连接,如果接收到更多数据,它将发出重置消息。

Is there any way to detect if a client/server closed its end of the connection? 有什么方法可以检测客户端/服务器是否关闭了连接的末端?

Not if you've already closed yours. 如果您已经关闭自己的门,则不会。 If you had just issued a write shutdown, you could read until EOS, which would tell you about the peer close, then close the socket. 如果您刚刚发出写关闭命令,则可以读取直到EOS为止,这将告诉您有关对等端关闭的信息,然后关闭套接字。 Not much point really unless you really have to achieve simultaneous closes. 除非您确实必须实现同时关闭,否则实际上并没有多大意义。

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

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