In Windows, there are two communication modes of sockets: blocking mode and non-blocking mode. A socket operating in blocking mode waits for an I/O operation to complete before returning (or a blocking operation can be invoked using the WSACancelBlockingCall() call). The blocking socket is simple to program and easy to implement. Because of this, the default mode of operation for a socket is set to block.

The following is a test of blocking mode TCP streaming socket programming, one server two client server-side program:

#include < WinSock2.h>
#pragma comment ( lib, "ws2_32" )
#include < stdio.h>
int main(a)
{
    printf( "Server side program /n" );
    / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (1) loading -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    WSADATA wsaData;
    WORD wVersionRequested= MAKEWORD( 2 ,2 );
    if( WSAStartup( wVersionRequested,& wsaData)! =0 )
    {
        printf( "WSAStartup() Failed,Error= [%d] /n" , WSAGetLastError());
        return 1 ;
    }
    else
        printf( "① Loading successful /n" );
    //----------------② Create streaming socket --------------
    SOCKET s= socket( AF_INET, SOCK_STREAM, 0 );
    if ( s== INVALID_SOCKET)
    {
        printf( "Socket () Failed,Error= [%d] /n" , WSAGetLastError());
        return 1 ;
    }
    else
        printf( "[%d] /n" , s);
    //-----------------③ Bind the local address ------------------
    struct sockaddr_in Sadd;
    Sadd.sin_family= AF_INET;
    Sadd.sin_port= htons( 1111 );
    Sadd.sin_addr.s_addr= inet_addr( "192.168.31.1" );
    if ( bind( s,( sockaddr*)& Sadd, sizeof ( Sadd))== SOCKET_ERROR)
    {
        printf( "Bind () Failed,Error= [%d] /n" , WSAGetLastError());
        return 1 ;
    }
    else
        printf( "③ Bind successfully, local IP address: [%s], port number: [%d] /n" , inet_ntoa( Sadd.sin_addr), ntohs( Sadd.sin_port));
    //------------------④ Enter the listening state --------------------
    if ( listen( s, 5 )== SOCKET_ERROR)
    {
        printf( "Listen () Failed,Error= [%d] /n" , WSAGetLastError());
        return 1 ;
    }
    else
        printf( "④ Enter listening state /n" );
    //------------------⑤ loop to accept the client's connection request -----------------
    printf( "⑤ Wait for client connection request /n/n" );
    struct sockaddr_in Cadd;
    int CaddLen= sizeof ( Cadd);
    while ( TRUE )
    {
        SOCKET c= accept( s,( sockaddr*)& Cadd,& CaddLen);
        if ( c== INVALID_SOCKET)
        {
            printf( "Accept () Failed,Error= [%d] /n" , WSAGetLastError());
            return 1 ;
        }
        else //************** Start sending and receiving ********************
        {
            printf( "Client has arrived, the socket created for this connection is: [%d] /n" , c);
            printf( "Client IP address: [%s], port: [%d] /n" , inet_ntoa( Cadd.sin_addr), ntohs( Cadd.sin_port));
            while ( 1 )
            {
                / / -- -- -- -- -- -- -- -- -- -- -- --
                char Rbuf[ 256 ];
                memset( Rbuf, 0 ,sizeof ( Rbuf));
                int SRecv= recv( c, Rbuf, 256 ,0 );
                if ( SRecv== SOCKET_ERROR)
                {
                    printf( "Recv () Failed,Error= [%d] /n" , WSAGetLastError());
                    break ;
                }
                else if ( SRecv== 0 )
                {
                    printf( "No data was received from the client, or the client has closed this connection!! /n" );
                    break ;
                }
                else //------ correctly receive --
                {
                    printf( "Received data: [%s] /n" , Rbuf);
                    char Sbuf[]= "Hello! I am a server" ;
                    int isend= send( c, Sbuf, sizeof ( Sbuf), 0 );
                    if ( isend== SOCKET_ERROR)
                    {
                        printf( "Send () Failed,Error= [%d] /n" , WSAGetLastError());
                        break ;
                    }
                    else if ( isend== 0 )
                    {
                        printf( "Message sending failed /n" );
                        closesocket( c);
                        break ;
                    }
                    else
                        printf( "Message to customer [%s] sent /n" , Sbuf);
                } //end Receives correctly
            } //end while2
        } //end Starts receiving
        closesocket( c);
        printf( "Close this connection socket [%d], and [%s] connection complete /n/n" , c, inet_ntoa( Cadd.sin_addr));
    } //end while 1
    //---------------⑥ Close and release --------------------
    closesocket( s);
    WSACleanup();
    return 0 ;
}
Copy the code

