SlideShare a Scribd company logo
1 of 7
Download to read offline
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

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

Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docxProject Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docxkacie8xcheco
 
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjjCN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjjPRADEEPERUKULLA2
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
 
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docxhanneloremccaffery
 
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docxCSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docxmydrynan
 
CC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdfCC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdfsecunderbadtirumalgi
 
Socket Programming
Socket ProgrammingSocket Programming
Socket ProgrammingCEC Landran
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project ReportKavita Sharma
 
Networking lab
Networking labNetworking lab
Networking labRagu Ram
 
13_TCP_Attack.pptx
13_TCP_Attack.pptx13_TCP_Attack.pptx
13_TCP_Attack.pptxAlmaOraevi
 
Socket Programming
Socket ProgrammingSocket Programming
Socket ProgrammingMostak Ahmed
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sksureshkarthick37
 

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

Os 2
Os 2Os 2
Os 2
 
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docxProject Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
 
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjjCN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
 
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docxCSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
 
EN-04 (1).pptx
EN-04 (1).pptxEN-04 (1).pptx
EN-04 (1).pptx
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
CC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdfCC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdf
 
03 sockets
03 sockets03 sockets
03 sockets
 
Np unit iii
Np unit iiiNp unit iii
Np unit iii
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Sockets
SocketsSockets
Sockets
 
Lecture9
Lecture9Lecture9
Lecture9
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
 
Networking lab
Networking labNetworking lab
Networking lab
 
13_TCP_Attack.pptx
13_TCP_Attack.pptx13_TCP_Attack.pptx
13_TCP_Attack.pptx
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
Sockets
Sockets Sockets
Sockets
 

More from secunderbadtirumalgi

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

More from secunderbadtirumalgi (20)

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

Recently uploaded

Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroomSamsung Business USA
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...Nguyen Thanh Tu Collection
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptxmary850239
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 

Recently uploaded (20)

Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
 
Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 

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.