Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I need help with this Assignment. It has to do with socket programming in C. I'd really appreciate it. Here is the sample code I

I need help with this Assignment. It has to do with socket programming in C. I'd really appreciate it.

image text in transcribed

image text in transcribed

Here is the sample code I need to go off for my Client and Server:

------------------Client-----------------------------

#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 ) ) 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; }

--------------------Client------------------------------

--------------------Server-----------------------------

#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( int argc, char *argv[] ) { 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 ) ) 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; }

--------------------------------Server-----------------------------------

Your objective for this assignment is to write a file transfer utility. The server program waits for a file request and responds with either an indication of error or the file requested and the client program requests and saves a file from the server based on user input. The client should follow these general steps: 1. Accept the filename from the command line 2. Send the filename to the server 3. Receive the server's response and process it (a) If the server indicates an error, display a message to the user and quit; no file is saved (b) If the server indicates success, receive and save the file to the local directory using the filename from the command line and quit The client may overwrite the local file if it exists. You should differentiate the error messages caused by errors on the client side (e.g., connection refused, missing arguments) from the errors received from the server (file unavailable) The server should follow these general steps: 1. Wait for a client connection 2. Receive the filename from the client 3. Send a response to the client (a) If the server can't read the file for any reason (doesn't exist, bad permissions, etc.), then send an indication of error to the client and quit (b) If the server can read the file, then send the file to the client and quit The format and structure of the messages sent from server to client are up to you, though you should strive for low overhead in your messages. Your programs do not need to differentiate the possible error conditions related to the file on the server (doesn't exist, bad permissions, etc.). A simple error that encompasses all possibilities is acceptable. The client should accept three command line arguments. The first is the server, which may be provided as an IP address or a hostname. The second argument is an integer indicating the server port number. The third is the file name to request from the server, which is also the filename used to save the file. The server should accept one command line argument: the port number on which it should listen, as an integer. Use port numbers 2000 or higher to avoid permission problems. The client executable must be called file_client and the server executable must be called file server Your objective for this assignment is to write a file transfer utility. The server program waits for a file request and responds with either an indication of error or the file requested and the client program requests and saves a file from the server based on user input. The client should follow these general steps: 1. Accept the filename from the command line 2. Send the filename to the server 3. Receive the server's response and process it (a) If the server indicates an error, display a message to the user and quit; no file is saved (b) If the server indicates success, receive and save the file to the local directory using the filename from the command line and quit The client may overwrite the local file if it exists. You should differentiate the error messages caused by errors on the client side (e.g., connection refused, missing arguments) from the errors received from the server (file unavailable) The server should follow these general steps: 1. Wait for a client connection 2. Receive the filename from the client 3. Send a response to the client (a) If the server can't read the file for any reason (doesn't exist, bad permissions, etc.), then send an indication of error to the client and quit (b) If the server can read the file, then send the file to the client and quit The format and structure of the messages sent from server to client are up to you, though you should strive for low overhead in your messages. Your programs do not need to differentiate the possible error conditions related to the file on the server (doesn't exist, bad permissions, etc.). A simple error that encompasses all possibilities is acceptable. The client should accept three command line arguments. The first is the server, which may be provided as an IP address or a hostname. The second argument is an integer indicating the server port number. The third is the file name to request from the server, which is also the filename used to save the file. The server should accept one command line argument: the port number on which it should listen, as an integer. Use port numbers 2000 or higher to avoid permission problems. The client executable must be called file_client and the server executable must be called file server

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions