CSocket 10038 WSAENOTSOCK Error

CSocket 10038 WSAENOTSOCK Error

Post by Y2xlb2h » Wed, 07 Mar 2007 00:00:48


I am currently implementing a server socket that listen to a particular port
on the local machine as well as another client socket that connect to the
same port on the same machine. Everything goes fine until I attempt to send
data using the client socket. When I call GetLastError(), I got the error
code 10038 - WSAENOTSOCK.

I suspect that the error is due to me trying to listen and connect on the
same port and address. I tried to use SetSockOpt as follow:

serverSocket->SetSockOpt(SO_REUSEADDR, &nOption, 1);

It is still not successful. Does anyone have any idea how I can resolve
this problem?



CSocket 10038 WSAENOTSOCK Error

Post by Scott McPh » Wed, 07 Mar 2007 00:21:03

There are two port numbers associated with a client socket. One of them
is the client's own and the other is the server to call. When calling a
server on the same machine these two port numbers must be different
because the IP:port pair is used to uniquely identify each socket.

Typical practice is to let the system select the client's own port (pass
NULL to CSocket::Create, and pass the server port to CSocket::Connect).

CSocket is part of the MFC library, so if you have follow up questions
try the MFC newsgroup for best results.

Scott McPhillips [VC++ MVP]


CSocket 10038 WSAENOTSOCK Error

Post by Ulrich Eck » Wed, 07 Mar 2007 00:21:20

You shouldn't bind the port of a TCP client socket but rather let the TCP
stack assign one. On the other hand, your description is a bit ambiguous,
so my guess what exactly is happening here might be wrong....


CSocket 10038 WSAENOTSOCK Error

Post by Y2xlb2h » Wed, 07 Mar 2007 01:17:30

Thanks for your reply. I am not sure if I have get it wrong. Cause what I
have done is as follows:

CSocket* serverSocket = new CSocket();

printf("Server socket created\n");

printf("Server socket start to listen\n");

CSocket* sock = new CSocket();

if(sock->Connect"", 111))
printf("Client socket connected");
string msg = "test";

if(sock->Send(msg.c_str(), strlen(msg.c_str())))

Is it wrong to do that? Really appreciate your help.

CSocket 10038 WSAENOTSOCK Error

Post by Michael K. » Wed, 07 Mar 2007 02:12:24


On the client side, you forgot to call sock.Create(NULL), as mentioned by
Scott. This must be done before the call to Connect.

CSocket 10038 WSAENOTSOCK Error

Post by Y2xlb2h » Wed, 07 Mar 2007 02:58:12

Thanks for your reply. I have tried and it works!