1

I'm trying to get a simple winsock program working, so I create my socket and send my data just fine. Then I use shutdown(ConnectSocket, SD_SEND) which according to msdn, disables sending data, but not receiving? The example on msdn does this too.

But after I call shutdown my recv function returns 0 without receiving any data.
If I comment out the shutdown code, everything works as it should.

Am I missing something?

Josh
  • 6,046
  • 11
  • 52
  • 83
  • Do you have control of the other side? Do you know if it's immediately closing the connection once it reads 0 bytes (which it will, once you cut off its incoming data...)? – cHao Mar 23 '12 at 20:43

2 Answers2

3

No, that's normal. The other side responded to your shutdown by shutting down. A zero return from recv indicates a normal connection shutdown. If you don't want the other side to shutdown its half of the connection, don't shut down yours.

(What did you expect the other side to do when its call to recv returned zero?)

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 2
    I was under the impression that if you specified `SD_SEND` it only shutdown sending, and not receiving. And you had to use `SD_RECEIVE` to shutdown receiving. – Josh Mar 23 '12 at 20:44
  • 3
    That is correct. You shutdown your send side, and the other side responded by shutting down its send side. What's on the other side of this connection? What was it supposed to do? (Do you have some agreement with the other side to continue to use the half-open connection?) – David Schwartz Mar 23 '12 at 20:44
  • That depends on how a shutdown is to be interpreted in the given protocol. In HTTP for instance, a client-side send shutdown after sending the request is (although unusual) not to be interpreted as a "never mind what you were about to send, I don't need a response". That said, if you're running a simple echo server, it's likely that the server will respond by also shutting down. – André Caron Mar 23 '12 at 20:44
-3

If it's an HTTP protocol, use Connection header to close connection or keep opened:

Connection: Close

in this case you don't need to shutdown sending

or

Connection: keep-alive

receiving will loop without shutting down the sending part of connection

Tengiz
  • 1,902
  • 14
  • 12