C socket - consecutive read/write, what happens? -


In a server / client, in C, in TCP, what if I continually read in my program? Is it possible, or do I always want to follow the "Customer Writing-> Server Read-> Server Writing-> Client Read" structure?

Is it possible to do something like this? Or is it possible that the third writing data in the server is obtained from 2 reads in the client, and such other bad things?

  write (1) read (2) read (3) (4)   

server.c < / P>

  read (1) write (2) write (3) write (4)    

Yes, it is possible to read (by default) the block until the data will not be available to read on the socket. There is nothing to read before reading () call return, by then your program reads in a particular order and there is no need to arrange writing.

The result of your code will be:

  reads the server block () in the Client (Read) client blocks () receives the server data, reads () Returns Write the server (three times) the customer receives the data, read (1) returns the customer's reading (2) and as soon as the data comes, the reader (3) arrives at the same time as the data return Returns   

Really Linking It Is Possible Writing (S) and Client reading (s) may be at almost the same time, it may be that the Interlave also happens, but writing the server (s) will always be in order and the client's reading () will always be in order.

If the sockets are SOCK_STREAM then the data order is protected, e.g. TCP or Unix socket like you are asking.

Then read (2) will always return the data written in writing for a TCP socket.

If you can get the UDP socket (the default of SOCK_DGRAM) you can get the messages out-of-order or not at all.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -