Düzenle

Aracılığıyla paylaş


Creating a Socket for the Server

After initialization, a SOCKET object must be instantiated for use by the server.

To create a socket for the server

  1. The getaddrinfo function is used to determine the values in the sockaddr structure:

    • AF_INET is used to specify the IPv4 address family.
    • SOCK_STREAM is used to specify a stream socket.
    • IPPROTO_TCP is used to specify the TCP protocol .
    • AI_PASSIVE flag indicates the caller intends to use the returned socket address structure in a call to the bind function. When the AI_PASSIVE flag is set and nodename parameter to the getaddrinfo function is a NULL pointer, the IP address portion of the socket address structure is set to INADDR_ANY for IPv4 addresses or IN6ADDR_ANY_INIT for IPv6 addresses.
    • 27015 is the port number associated with the server that the client will connect to.

    The addrinfo structure is used by the getaddrinfo function.

    #define DEFAULT_PORT "27015"
    
    struct addrinfo *result = NULL, *ptr = NULL, hints;
    
    ZeroMemory(&hints, sizeof (hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;
    
    // Resolve the local address and port to be used by the server
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }
    
  2. Create a SOCKET object called ListenSocket for the server to listen for client connections.

    SOCKET ListenSocket = INVALID_SOCKET;
    
  3. Call the socket function and return its value to the ListenSocket variable. For this server application, use the first IP address returned by the call to getaddrinfo that matched the address family, socket type, and protocol specified in the hints parameter. In this example, a TCP stream socket for IPv4 was requested with an address family of IPv4, a socket type of SOCK_STREAM and a protocol of IPPROTO_TCP. So an IPv4 address is requested for the ListenSocket.

    If the server application wants to listen on IPv6, then the address family needs to be set to AF_INET6 in the hints parameter. If a server wants to listen on both IPv6 and IPv4, two listen sockets must be created, one for IPv6 and one for IPv4. These two sockets must be handled separately by the application.

    Windows Vista and later offer the ability to create a single IPv6 socket that is put in dual stack mode to listen on both IPv6 and IPv4. For more information on this feature, see Dual-Stack Sockets.

    // Create a SOCKET for the server to listen for client connections
    
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    
  4. Check for errors to ensure that the socket is a valid socket.

    if (ListenSocket == INVALID_SOCKET) {
        printf("Error at socket(): %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }
    

Next Step: Binding a Socket

Getting Started With Winsock

Initializing Winsock

Winsock Server Application