[JAVA] TCP communication material

TCP communication material

TCP related memorandum

My Github

https://github.com/KosigayaSuguru/sample-project/tree/master/test-socket

About the operation to the closed socket

https://okwave.jp/qa/q7163796.html

It seems that socket.close () is sending FIN. When you actually execute close (), the TCP status will change when you close (). Me: FIN_WAIT2 Opponent: CLOSE_WAIT become.

Therefore, even if the other party writes (), no error occurs. (Since it is CLOSE_WAIT, the other party has no intention of ending the communication.)

If the other party is blocking read and forcibly terminates it by a method other than close () (that is, other than sending FIN), the read side will issue a corresponding exception.

If the other party writes () after executing close (), communication ends there in terms of TCP. From the state of your FIN_WAIT2 and the other party's CLOSE_WAIT, only your own LISTENING is changed. If the other party writes again, an exception will be thrown on the other side.

See also the summary of my github.

Socket Since it is not possible to detect that the communication partner has been disconnected, a monitoring thread that periodically sends a monitoring message is required.

Stream Note that if you use Writer, the handling unit will be character units, and you will not be able to perform strict operations using bytes.

I didn't realize it was blocked by InputStream.read () and stumbled

http://redfreshet.com/2015/11/01/java_inputstream_read/

Notes on Java implementation

http://majimem.hatenablog.com/entry/2014/06/11/165732 http://axe1lyze.blogspot.jp/2011/08/java.html?m=1 It may be good to get the InputStream before the OutputStream (or vice versa). (Read () may work ...)

Notes on Java implementation 2

After close (), an exception may be thrown in the read block part depending on the communication status with the other party. It is necessary to have a flag to manage that close () is called, and if the close flag is set, a trick such as suppressing the output of an exception is required. ..

Recommended Posts

TCP communication material
TCP communication (socket communication) in Java (ASCII, Binary)