8

I remember having read somewhere that a socket can be regarded as two independent half-duplex channels. Does it mean that recv() and send() of the same socket are actually irrelevant?

  • if so, is it by definition or implementation-specific?
  • if not, how the two interfere with each other?

thanks.

t.g.
  • 1,719
  • 2
  • 14
  • 25

2 Answers2

2

In case of SOCK_STREAM, you can use send/recv concurrently.

Ex : Suppose you have two threads, one is responsible for sending the data and second one is responsible for receiving the data you can do following

main Routine : get a socket fd. create a POSIX thread for sending the buffer to this fd. create a POSIX thread to receive the data arrived from this fd. connect to a server.

Thread 1 Routine : construct a message buffer; send the buffer into this fd.

Thread 2 Routine : recv data from this fd. process the date.

userNishant
  • 132
  • 9
2

I'm curious how you think they would interfere with each other. Are you thinking that you might receive what you sent?

  • 1
    No, I means how one can influence the correctness or efficiency of behavior of the other, if they do. For example, if they share the same buffer, it's very likely one must wait until the other is idle, right? – t.g. Jul 22 '10 at 11:40
  • 2
    You can issue blocking or non-blocking sends and recieves. With blocking, the data buffer you pass in IS the data buffer. No chance of confusion. With non-blocking, your data buffer is copied. There are no restrictions in the API documentation regarding interleaved use, so they must be using different buffers; for if they were not, they could not be non-blocking (they would have to block on each other). –  Jul 29 '10 at 08:59