Question: hello every one # i have this program which is soket program client side and i want you to let the program to do this

hello every one

# i have this program which is soket program client side and i want you to let the program to do this task :

(The Client calculates the round trip delay from its communications with the Server, and displays the result on its monitor)

just add function or whatever to the program to do this task (please make the part that you will add clear and use the bold font) .

program is

#include #include #include #include #include #include #include #include #include #include #include #include #include

/* the port client will be connecting to */ #define PORT "4120"

/* max number of bytes we can get at once */ #define MAXDATASIZE 100

/* get sockaddr, IPv4 or IPv6: */ void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) { return &(((struct sockaddr_in*)sa)->sin_addr); }

return &(((struct sockaddr_in6*)sa)->sin6_addr); }

/* set up to read keyboard */ static struct termios g_old_kbd_mode;

static void cooked(void) { tcsetattr(0, TCSANOW, &g_old_kbd_mode); }

/* set keyboard to raw mode */ static void raw(void) { static char init; struct termios new_kbd_mode;

if(init) return;

/* put keyboard (stdin, actually) in raw, unbuffered mode */ tcgetattr(0, &g_old_kbd_mode); memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios)); new_kbd_mode.c_lflag &= ~(ICANON | ECHO); new_kbd_mode.c_cc[VTIME] = 0; new_kbd_mode.c_cc[VMIN] = 1; tcsetattr(0, TCSANOW, &new_kbd_mode);

/* when we exit, go back to normal, "cooked" mode */ atexit(cooked);

init = 1; }

/* return true if key available */ static int kbhit(void) { struct timeval timeout; fd_set read_handles; int status;

raw();

/* check stdin (fd 0) for activity */ FD_ZERO(&read_handles); FD_SET(0, &read_handles); timeout.tv_sec = timeout.tv_usec = 0; status = select(0 + 1, &read_handles, NULL, NULL, &timeout); if(status < 0) { printf("select() failed in kbhit() "); exit(1); } return status; }

/* get key from keyboard */ static int getkey(void) { unsigned char temp;

raw();

/* stdin = fd 0 */ if(read(0, &temp, 1) != 1) return 0; return temp; }

/* run client */ int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; char msg[MAXDATASIZE]; struct addrinfo hints, *servinfo, *p; int rv; char s[INET6_ADDRSTRLEN]; int r; char ch; char port[4120] = PORT; char host[4120] = "localhost"; int i = 0;

/* initialize host and port */ if (argc == 3) { strcpy(host,argv[1]); strcpy(port,argv[2]); }

/* get server ip and host from user */ else { printf("Enter server ip : "); scanf("%s",host); printf("make connetion with the server (%s): ",PORT); scanf("%s",port); } /* seed random number generator */ srand(time(NULL));

memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM;

if ((rv = getaddrinfo(host,port, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s ", gai_strerror(rv)); return 1; }

/* loop through all the results and connect to the first one we can */ for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("client: socket"); continue; }

if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("client: connect"); continue; }

break; }

if (p == NULL) { fprintf(stderr, "client: failed to connect "); return 2; }

/* set to non blocking */ fcntl(sockfd, F_SETFL, O_NONBLOCK);

inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); printf("client: connecting to %s ", s);

freeaddrinfo(servinfo); /* free this structure */

while(1) {

/* client receves request from server */ numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0); /* got data */ if(numbytes>0) {

buf[numbytes] = '\0';

printf("client: received '%s' from server ",buf);

/* client receives exit command from server */ if(buf[0]=='e' || buf[0]=='E' || buf[0]=='X') { printf("client receives exit command from server "); break; } }

/* server request random number */ if(strcmp(buf,"R")==0) { r = rand()%101; sprintf(buf,"%d ",r); printf("client: sends '%d' to server ",r); /* client sends random number to server */ if (send(sockfd, buf, strlen(buf), 0) == -1) { perror("send"); } }

/* key pressed? */ if(kbhit()) { /* get command key */ ch = getkey(); msg[i++] = ch;

if(ch == ' ') { msg[i] = 0; i = 0;

/*send command to server */ printf("client: sends mesage %s to server ",msg); if (send(sockfd, msg, strlen(msg), 0) == -1) { perror("send"); } }

}

} /* end while */

printf("Client terminates "); close(sockfd); /* all done */

return 0; }

thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!