Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Enhance timeclnt.c and timeserv.c to enable them to display which connection number of the client. Compile the timeserv.c as described in Step 1. 1. compile

Enhance timeclnt.c and timeserv.c to enable them to display which connection number of the client.

Compile the timeserv.c as described in Step 1.

1. compile the program

>gcc timeserv.c -o timeserv

2. run it as a server ( If the port number is being used, try different port number. In this case, you have change the port nuber in the timeserv.c file (#define PORTNUM 13000).) The telnet command in 3. needs to use the same port number assigned in timeserv.c.

>timeserv &

3. open another shell and compile the timeclnt.c:

> gcc timeclnt.c -o timeclnt

4. From client connect to the server

>timeclnt 13999

5. Modify the timeserv.c so that the client side will receive a message stating "You are the Number 5 user!" It means that the server will be tracking how many connections have been accepted

Thing to submit:

1. Submit a .c file of updated timeslnt.c

2. A screen shot that shows the running result.

3. Describe all the system calls used to enable the client program to receive messages from server.

/* timeclnt.c - a client for timeserv.c * * usage: timeclnt hostname portnumber * */ #include #include #include #include #include #include /* for exit()*/ #include /*bzero */ #include /* read() */ #include /* read() */ #define oops(msg) { perror(msg); exit(1); }

main(int ac, char *av[]) { struct sockaddr_in servadd; /* the number to call */ struct hostent *hp; /* used to get number */ int sock_id, sock_fd; /* the socket and fd */ char message[BUFSIZ]; /* to receive message */ int messlen; /* for message length */

/* * * Step 1: Get a socket * */

sock_id = socket( AF_INET, SOCK_STREAM, 0 ); /* get a line */ if ( sock_id == -1 ) oops( "socket" ); /* or fail */

/* * * Step 2: connect to server * * need to build address (host,port) of server first * */

bzero( &servadd, sizeof( servadd ) ); /* zero the address */

hp = gethostbyname( av[1] ); /* lookup host's ip # */ if (hp == NULL) oops(av[1]); /* or die */ bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);

servadd.sin_port = htons(atoi(av[2])); /* fill in port number */

servadd.sin_family = AF_INET ; /* fill in socket type */

/* now dial */ if ( connect(sock_id,(struct sockaddr *)&servadd, sizeof(servadd)) !=0) oops( "connect" );

/* * * Step 3: transfer data from server, then hangup * */

messlen = read(sock_id, message, BUFSIZ); /* read stuff */ if ( messlen == - 1 ) oops("read") ; if ( write( 1, message, messlen ) != messlen ) /* and write to */ oops( "write" ); /* stdout */ close( sock_id ); }

timeserv.c

#include  #include  #include  #include  #include  #include  #include  #include  #include  #define PORTNUM 13000 /* our time service phone number */ #define HOSTLEN 256 #define oops(msg) { perror(msg) ; exit(1) ; } int main(int ac, char *av[]) { struct sockaddr_in saddr; /* build our address here */ struct hostent *hp; /* this is part of our */ char hostname[HOSTLEN]; /* address */ int sock_id,sock_fd; /* line id, file desc */ FILE *sock_fp; /* use socket as stream */ /*char *ctime(); */ /* convert secs to string */ time_t thetime; /* the time we report */ /* * Step 1: ask kernel for a socket */ sock_id = socket( PF_INET, SOCK_STREAM, 0 ); /* get a socket */ if ( sock_id == -1 ) oops( "socket" ); /* * Step 2: bind address to socket. Address is host,port */ bzero( (void *)&saddr, sizeof(saddr) ); /* clear out struct */ gethostname( hostname, HOSTLEN ); /* where am I ? */ hp = gethostbyname( hostname ); /* get info about host */ /* fill in host part */ bcopy( (void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length); saddr.sin_port = htons(PORTNUM); /* fill in socket port */ saddr.sin_family = AF_INET ; /* fill in addr family */ if ( bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0 ) oops( "bind" ); /* * Step 3: allow incoming calls with Qsize=1 on socket */ if ( listen(sock_id, 1) != 0 ) oops( "listen" ); /* * main loop: accept(), write(), close() */ while ( 1 ){ sock_fd = accept(sock_id, NULL, NULL); /* wait for call */ printf("Wow! got a call! "); if ( sock_fd == -1 ) oops( "accept" ); /* error getting calls */ sock_fp = fdopen(sock_fd,"w"); /* we'll write to the */ if ( sock_fp == NULL ) /* socket as a stream */ oops( "fdopen" ); /* unless we can't */ thetime = time(NULL); /* get time */ /* and convert to strng */ fprintf( sock_fp, "The time here is .." ); fprintf( sock_fp, "%s", ctime(&thetime) ); fclose( sock_fp ); /* release connection */ } } 

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

Recommended Textbook for

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions