SlideShare a Scribd company logo
CODE FOR echo_client.c:
/* A simple echo client using TCP */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_TCP_PORT 3000 /* well-known port */
#define BUFLEN 256 /* buffer length */
int main(int argc, char **argv)
{
int n, i, bytes_to_read;
int sd, port;
struct hostent *hp;
struct sockaddr_in server;
char *host, *bp, rbuf[BUFLEN], sbuf[BUFLEN];
switch(argc){
case 2:
host = argv[1];
port = SERVER_TCP_PORT;
break;
case 3:
host = argv[1];
port = atoi(argv[2]);
break;
default:
fprintf(stderr, "Usage: %s host [port]n", argv[0]);
exit(1);
}
/* Create a stream socket */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "Can't creat a socketn");
exit(1);
}
bzero((char *)&server, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
if (hp = gethostbyname(host))
bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
else if ( inet_aton(host, (struct in_addr *) &server.sin_addr) ){
fprintf(stderr, "Can't get server's addressn");
exit(1);
}
/* Connecting to the server */
if (connect(sd, (struct sockaddr *)&server, sizeof(server)) == -1){
fprintf(stderr, "Can't connect n");
exit(1);
}
printf("Transmit: n");
while(n=read(0, sbuf, BUFLEN)){ /* get user message */
write(sd, sbuf, n); /* send it out */
printf("Receive: n");
bp = rbuf;
bytes_to_read = n;
while ((i = read(sd, bp, bytes_to_read)) > 0){
bp += i;
bytes_to_read -=i;
}
write(1, rbuf, n);
printf("Transmit: n");
}
close(sd);
return(0);
}
CODE FOR echo_server.c:
/* A simple echo server using TCP */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_TCP_PORT 3000 /* well-known port */
#define BUFLEN 256 /* buffer length */
int echod(int);
void reaper(int);
int main(int argc, char **argv)
{
int sd, new_sd, client_len, port;
struct sockaddr_in server, client;
switch(argc){
case 1:
port = SERVER_TCP_PORT;
break;
case 2:
port = atoi(argv[1]);
break;
default:
fprintf(stderr, "Usage: %s [port]n", argv[0]);
exit(1);
}
/* Create a stream socket */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "Can't creat a socketn");
exit(1);
}
/* Bind an address to the socket */
bzero((char *)&server, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1){
fprintf(stderr, "Can't bind name to socketn");
exit(1);
}
/* queue up to 5 connect requests */
listen(sd, 5);
(void) signal(SIGCHLD, reaper);
while(1) {
client_len = sizeof(client);
new_sd = accept(sd, (struct sockaddr *)&client, &client_len);
if(new_sd < 0){
fprintf(stderr, "Can't accept client n");
exit(1);
}
switch (fork()){
case 0: /* child */
(void) close(sd);
exit(echod(new_sd));
default: /* parent */
(void) close(new_sd);
break;
case -1:
fprintf(stderr, "fork: errorn");
}
}
}
/* echod program */
int echod(int sd)
{
char *bp, buf[BUFLEN];
int n, bytes_to_read;
while(n = read(sd, buf, BUFLEN))
write(sd, buf, n);
close(sd);
return(0);
}
/* reaper */
void reaper(int sig)
{
int status;
while(wait3(&status, WNOHANG, (struct rusage *)0) >= 0);
}
This should be conducted in Linux Virtual Machines (VMs). PLEASE ADD EVERYTHING
MENTIONED UNDER What you needed to demonstrate in your report for part 2 and please
make sure it is correct and I will rate a thumbs up. The codes are given. Thanks Part 2: TCP
Server An Internet server at application layer uses either TCP or UDP as a transport protocol. In
this part, you will study servers operating on TCP. I. TCP Connection Mechanism In this section,
we will use the Echo service to study the TCP connection establishment and termination
mechanism. 1. Enable Wireshark in VM1 and start the capture process. 2. Start an "Echo" server
in VM1: $./echo_server [port_number] 3. Start an "Echo" client on VM2: $./echo_client
[server_IP_address] [port_number] From the client, send one or two messages. 4. TCP is a
connection-oriented protocol - a logical connection must be established between client and server
before data transfer (Figure 1). With this understanding, find out from the captured data in
Wireshark the number of TCP packets exchanged to setup a TCP connection. 5. Now terminate
the client process (but keep the server process running). The termination of the client also causes
the termination of the TCP connection (Figure 1). Find out from the captured data in Wireshark
the number of packets exchanged for the termination process. II. Concurrent Server A concurrent
server is a server that can provide service to multiple clients concurrently. In this section, we will
study how the concurrent feature of the TCP concurrent server is implemented. 6. Open another
terminal on VM1, type
The output should show that one echo_server process is running on the VM. 7. Start an "Echo"
client on VM2: $/echo_client [server_IP_address] [port_number] 8. Repeat step 6 again. You
will find out there are two echo server processes running. The second server process is a child
process created by the original concurrent server. The child process is the one that has a TCP
connection with and provides service to the client. 9. Open another terminal in VM2 and start a
second client. You will now find that there are 3 server processes running in VM1, one is the
original server while the other two are child processes. Each child process provides service to
one client. Figure 2 below illustrates the concept. 10. You can also verify that there are two TCP
connections by using netstat utility on either VM1 or VM2: $./netstat -t II. Socket Programming
In this part, you will study the source files of echo_server.c and echo_client.c to gain some
knowledge on how to write network applications based on TCP socket programming. By the end
of this part, you should be able to write a simple network application. Server Program The server
makes the following sequence of system calls to make itself available for TCP connection: [
begin{array}{l} text { sd = socket(AF_INET, SOCK_STREAM, 0); }  text { bind(sd, (struct
sockaddr *)&server, sizeof(server)); }  text { listen(sd, 5); }  text { new_sd = accept(sd,
(struct sockaddr * *)&client, &client_len); } end{array} ] The server makes a socket system
call to create a socket through which it can access the TCP/IP service. The call returns a
descriptor (sd) whose function is similar to a file descriptor. The
subsequent socket system calls through that socket will refer to this descriptor. The argument
"SOCK_STREAM" specifies that the server will use TCP as the transport-layer service. The
server then calls bind to assign IP address and port number to the just opened socket. Next, the
server calls listen to place a socket in passive (server) mode and tells the TCP module to enqueue
TCP connection requests with maximum queue size of 5 . Finally, the server calls accept to
accept an incoming connection request. The system call accept blocks the return until a TCP
connection request, which is issued by the client, arrives. When a TCP connection request
arrives, the call will return with a new socket descriptor, which can be used by the child process
to communicate with the client (see Figure 2). The return of the accept system call indicates that
a TCP connection has just been established. Subsequently, the server executes the following
code: The fork system call creates a child process which has the same code as the server process.
Both processes will continue execute at the point where the fork returns. The fork system call
returns a positive integer (the original server process ID) to the original process and 0 to the child
process. The original process closes the new socket and goes back to listen to the original socket
for new client connection request. The child process closes the original socket and uses the new
socket to communicate with the client by executing the function echod. Inside echod, the server
calls read to wait for the message from the client. The read system call blocks the return until the
server receives a message from the client. When read returns, the client message will be in the
character buffer (buf) and the return value is the length of the message in bytes. The server just
resends the message back to the client using the write system call. Note that the application does
not need to care how the message is sent. The protocols at the lower layers, such as TCP, IP and
Ethernet, will take care of it. When the client terminates the TCP connection, read returns 0 ,
which breaks the while loop, close the socket and exits. Client Program
Once the TCP connection has been established, the client calls read to read a message from the
terminal (descriptor 0 refers to standard input, i.e. the terminal). The system call read only
returns after the user (that is you) typed a message and hit CR>. After reading the message, the
client calls write to write a message to the TCP socket. The effect of this is that the message is
sent to the server across the network. The client then calls read to wait for the message echoed
back. Since TCP may not send back the message in one packet, the client keeps reading until the
number of bytes received is equal to the number of bytes sent. The last step of the echo process is
that the client writes the message back to the terminal (descriptor 1 refers to standard output,
which is again the terminal). Finally, if the user hits CTRLD>, the client will break out from the
loop and close the TCP socket, which in turn triggers the termination of TCP connection. (Note:
The termination is only triggered when the application that closes the socket is the last
application attached to the socket.) Figure 3 (below) illustrates the sequences of system calls
made in the server and client. It also shows TCP packet exchanges associated with those system
calls.
Your assignment in part III You are required to write a simple "Hello" application. In this
application, after the establishment of a TCP connection, the server sends a "Hello" message to
the client. After sending the message, the server closes the TCP connection and exits. In the
client side, the client will wait for the "Hello" message. Once it received a message, it displays
the message at the terminal. The client also exits when it finds that the TCP connection is
terminated (Note: the termination of a TCP connection will force the read system call to return
with value 0.). Figure 4 illustrates the sequence of system calls and the associated packet
exchanges. Implement the application by modifying the echo client and server programs. Figure
4 What vou needed to demonstrate in vour report for part 2 1. Show your Wireshark captured
data in part I. Identify the TCP connection packets and termination packets. 2. In part II, start 2
clients and show that the concurrent server created two child processes to service the clients. 3.
Demonstrate your programs in part III.

