CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2003
    Posts
    13

    Blocking sockets

    I was wondering how blocking sockets (TCP) work when you do a send and receive at the same time. Specifically I was wondering about the 2 cases below:

    1) There are two threads. The first thread is pending on a recv() call on the socket and no messages are coming in. The second thread needs to send a message on the same socket. Can the second thread send a message while the first thread is pending on the receive? Is this all transparent to the application layer?

    2) If 1) is true, is it possible for a send to get pre-empted in the middle if a message is received, while the send is taking place? If so, does this cause any errors in send or recv?

    In general, is there any possiblities of socket or data collisions when trying access the same socket in multiple threads?
    Thanks

  2. #2
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354

    Re: Blocking sockets

    One thing you have to understand is a TCP connection is bidirectional. That means data can be send and received at the same time. How ever this won't happen exactly that way in a single medium. Data going in opposite directions can't be passed on a wire. In other words you can issue a call without worring that the other has actually completed.

    Winsock is thread safe. But when using a socket on multiple threads, you have to take the same precaution as you would take on a shared global resource. Said that, calling a recv() on one thread and send() on a different one on a same socket is considered to be safe. However you have to take care while issueing 2 recv() or two send() on 2 threads.

    You may also wan't to note that when the call to blocking send returns, the actual send may not have taken place.
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured