src/netbase.cpp
SOCKET CreateSocket(const CService &addrConnect)
{
    // Create a sockaddr from the specified service.
    struct sockaddr_storage sockaddr;
    socklen_t len = sizeof(sockaddr);
    if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
        LogPrintf("Cannot create socket for %s: unsupported network\n", addrConnect.ToString());
        return INVALID_SOCKET;
    }

    // Create a TCP socket in the address family of the specified service.
    SOCKET hSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
    if (hSocket == INVALID_SOCKET)
        return INVALID_SOCKET;

    // Ensure that waiting for I/O on this socket won't result in undefined
    // behavior.
    if (!IsSelectableSocket(hSocket)) {
        CloseSocket(hSocket);
        LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
        return INVALID_SOCKET;
    }

#ifdef SO_NOSIGPIPE
    int set = 1;
    // Set the no-sigpipe option on the socket for BSD systems, other UNIXes
    // should use the MSG_NOSIGNAL flag for every send.
    setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
#endif

    // Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
    SetSocketNoDelay(hSocket);

    // Set the non-blocking option on the socket.
    if (!SetSocketNonBlocking(hSocket, true)) {
        CloseSocket(hSocket);
        LogPrintf("CreateSocket: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
    }
    return hSocket;
}
/**
 * Try to create a socket file descriptor with specific properties in the
 * communications domain (address family) of the specified service.
 *
 * For details on the desired properties, see the inline comments in the source
 * code.
 */
Try to create a socket file descriptor with specific properties in the
communications domain (address family) of the specified service.

For details on the desired properties, see the inline comments in the source
code.
Try to create a socket file descriptor with specific properties in the communications domain ( address family ) of the specified service . For details on the desired properties see the inline comments in the source code .
SOCKET CreateSocket(const CService &addrConnect)
{
    struct sockaddr_storage sockaddr;
    socklen_t len = sizeof(sockaddr);
    if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
        LogPrintf("Cannot create socket for %s: unsupported network\n", addrConnect.ToString());
        return INVALID_SOCKET;
    }
    SOCKET hSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
    if (hSocket == INVALID_SOCKET)
        return INVALID_SOCKET;
    if (!IsSelectableSocket(hSocket)) {
        CloseSocket(hSocket);
        LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
        return INVALID_SOCKET;
    }
#ifdef SO_NOSIGPIPE
    int set = 1;
    setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
#endif
    SetSocketNoDelay(hSocket);
    if (!SetSocketNonBlocking(hSocket, true)) {
        CloseSocket(hSocket);
        LogPrintf("CreateSocket: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
    }
    return hSocket;
}
SOCKET CreateSocket ( const CService & addrConnect ) { struct sockaddr_storage sockaddr ; socklen_t len = sizeof ( sockaddr ) ; if ( ! addrConnect . GetSockAddr ( ( struct sockaddr * ) & sockaddr , & len ) ) { LogPrintf ( " \n " , addrConnect . ToString ( ) ) ; return INVALID_SOCKET ; } SOCKET hSocket = socket ( ( ( struct sockaddr * ) & sockaddr ) -> sa_family , SOCK_STREAM , IPPROTO_TCP ) ; if ( hSocket == INVALID_SOCKET ) return INVALID_SOCKET ; if ( ! IsSelectableSocket ( hSocket ) ) { CloseSocket ( hSocket ) ; LogPrintf ( " \n " ) ; return INVALID_SOCKET ; } #ifdef SO_NOSIGPIPE int set = 1 ; setsockopt ( hSocket , SOL_SOCKET , SO_NOSIGPIPE , ( void * ) & set , sizeof ( int ) ) ; #endif SetSocketNoDelay ( hSocket ) ; if ( ! SetSocketNonBlocking ( hSocket , true ) ) { CloseSocket ( hSocket ) ; LogPrintf ( " \n " , NetworkErrorString ( WSAGetLastError ( ) ) ) ; } return hSocket ; }
Try to create a socket file descriptor with specific properties in the
 communications domain (address family) of the specified service.
Try to create a socket file descriptor with specific properties in the communications domain ( address family ) of the specified service .
// Create a sockaddr from the specified service. // Create a TCP socket in the address family of the specified service. // Ensure that waiting for I/O on this socket won't result in undefined // behavior. // Set the no-sigpipe option on the socket for BSD systems, other UNIXes // should use the MSG_NOSIGNAL flag for every send. // Set the no-delay option (disable Nagle's algorithm) on the TCP socket. // Set the non-blocking option on the socket.