More Related Content

PDF
CC++ echo serverThis assignment is designed to introduce network .pdf
PPT
Introduction to sockets tcp ip protocol.ppt
PPT
Socket Programming
PPT
Sockets in unix
PPTX
Socket programming
PDF
Socket programming using C
PPT
Np unit iii
PDF
sockets SMTP Bmsce ppt information science and engineering
CC++ echo serverThis assignment is designed to introduce network .pdf
Introduction to sockets tcp ip protocol.ppt
Socket Programming
Sockets in unix
Socket programming
Socket programming using C
Np unit iii
sockets SMTP Bmsce ppt information science and engineering

Similar to CODE FOR echo_client.c A simple echo client using TCP #inc.pdf (20)

PPT
Sockets intro
PPT
Socket programming-tutorial-sk
PPT
03 sockets
PDF
socketProgramming-TCP-and UDP-overview.pdf
PDF
Networking lab
PPTX
L5-Sockets.pptx
PPT
LECTURE-17(Socket Programming) Detailed.
PPTX
Socket Programming
PPT
Sockets
PPT
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
PPT
sockets_intro.ppt
PDF
lab04.pdf
PPT
Socket Programming
PPTX
Basics of sockets
PPT
Socket System Calls
DOCX
Udp socket programming(Florian)
PPTX
Network Programming-Python-13-8-2023.pptx
PPT
PPT
Socket programming
Sockets intro
Socket programming-tutorial-sk
03 sockets
socketProgramming-TCP-and UDP-overview.pdf
Networking lab
L5-Sockets.pptx
LECTURE-17(Socket Programming) Detailed.
Socket Programming
Sockets
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
sockets_intro.ppt
lab04.pdf
Socket Programming
Basics of sockets
Socket System Calls
Udp socket programming(Florian)
Network Programming-Python-13-8-2023.pptx
Socket programming
Ad

More from secunderbadtirumalgi (20)

PDF
Code using Java Programming and use JavaFX. Show your code and outpu.pdf
PDF
Como parte de su programa Global Health Partnerships (2008-2011), Pf.pdf
PDF
Como parte de la investigaci�n de su tesis, genera una biblioteca de.pdf
PDF
Como administrador de la colecci�n de espec�menes en un museo de his.pdf
PDF
Como alcalde de Tropical Island, se enfrenta al doble mandato de pre.pdf
PDF
Commentators on the US economy feel the US economy fell into a reces.pdf
PDF
Combe Corporation has two divisions Alpha and Beta. Data from the m.pdf
PDF
Coloque los eventos para explicar c�mo un bien p�blico llega a ser d.pdf
PDF
College students often make up a substantial portion of the populati.pdf
PDF
Code using Java Programming. Show your code and output.Create a pe.pdf
PDF
code in html with div styles 9. Town of 0z Info - Microsoft Interne.pdf
PDF
Coastal Louisiana has been experiencing habitat fragmentation and ha.pdf
PDF
Cloud Solutions had the following accounts and balances as of Decemb.pdf
PDF
Climate scientists claim that CO2 has risen recently to levels that .pdf
PDF
Climate change is one of the defining challenges of our time, but to.pdf
PDF
Classify each of the following items as excludable, nonexcludable, r.pdf
PDF
Chris is a young moonshine producer in the Tennessee region of Appal.pdf
PDF
choose the right answer onlyQuestion 9 What are the four facto.pdf
PDF
Choose ONE of the scenarios below and write a problem-solving report.pdf
PDF
Charlie Raymond, 65-year-old male who was admitted to a negative p.pdf
Code using Java Programming and use JavaFX. Show your code and outpu.pdf
Como parte de su programa Global Health Partnerships (2008-2011), Pf.pdf
Como parte de la investigaci�n de su tesis, genera una biblioteca de.pdf
Como administrador de la colecci�n de espec�menes en un museo de his.pdf
Como alcalde de Tropical Island, se enfrenta al doble mandato de pre.pdf
Commentators on the US economy feel the US economy fell into a reces.pdf
Combe Corporation has two divisions Alpha and Beta. Data from the m.pdf
Coloque los eventos para explicar c�mo un bien p�blico llega a ser d.pdf
College students often make up a substantial portion of the populati.pdf
Code using Java Programming. Show your code and output.Create a pe.pdf
code in html with div styles 9. Town of 0z Info - Microsoft Interne.pdf
Coastal Louisiana has been experiencing habitat fragmentation and ha.pdf
Cloud Solutions had the following accounts and balances as of Decemb.pdf
Climate scientists claim that CO2 has risen recently to levels that .pdf
Climate change is one of the defining challenges of our time, but to.pdf
Classify each of the following items as excludable, nonexcludable, r.pdf
Chris is a young moonshine producer in the Tennessee region of Appal.pdf
choose the right answer onlyQuestion 9 What are the four facto.pdf
Choose ONE of the scenarios below and write a problem-solving report.pdf
Charlie Raymond, 65-year-old male who was admitted to a negative p.pdf
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Types and Its function , kingdom of life
PDF
Computing-Curriculum for Schools in Ghana
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
master seminar digital applications in india
PPTX
Cell Structure & Organelles in detailed.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Basic Mud Logging Guide for educational purpose
PDF
Classroom Observation Tools for Teachers
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Lesson notes of climatology university.
Microbial diseases, their pathogenesis and prophylaxis
Cell Types and Its function , kingdom of life
Computing-Curriculum for Schools in Ghana
GDM (1) (1).pptx small presentation for students
master seminar digital applications in india
Cell Structure & Organelles in detailed.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Insiders guide to clinical Medicine.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Basic Mud Logging Guide for educational purpose
Classroom Observation Tools for Teachers
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
01-Introduction-to-Information-Management.pdf
Final Presentation General Medicine 03-08-2024.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Institutional Correction lecture only . . .
Anesthesia in Laparoscopic Surgery in India
Lesson notes of climatology university.

CODE FOR echo_client.c A simple echo client using TCP #inc.pdf

  • 1. CODE FOR echo_client.c: /* A simple echo client using TCP */ #include #include #include #include #include #include #include #include #include #define SERVER_TCP_PORT 3000 /* well-known port */ #define BUFLEN 256 /* buffer length */ int main(int argc, char **argv) { int n, i, bytes_to_read; int sd, port; struct hostent *hp; struct sockaddr_in server; char *host, *bp, rbuf[BUFLEN], sbuf[BUFLEN]; switch(argc){ case 2: host = argv[1]; port = SERVER_TCP_PORT; break; case 3: host = argv[1]; port = atoi(argv[2]); break; default: fprintf(stderr, "Usage: %s host [port]n", argv[0]); exit(1); } /* Create a stream socket */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  • 2. fprintf(stderr, "Can't creat a socketn"); exit(1); } bzero((char *)&server, sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_port = htons(port); if (hp = gethostbyname(host)) bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length); else if ( inet_aton(host, (struct in_addr *) &server.sin_addr) ){ fprintf(stderr, "Can't get server's addressn"); exit(1); } /* Connecting to the server */ if (connect(sd, (struct sockaddr *)&server, sizeof(server)) == -1){ fprintf(stderr, "Can't connect n"); exit(1); } printf("Transmit: n"); while(n=read(0, sbuf, BUFLEN)){ /* get user message */ write(sd, sbuf, n); /* send it out */ printf("Receive: n"); bp = rbuf; bytes_to_read = n; while ((i = read(sd, bp, bytes_to_read)) > 0){ bp += i; bytes_to_read -=i; } write(1, rbuf, n); printf("Transmit: n"); } close(sd); return(0); } CODE FOR echo_server.c: /* A simple echo server using TCP */
  • 3. #include #include #include #include #include #include #include #include #include #define SERVER_TCP_PORT 3000 /* well-known port */ #define BUFLEN 256 /* buffer length */ int echod(int); void reaper(int); int main(int argc, char **argv) { int sd, new_sd, client_len, port; struct sockaddr_in server, client; switch(argc){ case 1: port = SERVER_TCP_PORT; break; case 2: port = atoi(argv[1]); break; default: fprintf(stderr, "Usage: %s [port]n", argv[0]); exit(1); } /* Create a stream socket */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { fprintf(stderr, "Can't creat a socketn"); exit(1); } /* Bind an address to the socket */ bzero((char *)&server, sizeof(struct sockaddr_in)); server.sin_family = AF_INET;
  • 4. server.sin_port = htons(port); server.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1){ fprintf(stderr, "Can't bind name to socketn"); exit(1); } /* queue up to 5 connect requests */ listen(sd, 5); (void) signal(SIGCHLD, reaper); while(1) { client_len = sizeof(client); new_sd = accept(sd, (struct sockaddr *)&client, &client_len); if(new_sd < 0){ fprintf(stderr, "Can't accept client n"); exit(1); } switch (fork()){ case 0: /* child */ (void) close(sd); exit(echod(new_sd)); default: /* parent */ (void) close(new_sd); break; case -1: fprintf(stderr, "fork: errorn"); } } } /* echod program */ int echod(int sd) { char *bp, buf[BUFLEN]; int n, bytes_to_read; while(n = read(sd, buf, BUFLEN)) write(sd, buf, n); close(sd);
  • 5. return(0); } /* reaper */ void reaper(int sig) { int status; while(wait3(&status, WNOHANG, (struct rusage *)0) >= 0); } This should be conducted in Linux Virtual Machines (VMs). PLEASE ADD EVERYTHING MENTIONED UNDER What you needed to demonstrate in your report for part 2 and please make sure it is correct and I will rate a thumbs up. The codes are given. Thanks Part 2: TCP Server An Internet server at application layer uses either TCP or UDP as a transport protocol. In this part, you will study servers operating on TCP. I. TCP Connection Mechanism In this section, we will use the Echo service to study the TCP connection establishment and termination mechanism. 1. Enable Wireshark in VM1 and start the capture process. 2. Start an "Echo" server in VM1: $./echo_server [port_number] 3. Start an "Echo" client on VM2: $./echo_client [server_IP_address] [port_number] From the client, send one or two messages. 4. TCP is a connection-oriented protocol - a logical connection must be established between client and server before data transfer (Figure 1). With this understanding, find out from the captured data in Wireshark the number of TCP packets exchanged to setup a TCP connection. 5. Now terminate the client process (but keep the server process running). The termination of the client also causes the termination of the TCP connection (Figure 1). Find out from the captured data in Wireshark the number of packets exchanged for the termination process. II. Concurrent Server A concurrent server is a server that can provide service to multiple clients concurrently. In this section, we will study how the concurrent feature of the TCP concurrent server is implemented. 6. Open another terminal on VM1, type The output should show that one echo_server process is running on the VM. 7. Start an "Echo" client on VM2: $/echo_client [server_IP_address] [port_number] 8. Repeat step 6 again. You will find out there are two echo server processes running. The second server process is a child process created by the original concurrent server. The child process is the one that has a TCP connection with and provides service to the client. 9. Open another terminal in VM2 and start a second client. You will now find that there are 3 server processes running in VM1, one is the original server while the other two are child processes. Each child process provides service to one client. Figure 2 below illustrates the concept. 10. You can also verify that there are two TCP connections by using netstat utility on either VM1 or VM2: $./netstat -t II. Socket Programming
  • 6. In this part, you will study the source files of echo_server.c and echo_client.c to gain some knowledge on how to write network applications based on TCP socket programming. By the end of this part, you should be able to write a simple network application. Server Program The server makes the following sequence of system calls to make itself available for TCP connection: [ begin{array}{l} text { sd = socket(AF_INET, SOCK_STREAM, 0); } text { bind(sd, (struct sockaddr *)&server, sizeof(server)); } text { listen(sd, 5); } text { new_sd = accept(sd, (struct sockaddr * *)&client, &client_len); } end{array} ] The server makes a socket system call to create a socket through which it can access the TCP/IP service. The call returns a descriptor (sd) whose function is similar to a file descriptor. The subsequent socket system calls through that socket will refer to this descriptor. The argument "SOCK_STREAM" specifies that the server will use TCP as the transport-layer service. The server then calls bind to assign IP address and port number to the just opened socket. Next, the server calls listen to place a socket in passive (server) mode and tells the TCP module to enqueue TCP connection requests with maximum queue size of 5 . Finally, the server calls accept to accept an incoming connection request. The system call accept blocks the return until a TCP connection request, which is issued by the client, arrives. When a TCP connection request arrives, the call will return with a new socket descriptor, which can be used by the child process to communicate with the client (see Figure 2). The return of the accept system call indicates that a TCP connection has just been established. Subsequently, the server executes the following code: The fork system call creates a child process which has the same code as the server process. Both processes will continue execute at the point where the fork returns. The fork system call returns a positive integer (the original server process ID) to the original process and 0 to the child process. The original process closes the new socket and goes back to listen to the original socket for new client connection request. The child process closes the original socket and uses the new socket to communicate with the client by executing the function echod. Inside echod, the server calls read to wait for the message from the client. The read system call blocks the return until the server receives a message from the client. When read returns, the client message will be in the character buffer (buf) and the return value is the length of the message in bytes. The server just resends the message back to the client using the write system call. Note that the application does not need to care how the message is sent. The protocols at the lower layers, such as TCP, IP and Ethernet, will take care of it. When the client terminates the TCP connection, read returns 0 , which breaks the while loop, close the socket and exits. Client Program Once the TCP connection has been established, the client calls read to read a message from the terminal (descriptor 0 refers to standard input, i.e. the terminal). The system call read only
  • 7. returns after the user (that is you) typed a message and hit CR>. After reading the message, the client calls write to write a message to the TCP socket. The effect of this is that the message is sent to the server across the network. The client then calls read to wait for the message echoed back. Since TCP may not send back the message in one packet, the client keeps reading until the number of bytes received is equal to the number of bytes sent. The last step of the echo process is that the client writes the message back to the terminal (descriptor 1 refers to standard output, which is again the terminal). Finally, if the user hits CTRLD>, the client will break out from the loop and close the TCP socket, which in turn triggers the termination of TCP connection. (Note: The termination is only triggered when the application that closes the socket is the last application attached to the socket.) Figure 3 (below) illustrates the sequences of system calls made in the server and client. It also shows TCP packet exchanges associated with those system calls. Your assignment in part III You are required to write a simple "Hello" application. In this application, after the establishment of a TCP connection, the server sends a "Hello" message to the client. After sending the message, the server closes the TCP connection and exits. In the client side, the client will wait for the "Hello" message. Once it received a message, it displays the message at the terminal. The client also exits when it finds that the TCP connection is terminated (Note: the termination of a TCP connection will force the read system call to return with value 0.). Figure 4 illustrates the sequence of system calls and the associated packet exchanges. Implement the application by modifying the echo client and server programs. Figure 4 What vou needed to demonstrate in vour report for part 2 1. Show your Wireshark captured data in part I. Identify the TCP connection packets and termination packets. 2. In part II, start 2 clients and show that the concurrent server created two child processes to service the clients. 3. Demonstrate your programs in part III.