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 ?
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).
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).
Edited 11-Feb-11 8:03am
What do you mean "at the same time", exactly? At least the same socket can be used for reading and writing.—SA
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.
Edited 11-Feb-11 17:46pm
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)