Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please modify the following socket program and let the server send itself (the executable file) to the client. Socket Programming Example ============================================= Socket Server Example

Please modify the following socket program and let the server send itself (the executable file) to the client.

Socket Programming Example

=============================================

Socket Server Example (server.c) ============================================= #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int listenfd = 0, connfd = 0; struct sockaddr_in serv_addr; char sendBuff[1025]; time_t ticks; listenfd = socket(AF_INET, SOCK_STREAM, 0); memset(&serv_addr, '0', sizeof(serv_addr)); memset(sendBuff, '0', sizeof(sendBuff)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(5000); bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); listen(listenfd, 10); while(1) { connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); ticks = time(NULL); snprintf(sendBuff, sizeof(sendBuff), "%.24s ", ctime(&ticks)); write(connfd, sendBuff, strlen(sendBuff)); close(connfd); sleep(1); } } =============================================

Discussions:

1) The call to the function socket() creates an UN-named socket inside the kernel and returns an integer known as socket descriptor.

2) This function takes domain/family as its first argument. For Internet family of IPv4 addresses we use AF_INET.

3) The second argument SOCK_STREAM specifies that the transport layer protocol that we want should be reliable ie it should have acknowledgement techniques. For example : TCP

4) The third argument is generally left zero to let the kernel decide the default protocol to use for this

5) The call to the function bind() assigns the details specified in the structure serv_addr to the socket created in the step above. The details include, the family/domain, the interface to listen on(in case the system has multiple interfaces to network) and the port on which the server will wait for the client requests to come.

6) The call to the function listen() with second argument as 10 specifies maximum number of client

7) After the call to listen(), this socket becomes a fully functional listening socket.

8) In the call to accept(), the server is put to sleep and when for an incoming client request, the three way TCP handshake* is complete, the function accept () wakes up and returns the socket descriptor representing the client socket.

9) The call to accept() is run in an infinite loop so that the server is always running and the delay or sleep of 1 sec ensures that this server does not eat up all of your CPU processing.

10) As soon as server gets a request from client, it prepares the date and time and writes on the client socket through the descriptor returned by accept().

============================================= Socket Client Example (client.c)

============================================= #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int sockfd = 0, n = 0; char recvBuff[1024]; struct sockaddr_in serv_addr; if(argc != 2) { printf(" Usage: %s ",argv[0]); return 1; } memset(recvBuff, '0',sizeof(recvBuff)); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf(" Error : Could not create socket "); return 1; } memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(5000); if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0) { printf(" inet_pton error occured "); return 1; } if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf(" Error : Connect Failed "); return 1; } while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0) { recvBuff[n] = 0; if(fputs(recvBuff, stdout) == EOF) { printf(" Error : Fputs error "); } } if(n < 0) { printf(" Read error "); } return 0; }

============================================= Discussions: 1) A socket is created through call to socket() function.

2) Information like IP address of the remote host and its port is bundled up in a structure and a call to function connect() is made which tries to connect this socket with the socket (IP address and port) of the remote host.

3) Note that here we have not bind our client socket on a particular port as client generally use port assigned by kernel as client can have its socket associated with any port but In case of server it has to be a well known socket, so known servers bind to a specific port like HTTP server runs on port 80

4)Once the sockets are connected, the server sends the data (date+time) on clients socket through clients socket descriptor and client can read it through normal read call on the its socket descriptor.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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