Question
I NEED THE CODE TO DO THE FOLLOWING. PLEASE ADD COMMENTS SO I CAN SEE AND STUDY FOR WHEN COMPLETED THE CLIENT 1. Call lookup
I NEED THE CODE TO DO THE FOLLOWING. PLEASE ADD COMMENTS SO I CAN SEE AND STUDY FOR
WHEN COMPLETED THE CLIENT
1. Call lookup and connect
2. Repeat the following forever
(a) Get two numbers from the user
(b) Copy the numbers into a buffer
(c) Send the buffer to the server
(d) Receive the sum from the server into a buffer
(e) Copy the sum out of the buffer
(f) Print the sum
When complete, the server should do the following:
1. Call bind and listen
2. accept a connection from the client
3. Repeat the following forever
(a) Receive a buffer that contains two numbers
(b) Copy the numbers out of the buffer
(c) Add the numbers
(d) Copy the sum into the buffer
(e) Send the buffer containing sum to the client
CLIENT CODE:
#include#include #include #include #include #include #include #define SERVER_PORT "5432" #define MAX_LINE 256 /* * Lookup a host IP address and connect to it using service. Arguments match the first two * arguments to getaddrinfo(3). * * Returns a connected socket descriptor or -1 on error. Caller is responsible for closing * the returned socket. */ int lookup_and_connect( const char *host, const char *service ); int main( int argc, char *argv[] ) { char *host; char buf[MAX_LINE]; int s; int len; if ( argc == 2 ) { host = argv[1]; } else { fprintf( stderr, "usage: %s host ", argv[0] ); exit( 1 ); } /* Lookup IP and connect to server */ if ( ( s = lookup_and_connect( host, SERVER_PORT ) ) < 0 ) { exit( 1 ); } /* Main loop: get and send lines of text */ while ( fgets( buf, sizeof( buf ), stdin ) ) { buf[MAX_LINE - 1] = '\0'; len = strlen( buf ) + 1; if ( send( s, buf, len, 0 ) == -1 ) { perror( "stream-talk-client: send" ); close( s ); exit( 1 ); } } close( s ); return 0; } int lookup_and_connect( const char *host, const char *service ) { struct addrinfo hints; struct addrinfo *rp, *result; int s; /* Translate host name into peer's IP address */ memset( &hints, 0, sizeof( hints ) ); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = 0; hints.ai_protocol = 0; if ( ( s = getaddrinfo( host, service, &hints, &result ) ) != 0 ) { fprintf( stderr, "stream-talk-client: getaddrinfo: %s ", gai_strerror( s ) ); return -1; } /* Iterate through the address list and try to connect */ for ( rp = result; rp != NULL; rp = rp->ai_next ) { if ( ( s = socket( rp->ai_family, rp->ai_socktype, rp->ai_protocol ) ) == -1 ) { continue; } if ( connect( s, rp->ai_addr, rp->ai_addrlen ) != -1 ) { break; } close( s ); } if ( rp == NULL ) { perror( "stream-talk-client: connect" ); return -1; } freeaddrinfo( result ); return s; }
SERVER CODE:
#include#include #include #include #include #include #include #define SERVER_PORT "5432" #define MAX_LINE 256 #define MAX_PENDING 5 /* * Create, bind and passive open a socket on a local interface for the provided service. * Argument matches the second argument to getaddrinfo(3). * * Returns a passively opened socket or -1 on error. Caller is responsible for calling * accept and closing the socket. */ int bind_and_listen( const char *service ); int main( void ) { char buf[MAX_LINE]; int s, new_s; int len; /* Bind socket to local interface and passive open */ if ( ( s = bind_and_listen( SERVER_PORT ) ) < 0 ) { exit( 1 ); } /* Wait for connection, then receive and print text */ while ( 1 ) { if ( ( new_s = accept( s, NULL, NULL ) ) < 0 ) { perror( "stream-talk-server: accept" ); close( s ); exit( 1 ); } while ( ( len = recv( new_s, buf, sizeof( buf ), 0 ) ) ) { if ( len < 0 ) { perror( "streak-talk-server: recv" ); close( s ); exit( 1 ); } fputs( buf, stdout ); } close( new_s ); } close( s ); return 0; } int bind_and_listen( const char *service ) { struct addrinfo hints; struct addrinfo *rp, *result; int s; /* Build address data structure */ memset( &hints, 0, sizeof( struct addrinfo ) ); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; hints.ai_protocol = 0; /* Get local address info */ if ( ( s = getaddrinfo( NULL, service, &hints, &result ) ) != 0 ) { fprintf( stderr, "stream-talk-server: getaddrinfo: %s ", gai_strerror( s ) ); return -1; } /* Iterate through the address list and try to perform passive open */ for ( rp = result; rp != NULL; rp = rp->ai_next ) { if ( ( s = socket( rp->ai_family, rp->ai_socktype, rp->ai_protocol ) ) == -1 ) { continue; } if ( !bind( s, rp->ai_addr, rp->ai_addrlen ) ) { break; } close( s ); } if ( rp == NULL ) { perror( "stream-talk-server: bind" ); return -1; } if ( listen( s, MAX_PENDING ) == -1 ) { perror( "stream-talk-server: listen" ); close( s ); return -1; } freeaddrinfo( result ); return s; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started