c ++ Socket client and server versions of two examples

Client

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include<arpa/inet.h>
#include <error.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
//执行 ./ClientLinux.out 127.0.0.1 8080
int main(int arg, char* args[])
{
    for( Int I = 0 ; I < . 3 ; I ++ ) 
    { 
        the printf ( " Arg:% S \ n- " , args [I]); 
    } 
    IF (Arg < . 3 ) 
    { 
        the printf ( " Arg <. 3 \ n- " );
         return - . 1 ; 
    } 
    int Port = atoi (args [ 2 ]);
     // Step: initializing a socket example 
    int ST = socket (AF_INET, SOCK_STREAM, 0 ); 

    // Step: a defined IP address and configuration settings value 
    struct the sockaddr_in addr;
    // memory initialization, initialize the memory pointed to by variable filling addr n bytes with sign 0 
    Memset (& addr, 0 , the sizeof (addr));
     // Set the protocol used is TCP / IP protocol 
    addr.sin_family = AF_INET;
     // set the port number 
    addr.sin_port = the htons (port);
     // set the IP address 
    addr.sin_addr.s_addr the inet_addr = (args [ . 1 ]); 

    // step 3: start the server connection 
    IF (connect (ST, ( struct the sockaddr *) & addr, the sizeof (addr)) == - . 1 ) 
    { 
        the printf ( " Connect Fail% S \ n- " , the strerror (errno));
        return EXIT_FAILURE; 
    } 

    // fourth step: initialization information to be transmitted and transmits the data send function through 
    the while ( . 1 ) 
    { 
        char S [ 1024 ]; 
        Memset (S, 0 , the sizeof (S));
         // Read (STDIN_FILENO, S, the sizeof (S)); 
        Scanf ( " % S " , S);
         // strcpy (S, "Hello World"); 
        IF (Send (ST, S, strlen (S), 0 ) == - . 1 ) 
        { 
            the printf ( " Send Fail% S \ n- " , the strerror (errno));
            return EXIT_FAILURE; 
        } 
        // before the client server receiving a message buffer where 
        Memset (S, 0 , the sizeof (S));
         // client accept message returned from the server 
        int RC = the recv (ST, S, the sizeof (S ), 0 );
         IF (RC> 0 ) // If successful acceptance message server, print out 
        { 
            the printf ( " Client the recv:% S \ n- " , S); 
        } 
        the else 
        { 
            BREAK ; 
        } 
    } 

    Close (ST) ; 
    getchar (); 
    return EXIT_SUCCESS;
}

Server

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include<arpa/inet.h>
#include <error.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
//执行命令 ./ServerLinux.out 8080
int main(int arg, char* args[])
{
    for (int I = 0 ; I < 2 ; I ++ ) 
    { 
        the printf ( " Parameters% D:% S " , I, args [I]); 
    } 
    IF (Arg < 2 ) 
    { 
        return - . 1 ; 
    } 
    int Port = atoi (args [ . 1 ]);
     int ST = socket (AF_INET, SOCK_STREAM, 0 ); 

    // the setsockopt set an attribute of the socket, so that the address can be reused. 
    int ON = 0 ;
     IF (the setsockopt (ST, SOL_SOCKET, the SO_REUSEADDR, & ON, the sizeof (ON)) == - . 1) 
    { 
        The printf ( " the setsockopt failed:% S \ n- " , the strerror (errno));
         return EXIT_FAILURE; 
    } 
    struct the sockaddr_in addr; 
    Memset ( & addr, 0 , the sizeof (addr)); 
    addr.sin_family = AF_INET; 
    addr.sin_port = htons (Port);
     // INADDR_ANY means that all Ip addresses on the server. A server can have multiple ip address. All the ip address bound to the socket of the machine 
    addr.sin_addr.s_addr = htonl (INADDR_ANY); 

    // the address of the server to bind ip 
    IF (the bind (ST, ( struct the sockaddr *) & addr, the sizeof(addr)) == - . 1 ) 
    { 
        the printf ( " the bind Fail% S \ n- " , the strerror (errno));
         return EXIT_FAILURE; 
    } 
    // Server begins listening. 20 represents the number of simultaneous connections over (20 concurrent) 
    IF (the listen (ST, 20 ) == - . 1 ) 
    { 
        the printf ( " the listen Fail% S \ n- " , the strerror (errno));
         return EXIT_FAILURE; 
    } 
    char S [ 1024 ];
     int client_st = 0 ; // client Socket 
    the socklen_t len =0 ; //
     struct the sockaddr_in client_addr; // client the IP 
    void * P = & client_addr;
     int I = 0 ;
     for (I = 0 ; I < 50 ; I ++ ) 
    { 
        Memset ( & client_addr, 0 , the sizeof (client_addr)); 
        len socklen_t = sizeof (client_addr);
         // the Accept will block until a client connects over. accept returned to the client descriptor 
        client_st = accept (ST, ( struct the sockaddr *) & client_addr, & len);
         IF(client_st == - . 1 ) 
        { 
            the printf ( " Accept Fail% S \ n- " , the strerror (errno));
             return EXIT_FAILURE; 
        } 
        // print client ip address 
        the printf ( " Accept ip:% S \ n- " , inet_ntoa ( client_addr.sin_addr)); 
        Memset (S, 0 , the sizeof ( 1024 )); 


        /// / accept message from the client 
        // IF (the recv (client_st, S, the sizeof (S), 0) == -1)
         / / {
         //     the printf ( "% S the receive Fail \ n-", the strerror (errno));
         //    Close (client_st);
         //     return EXIT_FAILURE;
         // }
         // the printf ( "Content:% S \ n-", S); 

        the while ( . 1 ) 
        { 
            Memset (S, 0 , the sizeof (S));
             // the recv are blocking call, if the client does not close, then the server will die and other 
            int rc = recv (client_st, S, sizeof (S), 0 );
             IF (rc> 0 ) 
            { 
                printf ( " receive Success% S \ the n- " , S); 
                Memset (S, 0 ,the sizeof (S));
                 // accept data entry from the console 
                Scanf ( " % S " , S);
                 // the server returns to the client message 
                Send (client_st, S, the sizeof (S), 0 ); 
            } 
            the else  IF (RC == 0 ) // if the client closes the connection, server receiving end, it returns 0 
            { 
                the printf ( " the receive Close \ n- " );
                 BREAK ; 
            } 
            the else 
            { 
                the printf ( " the receive Fail% S \ n-" , The strerror (errno));
                 BREAK ; 
            } 
        } 
        Close (client_st); // Close client Socket 
    } 
    Close (ST); 
    getchar (); 
}

 

Guess you like

Origin www.cnblogs.com/caoruipeng/p/11782982.html