WSA error 10048 when binding server sockets

后端 未结 1 1477
温柔的废话
温柔的废话 2021-01-22 05:14

I started with the simple server tutorial on the msdn website in order to learn how to use sockets in client and server applications.

Once I was done following thet tuto

1条回答
  •  萌比男神i
    2021-01-22 05:47

    WSA 10048 is the 'Address in use' error (https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx).

    I don't have windows so I can't run your code, but this error commonly occurs when a server keeps a server port reserved for a number of minutes before it can be reused.

    A socket option is provided to allow faster reuse, setsockopt (SO_REUSEADDR).

    In your case, you would add the following lines right after creating and checking ListenSocket:

    int optval = 1;
    iResult = ::setsockopt(ListenSocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
    
    if (iResult == SOCKET_ERROR) {
            std::cout << 9 << std::endl;
            printf("setsockopt failed with error: %d\n", WSAGetLastError());
    }
    

    The TCP stack has good reasons for not re-issuing old port numbers immediatly after they are released, but for server ports this is unwanted behaviour. Only, the stack does not know the difference between a server and a non-server port, so we must use setsockopt.

    0 讨论(0)
提交回复
热议问题