1

I'm writing a client-server program, where the client is C++/winapi and the server is C#/.net.

the client have a loop where it reads from server (and may block the calling thread [denote t1] , which is fine with me). it also have another thread [denote t2] , that wait on an Event object with a timeout.

if the timeout is reached (and the Event is yet to be singled) the t2 thread, will write (exacly on byte) on the same socket.

The problem I have, is that it seems like the write won't return untill the read on t1 returns (in some legitimate scnerions it will never happen) , as if the socket was not full-duplex.

P.S : socket is an AF_INET/ SOCK_STREAM and I'm using Readfile and WriteFile for socket IO.

thanks.

art Landaq
  • 23
  • 3

2 Answers2

0

Neither sockets not read() and write(), or send() and recv(), behave that way. You must have some synchronization of your own.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

I have been programming with WinSock for over a decade, and I can assure you that sockets are always full duplex.

The only way WriteFile() (or send() or WSASend()) would block the calling thread for any amount of time is if the socket is running in blocking mode and its outbound queue of data waiting to be transmitted has been completely full (the size of the queue is controlled by the SO_SNDBUF socket option). That indicates that the other party (your C# server) is not reading inbound data from its socket endpoint and acknowledging the received data in a timely manner so your socket endpoint can remove that data from its outbound queue so new data can be accepted for transmission.

If you don't want your call to WriteFile() to block, you can either:

  1. enable the SO_SNDTIMEO socket option to specify a timeout for blocking writes.

  2. use select(), WSAAsyncSelect(), or WSAAsyncEvent() to detect when the socket is actually writable (ie, when it can accept data without blocking) before writing anything new to the socket.

  3. switch to non-blocking I/O, asynchronous overlapped I/O, or I/O completion ports.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • What about this? http://stackoverflow.com/a/16708230/886887 Are sockets a special exception? Does that exception apply if you're using ReadFile/WriteFile rather than the socket functions? – Harry Johnston May 29 '13 at 06:13
  • `socket()` creates a socket that uses overlapped I/O internally. To avoid using overlapped I/O, you would have to call `WSASocket()` instead and omit the `WSA_FLAG_OVERLAPPED` flag. But that is generally not advisable in most situations. – Remy Lebeau May 29 '13 at 14:11
  • Doesn't that mean that if you're using ReadFile and WriteFile you need to use overlapped I/O, i.e., specify a valid OVERLAPPED structure? Could that be the OPs problem? – Harry Johnston Jun 03 '13 at 21:40
  • The real problem is that the OP is using `ReadFile()` and `WriteFile()` with sockets at all. He should be using `recv()`/`WSARecv()` and `send()`/`WSASend()` instead. – Remy Lebeau Jun 03 '13 at 22:56