How to synchronize read and write to sockets?

Create a socket. On the one hand, we have a “server” on the socket, and on the other hand, there is a “client”. Both the server and the client can write and read from the socket. This is what I understand. I do not understand the following things:

  • If the server reads from the socket, does it see only the material written by the client on the socket in the socket? I mean, if the server writes something to the socket and reads from the socket with what, will it (the server) see in the socket what it wrote there (the server) there? I hope no.

  • Consider the following situation. The client writes something to the socket, and then writes something new to the socket, and then the server reads from the socket. What will the server see? Only “new” things written by the client, or both “new” and “old”?

  • If the client (or server) writes to the socket, can it see if the written information was received by the other party? For example, out.println("Hello, Server!") return true , the server received this message.

+4
source share
3 answers

A socket connection is two unidirectional flows: one from client to server and one from server to client.

Each side can put data in one stream and read from the other.

Data placed in one stream is added to the end of existing data.

When data is read from one end of the stream, the oldest available data will be read (i.e. the stream is FIFO).

These statements should allow you to answer all your questions, but I will also make this explicit:

  • The server will read only what the client wrote, and not what he wrote himself
  • First, the server will read the old material, and then the new material
  • The client does not have an immediate way to find out if the data was actually received by the other party if the server did not send anything in response. In some circumstances, he may know that the dispatch failed (for example, when there is a physical problem on the client side), but, as a rule, cannot rely on this notification.
+4
source
  • You're right. The server will only see what the client has written to the socket

  • The server should see both old and new (old and then new)

  • I think it depends on the type of socket. TCP will be reliable (your data will go to the other end), while UDP does not care (you just want the data to be fast and the losses are acceptable, i.e. Streaming video). But you will not know if a message is received if you do not send the response to the socket.

0
source

Sockets provide two unidirectional “channels” for communication with the other end: one for reading data (InputStream) and the other for sending data (OutputStream). Therefore, there is no danger when reading in the input stream immediately after writing something to the output stream.

Secondly, writing new data to the output stream does not overwrite or replace the "old" content. The content is buffered until it is read by another application.

FInally, data reception is not acknowledged by the other party, but if you do not receive an IOException after sending the data, you can be sure that it was received by another application.

0
source

Source: https://habr.com/ru/post/1304780/


All Articles