Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 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

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).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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.
--SA
Nish Nishant 11-Feb-11 17:54pm    
Thanks Kryukov.
Sergey Alexandrovich Kryukov 11-Feb-11 13:39pm    
A just referenced you answer in mine, an important point
--SA
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).
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-Feb-11 13:41pm    
Correct, my 5 (pretty mush same as I said).
--SA
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).
Sergey Alexandrovich Kryukov 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.
--SA
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! :)
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.

—SA
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 11-Feb-11 13:40pm    
Who was so clever to vote "1"? I'm just curious...
--SA
Nish Nishant 11-Feb-11 17:55pm    
Compensated with a 5 vote :-)
Sergey Alexandrovich Kryukov 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.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900