在c中与Windows套接字应用程序绑定错误10038

发布于 2024-08-17 05:13:54 字数 2959 浏览 5 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-08-24 05:13:54

我刚刚明白了,我用 == 代替

ListeningSocket == socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

=

ListeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

I just got it, I put == for

ListeningSocket == socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

instea of =

ListeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文