is it safe to recv() and send() on one socket concurrently?
Asked Answered
B

2

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.

Brier answered 22/7, 2010 at 11:24 Comment(2)
depends on the protocol. TCP is full-duplex, so yes.Bartolommeo
Thanks, do you have any counter-example? or any other reference link/code so that I can read?Brier
S
2

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

Smothers answered 22/7, 2010 at 11:33 Comment(2)
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?Brier
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).Smothers
I
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.

Individual answered 27/3, 2015 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.