Click here to Skip to main content
13,503,505 members
Rate this:
Please Sign up or sign in to vote.
See more:
can any one point out the reason why u cant send and receive on a socket at the same time ?

to my understanding there are 2 streams one to push and one to pull

if you attempt to send/receive simultaneously you will get wasealready error

what is the reason that the socket throws wasealready error (10035)
does it have any thing to do with the ack window the receiving side sends back ?
as if to keep the line open for the window ?
Posted 11-Feb-11 4:44am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can send and receive on the same socket at the same time (via multiple threads). But the send and receive may not actually occur simultaneously, since one operation may block the other from starting until it's done. That is how it's in Winsock. The .NET wrappers/implementation may or may not permit this - it depends on which class you are using (you will need to look at the implementation using some tool like Reflector to see if this is permitted).
SAKryukov 11-Feb-11 13:36pm
It's correct (a 5), but since we agree that it is not literally "at the same time", but the same socket can sometimes read, sometimes write, the answer is more general, not only for different thread. However, the possibility to work with the sockets in different threads is very important.
Nishant Sivakumar 11-Feb-11 17:54pm
Thanks Kryukov.
SAKryukov 11-Feb-11 13:39pm
A just referenced you answer in mine, an important point
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

In fact, you can send and receive at the same time. What you can't have is two threads sending over the same TCP/IP socket or two threads receiving at the same TCP/IP socket.

I use this a lot in my remoting framework. See:
I am sending a file to the client (so I keep calling write).
The client, for some reason, wants to cancel the file, but don't want to disconnect (my application does many things). So, how can it inform such situation to the server?

One of the ways is:
Write some bytes. Read for a response. Write more bytes. But, in this case, I need to read between writes.

Another approach is:
I do a while(canSend) and write bytes.
From another thread, I read the same socket. If I receive a "cancellation message", I set canSend to false.

Note that I am only simplifying the example. But if you need any asynchronous message, you may need to send and receive at the same time.

**** I searched for WSAEALREADY: In my understanding, it only happens in non-blocking sockets, so I think it will be better to know exactly how you are using it. To be honest, I always used blocking sockets and created my own threads to access them (so a thread can be blocked until it receives a response).
SAKryukov 11-Feb-11 13:41pm
Correct, my 5 (pretty mush same as I said).
Paulo Zemek 11-Feb-11 13:46pm
I was confusing the posters... but we don't say the same.
You say it is not possible to overlap. They can. One thread can be sending, and another thread can be reading on the same socket. What can't happen is two threads reading or writing at the same time (in fact, in .Net, I can't even write from many threads, even if I use a lock, so I send everything to a single thread, that serializes writes).
SAKryukov 11-Feb-11 23:44pm
Thank you, Paulo.
I suspected you note something like that but was not sure.
Still, with "at the same time" anyone have to be more accurate (you know how in Relativity timing order depends on the reference system (I'm not 100% joking...)). After all, in physical network level the packages are sequential. So the question how much they overlap is reduced to the question how much they interlaced, if you understand what I mean. I'm still in doubt... Do you have some reference? And I'll try to reduce the strength of my statement, because you made me unsure...

Thank you.
Anto Pinjatić 4-Nov-17 16:16pm
I have found it useful. You can just switch every 1/10 of the second between receiving and sending, with bool canSend.
Thank you for posting this! :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

What do you mean "at the same time", exactly? At least the same socket can be used for reading and writing.

But it's quite typical when a socket is used to interlace read and right. It's very usual protocol when a socket reads a message, then sends a confirmation or something like that.

Also, Nishant's note about different thread is very important. Sockets are thread-safe, but not just so. Do you know that historically sockets were designed primarily as the inter-process communication primitive to be used on the same machine? So they are a real IPC tool.

SAKryukov 11-Feb-11 13:40pm
Who was so clever to vote "1"? I'm just curious...
Nishant Sivakumar 11-Feb-11 17:55pm
Compensated with a 5 vote :-)
SAKryukov 11-Feb-11 23:47pm
I see, thanks a lot.
However, Paulo makes me to be not so sure about "at the same time", see my comments to his Answer.
So I modified my statement a bit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180417.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100