Question

I have a Windows server app which does

  • WSAStartup(), obviously
  • listen() on TCP port X
  • accept() an incoming connection
  • ioctlsocket() the socket into nonblocking mode
  • try to reply with WSASendMsg

I receive WSAEINVAL error code, which supposedly means: 'The socket has not been bound with bind, or the socket was not created with the overlapped flag.'

In this case it makes no sense to call bind(), since the socket is already connected with local and remote addresses. And the socket was definitely not created with the overlapped flag either.

So what is the real reason I get the error that escaped that line of documentation?

Was it helpful?

Solution

AFAICWO: Because I'm using accept() to listening for a TCP socket connection request, what accept returns me is a connection-oriented socket. So I have to use WSASend() instead of WSASendMsg(). MSDN docs do have this tidbit which eventually tipped me off to where I was going wrong:

The WSASendMsg function can only be used with datagrams and raw sockets.

In other words, WSASendMsg() is ONLY intended for use in connectionless scenarios like UDP, ICMP, or any other raw sockets where you are authoring your own packets.

(Probably, I could also have used WSASendTo() instead:)

On a connection-oriented socket, the lpTo and iToLen parameters are ignored; in this case, the WSASendTo is equivalent to WSASend.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top