Client no. 1:

#include < WinSock2.h>
#pragma comment( lib, "ws2_32" )
#include < stdio.h>
int main(a)
{
    printf( "No. 1 client program /n" );
    / / -- -- -- -- -- -- -- -- -- -- - (1) loading -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
    WSADATA wsaData;
    WORD wVersionRequested= MAKEWORD( 2 ,2 );
    if( WSAStartup( wVersionRequested,& wsaData)! =0 )
    {
        printf( "WSAStartup Failed,Error= [%d] /n" , WSAGetLastError());
        return 1 ;
    }
    else
        printf( "① Loading successful /n" );
    //-------------② Create streaming socket -----------------
    SOCKET c1= socket( AF_INET, SOCK_STREAM, 0 );
    if ( c1== INVALID_SOCKET)
    {
        printf( "Socket () Failed,Error= [%d] /n" , WSAGetLastError());
        return 1 ;
    }
    else
        printf( "② Created socket: [%d] /n" , c1);
    //------------- Binding address ---------------------
    struct sockaddr_in C1add;
    C1add.sin_family= AF_INET;
    C1add.sin_port= htons( 2222 );
    C1add.sin_addr.s_addr= inet_addr( "192.168.31.2" );
    if ( bind( c1,( sockaddr*)& C1add, sizeof ( C1add))== SOCKET_ERROR)
    {
        printf( "Bind () Failed,Error= [%d] /n" , WSAGetLastError());
        return 1 ;
    }
    else
        printf( "Binding successful, local IP address: [%s], port number: [%d] /n" , inet_ntoa( C1add.sin_addr), ntohs( C1add.sin_port));
    //------------③ Connection request ---------------
    struct sockaddr_in Sadd;
    Sadd.sin_family= AF_INET;
    Sadd.sin_port= htons( 1111 );
    Sadd.sin_addr.s_addr= inet_addr( "192.168.31.1" );
    if ( connect( c1,( sockaddr*)& Sadd, sizeof ( Sadd))==- 1 )
    {
        printf( "Failed connect(),Error= [%d] /n" , WSAGetLastError());
        return 1 ;
    }
    else ********* If the connection is successful, you can send and receive ************
    {
        printf( "③ Successful connection, server IP address: [%s], port number: [%d] /n" , inet_ntoa( Sadd.sin_addr), ntohs( Sadd.sin_port));
        int a;
        printf( "Want to send data? (Type "1" to send, type other values to exit) );
        scanf( "%d" ,& a);
        while ( a== 1 )
        {
            / / -- -- -- -- -- -- -- --
            char S_buf[]= "Hello! I am a client 1" ;
            int isend= send( c1, S_buf, strlen( S_buf), 0 );
            if ( isend== SOCKET_ERROR)
            {
                printf( "Failed send(),Error= [%d], or server unexpectedly shut down /n" , WSAGetLastError());
                return 1 ;
            }
            else if( isend! =0 )
                printf( "Message [%s] sent /n" , S_buf);
            else
                printf( "Message cannot be sent to client /n" );
            / / -- -- -- -
            char R_buf[ 256 ];
            int RRecv;
            memset( R_buf, 0 ,sizeof ( R_buf));
            RRecv= recv( c1, R_buf, 256 ,0 );
            if ( RRecv== SOCKET_ERROR)
            {
                printf( "Failed recv(),Error= [%d] /n" , WSAGetLastError());
                return 1 ;
            }
            else if ( RRecv== 0 )
                printf( "Could not receive any data from the server, or the server unexpectedly shut down /n" );
            else
            {
                printf( "Received data from server: [%s] /n" , R_buf);
                printf( "Do you want to continue sending data? (Type "1" to continue sending, type other values to exit) );
                scanf( "%d" ,& a);
                if( a! =1 )
                    break; }}/ / -- -- -- -- -- -- -- end to end
        printf( "Opt out of connection /n" );
    } / / -- -- -- -- -- - end to end
    //-------------------④ Close and release ------------
    closesocket( c1);
    WSACleanup();
    printf( "④ Complete connection with server /n" );
    return 0 ;
}
Copy the code

The program of client 2 is similar to that of Client 1, except that the prompt text is changed to “Client 2” and the BOUND IP address is different. Start the server side, then start client 1, then start client 2, and you should see something like this: