在c中与Windows套接字应用程序绑定错误10038
我正在用 c 语言测试套接字服务器应用程序,并且在绑定函数上遇到错误,代码为 10038。我查了一下,MSDN 说这意味着:
尝试对非套接字的操作。套接字句柄参数未引用有效的套接字,或者对于 select,fd_set 的成员无效。
这是代码:
// 我有正确的包含文件,例如 include ,但是当我输入 #include Winsock2.h 时,stackoverflow 显示它很奇怪
int main()
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
int Port = 5150;
if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0)
{
printf("Server: WSAStartup failed with error %ld\n",WSAGetLastError());
return -1;
}
else
{
printf("Server: The Winsock dll found!\n");
printf("Server: The current status is: %s.\n",wsaData.szSystemStatus);
}
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
printf("Server: The dll do not support Winsock version
%u.%u!\n",LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
WSACleanup();
return -1;
}
else
{
printf("Server: The dll supports the Winsock version %u.%u!\n",LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
printf("Server: The highest version this dll can support: %u.%u\n",LOBYTE(wsaData.wHighVersion),HIBYTE(wsaData.wHighVersion));
}
ListeningSocket == socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(ListeningSocket == INVALID_SOCKET)
{
printf("Server: Error at socket(), error code: %ld\n",WSAGetLastError());
WSACleanup();
return -1;
}
else
printf("Server: socket() is OK!\n");
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(ListeningSocket, (SOCKADDR *)&ServerAddr,sizeof(ServerAddr)) == SOCKET_ERROR)
{
printf("Server: bind() failed! Error code: %ld.\n",WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return -1;
}
else
printf("Server: bind() is OK!\n");
if(listen(ListeningSocket,5) == SOCKET_ERROR)
{
printf("Server: listen(): Error listening on socket %ld.\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return -1;
}
else
printf("Server: listen() is OK, I'm waiting for connections...\n");
printf("Server: accept() is ready...\n");
while(1)
{
NewConnection = SOCKET_ERROR;
while(NewConnection == SOCKET_ERROR)
{
NewConnection = accept(ListeningSocket, NULL, NULL);
}
printf("Server: accept() is OK...\n");
printf("Server: Client connected, ready for receiving and sending data...\n");
ListeningSocket = NewConnection;
break;
}
if(closesocket(NewConnection) != 0)
printf("Server: Cannot close \"NewConnection\" socket. Error code: %ld\n",
WSAGetLastError());
else
printf("Server: Closing \"NewConnection\" socket...\n");
if(WSACleanup() != 0)
printf("Server: WSACleanup() failed! Error code: %ld\n", WSAGetLastError());
else
printf("Server: WSACleanup() is OK...\n");
return 0;
}
I am testing out a Socket Server application in c and I am getting an error on the bind function with code 10038. I looked this up and MSDN says it means:
An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.
Here is the code:
// I have the correct include files such as include , but stackoverflow displays it weird when i put #include winsock2.h
int main()
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
int Port = 5150;
if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0)
{
printf("Server: WSAStartup failed with error %ld\n",WSAGetLastError());
return -1;
}
else
{
printf("Server: The Winsock dll found!\n");
printf("Server: The current status is: %s.\n",wsaData.szSystemStatus);
}
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
printf("Server: The dll do not support Winsock version
%u.%u!\n",LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
WSACleanup();
return -1;
}
else
{
printf("Server: The dll supports the Winsock version %u.%u!\n",LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
printf("Server: The highest version this dll can support: %u.%u\n",LOBYTE(wsaData.wHighVersion),HIBYTE(wsaData.wHighVersion));
}
ListeningSocket == socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(ListeningSocket == INVALID_SOCKET)
{
printf("Server: Error at socket(), error code: %ld\n",WSAGetLastError());
WSACleanup();
return -1;
}
else
printf("Server: socket() is OK!\n");
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(ListeningSocket, (SOCKADDR *)&ServerAddr,sizeof(ServerAddr)) == SOCKET_ERROR)
{
printf("Server: bind() failed! Error code: %ld.\n",WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return -1;
}
else
printf("Server: bind() is OK!\n");
if(listen(ListeningSocket,5) == SOCKET_ERROR)
{
printf("Server: listen(): Error listening on socket %ld.\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return -1;
}
else
printf("Server: listen() is OK, I'm waiting for connections...\n");
printf("Server: accept() is ready...\n");
while(1)
{
NewConnection = SOCKET_ERROR;
while(NewConnection == SOCKET_ERROR)
{
NewConnection = accept(ListeningSocket, NULL, NULL);
}
printf("Server: accept() is OK...\n");
printf("Server: Client connected, ready for receiving and sending data...\n");
ListeningSocket = NewConnection;
break;
}
if(closesocket(NewConnection) != 0)
printf("Server: Cannot close \"NewConnection\" socket. Error code: %ld\n",
WSAGetLastError());
else
printf("Server: Closing \"NewConnection\" socket...\n");
if(WSACleanup() != 0)
printf("Server: WSACleanup() failed! Error code: %ld\n", WSAGetLastError());
else
printf("Server: WSACleanup() is OK...\n");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚明白了,我用 == 代替
=
I just got it, I put == for
instea of =