This is a problem related to computer networks and solution needs to be in Java
ID: 3664342 • Letter: T
Question
This is a problem related to computer networks and solution needs to be in Java or python.
Question:Implement a concurrent server architecture using TCP. Server need to be able to handle a minimum of five requests at a time.
Details(hint) - s
A client takes five arguments: the name of the server machine, the port number to which it
should connect, the execution_count, the time_delay (in seconds) and the command (in that
order). It translates the server name to its IP address using the gethostbyname() system call,
uses the IP address to fill in a sockaddr_in structure, and then opens a socket to the server
using the socket and connect system calls. Next, the client sends the command as an ASCII
string, using the write system call. Finally, it reads the reply, using the read call, and displays
the result on the screen using the write system call.
The server takes one argument, which is the port number on which it listens for an incoming
command. It fills in a sockaddr_in structure with the port number, with the incoming address
field (s_addr) set to INADDR_ANY. The server should display its IP Address / servername
for the client to connect to it. The server uses the socket, bind, and listen system calls to
create a socket, bind the port number to it, and to listen for a request. On getting the request,
server queues it and then accepts it. It reads the message sent to it, executes the
corresponding command using the Unix system call and writes the result to a temporary file.
It then opens the file for reading, reads the result, and uses the write call to write the results
back to the client.
TCP/IP does not provide message boundaries. So, the client does not know how long a result
to read from the server. To get around this, the first 10 bytes of the server's message is the
length of the reply, in ASCII. For example, if the length of the reply is 15 bytes, the first 10
bytes would be ASCII 5, ASCII 1 and eight 0s (use sprintf call to convert from binary to
ASCII).
For this question, the session termination is initiated by the server when it has executed the
command execution_times. The session also may be terminated by entering rcend on the
client terminal.
Source code execution commands:
Server side (applicable for both Connection-oriented & Connectionless)
Execution command:
~$ <rscmnd> -p <port-number> -h <help>
where:
a. <rscmnd> is for running the executable of server source code
b. <port-number> is the port number at which the server will listen to for connections (use
port number greater than 10000 to be in safer side).
c. <help> will print out how to run the executable
Client side (applicable for all clients)
Execution command:
~$ <rccmnd> -s <server-name> -p <server-port> -c <command> -n <num_exec> -d <delay> -h <help>
where:
a. <rccmnd> is for running the executable of client src code
b. <server-name> is the domain name of the server
c. <server-port> is the port number at which server is listening
d. <command> is the command to be executed by the server
e. <num_exec> is number of times the command to be executed
f. <delay> is the delay in consecutive executions
g. <help> will print out how to run the executable
Explanation / Answer
pid_t child_make(int i,int listenfd,int addrlen) { pid_t pid; if((pid=fork())>0) rteurn(pid); //parent child_main(i,listenfd,addrlen); //never returns } void child_main(int i,int listenfd,int addrlen) { int connfd; socklen_t clilen; struct sockaddr *cliaddr; cliaddr=malloc(addrlen); printf("Child %ld starting ",(long)getpid()); for(;;) { clilen=addrlen; connfd=accept(listenfd,cliaddr,&clilen); /* perform processing jobs */ while(1){ if ((newfd = accept(listener, (struct sockaddr *)&remoteaddr,&addrlen)) == -1) { perror("accept"); } else { //fork a new child whenever we have a new client connection pid=fork(); if(pid==0){ //handle the client printf("new connection from %s on socket %d ", inet_ntoa(remoteaddr.sin_addr),newfd); printf("Recieved %d bytes from the client",read(newfd,buf,sizeof(buf))); printf("%s",buf); //do any other tasks as needed exit(0); } } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.