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

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 socket\n"); 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 address\n"); 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 socket\n"); 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 socket\n"); 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: error\n"); } } } /* echod program */ int echod(int sd) { char *bp, buf[BUFLEN]; int n,.

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.

Recommandé

Remote Procedure Call par
Remote Procedure CallRemote Procedure Call
Remote Procedure CallNadia Nahar
587 vues9 diapositives
Socket programming par
Socket programmingSocket programming
Socket programmingAnurag Tomar
862 vues39 diapositives
Socket System Calls par
Socket System CallsSocket System Calls
Socket System CallsAvinash Varma Kalidindi
31.1K vues22 diapositives
Net Programming.ppt par
Net Programming.pptNet Programming.ppt
Net Programming.pptEloAcubaOgardo
4 vues37 diapositives
Network Prog.ppt par
Network Prog.pptNetwork Prog.ppt
Network Prog.pptEloOgardo
4 vues37 diapositives

Contenu connexe

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

Application Layer and Socket Programming par
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
3.9K vues52 diapositives
EN-04 (1).pptx par
EN-04 (1).pptxEN-04 (1).pptx
EN-04 (1).pptxTienTran779192
5 vues14 diapositives
Networking in python by Rj par
Networking in python by RjNetworking in python by Rj
Networking in python by RjShree M.L.Kakadiya MCA mahila college, Amreli
1K vues27 diapositives
03 sockets par
03 sockets03 sockets
03 socketsPavan Illa
528 vues74 diapositives
Np unit iii par
Np unit iiiNp unit iii
Np unit iiivamsitricks
1.9K vues38 diapositives
Socket Programming par
Socket ProgrammingSocket Programming
Socket ProgrammingCEC Landran
6.6K vues27 diapositives

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

Application Layer and Socket Programming par elliando dias
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
elliando dias3.9K vues
Socket Programming par CEC Landran
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran6.6K vues
Sockets par sivindia
SocketsSockets
Sockets
sivindia2.1K vues
Mail Server Project Report par Kavita Sharma
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
Kavita Sharma34.2K vues
Networking lab par Ragu Ram
Networking labNetworking lab
Networking lab
Ragu Ram11.4K vues
Here is the source code I have for the server-side- #include -stdio-h-.pdf par trishulinoverseas1
Here is the source code I have for the server-side- #include -stdio-h-.pdfHere is the source code I have for the server-side- #include -stdio-h-.pdf
Here is the source code I have for the server-side- #include -stdio-h-.pdf
692015 programming assignment 1 building a multi­threaded w par smile790243
692015 programming assignment 1 building a multi­threaded w692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w
smile790243159 vues
Socket programming using C par Ajit Nayak
Socket programming using CSocket programming using C
Socket programming using C
Ajit Nayak1.7K vues
Networking & Socket Programming In Java par Ankur Agrawal
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
Ankur Agrawal3.7K vues

Plus de secunderbadtirumalgi

Codebaseimport numpy as npimport mathimport cvxpy as cpimpo.pdf par
Codebaseimport numpy as npimport mathimport cvxpy as cpimpo.pdfCodebaseimport numpy as npimport mathimport cvxpy as cpimpo.pdf
Codebaseimport numpy as npimport mathimport cvxpy as cpimpo.pdfsecunderbadtirumalgi
15 vues4 diapositives
Como preliminar a la solicitud de estimaciones presupuestarias de ve.pdf par
Como preliminar a la solicitud de estimaciones presupuestarias de ve.pdfComo preliminar a la solicitud de estimaciones presupuestarias de ve.pdf
Como preliminar a la solicitud de estimaciones presupuestarias de ve.pdfsecunderbadtirumalgi
2 vues1 diapositive
Como director general reci�n nombrado de Minnesota Micromotors, Inc..pdf par
Como director general reci�n nombrado de Minnesota Micromotors, Inc..pdfComo director general reci�n nombrado de Minnesota Micromotors, Inc..pdf
Como director general reci�n nombrado de Minnesota Micromotors, Inc..pdfsecunderbadtirumalgi
3 vues1 diapositive
Como gerente de operaciones de Holz Furniture, debe tomar la decisi�.pdf par
Como gerente de operaciones de Holz Furniture, debe tomar la decisi�.pdfComo gerente de operaciones de Holz Furniture, debe tomar la decisi�.pdf
Como gerente de operaciones de Holz Furniture, debe tomar la decisi�.pdfsecunderbadtirumalgi
30 vues1 diapositive
Como l�der de una empresa, desea que su empresa alcance un mayor niv.pdf par
Como l�der de una empresa, desea que su empresa alcance un mayor niv.pdfComo l�der de una empresa, desea que su empresa alcance un mayor niv.pdf
Como l�der de una empresa, desea que su empresa alcance un mayor niv.pdfsecunderbadtirumalgi
2 vues1 diapositive
Como parte de la investigaci�n de su tesis, genera una biblioteca de.pdf par
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
2 vues1 diapositive

Plus de secunderbadtirumalgi(20)

Codebaseimport numpy as npimport mathimport cvxpy as cpimpo.pdf par secunderbadtirumalgi
Codebaseimport numpy as npimport mathimport cvxpy as cpimpo.pdfCodebaseimport numpy as npimport mathimport cvxpy as cpimpo.pdf
Codebaseimport numpy as npimport mathimport cvxpy as cpimpo.pdf
Como preliminar a la solicitud de estimaciones presupuestarias de ve.pdf par secunderbadtirumalgi
Como preliminar a la solicitud de estimaciones presupuestarias de ve.pdfComo preliminar a la solicitud de estimaciones presupuestarias de ve.pdf
Como preliminar a la solicitud de estimaciones presupuestarias de ve.pdf
Como director general reci�n nombrado de Minnesota Micromotors, Inc..pdf par secunderbadtirumalgi
Como director general reci�n nombrado de Minnesota Micromotors, Inc..pdfComo director general reci�n nombrado de Minnesota Micromotors, Inc..pdf
Como director general reci�n nombrado de Minnesota Micromotors, Inc..pdf
Como gerente de operaciones de Holz Furniture, debe tomar la decisi�.pdf par secunderbadtirumalgi
Como gerente de operaciones de Holz Furniture, debe tomar la decisi�.pdfComo gerente de operaciones de Holz Furniture, debe tomar la decisi�.pdf
Como gerente de operaciones de Holz Furniture, debe tomar la decisi�.pdf
Como l�der de una empresa, desea que su empresa alcance un mayor niv.pdf par secunderbadtirumalgi
Como l�der de una empresa, desea que su empresa alcance un mayor niv.pdfComo l�der de una empresa, desea que su empresa alcance un mayor niv.pdf
Como l�der de una empresa, desea que su empresa alcance un mayor niv.pdf
Como parte de la investigaci�n de su tesis, genera una biblioteca de.pdf par secunderbadtirumalgi
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 director ejecutivo de la empresa, su primera orden del d�a es d.pdf par secunderbadtirumalgi
Como director ejecutivo de la empresa, su primera orden del d�a es d.pdfComo director ejecutivo de la empresa, su primera orden del d�a es d.pdf
Como director ejecutivo de la empresa, su primera orden del d�a es d.pdf
Como auditor, ha descubierto los siguientes problemas con el sistema.pdf par secunderbadtirumalgi
Como auditor, ha descubierto los siguientes problemas con el sistema.pdfComo auditor, ha descubierto los siguientes problemas con el sistema.pdf
Como auditor, ha descubierto los siguientes problemas con el sistema.pdf
Como analista de Delta Airlines, se le pide que ayude al personal de.pdf par secunderbadtirumalgi
Como analista de Delta Airlines, se le pide que ayude al personal de.pdfComo analista de Delta Airlines, se le pide que ayude al personal de.pdf
Como analista de Delta Airlines, se le pide que ayude al personal de.pdf
Como alcalde de Tropical Island, se enfrenta al doble mandato de pre.pdf par secunderbadtirumalgi
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 par secunderbadtirumalgi
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
Comment on Target�s efforts to predict shopper behavior. Do you have.pdf par secunderbadtirumalgi
Comment on Target�s efforts to predict shopper behavior. Do you have.pdfComment on Target�s efforts to predict shopper behavior. Do you have.pdf
Comment on Target�s efforts to predict shopper behavior. Do you have.pdf
Coloque los eventos para explicar c�mo un bien p�blico llega a ser d.pdf par secunderbadtirumalgi
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
collective bargaining, as practised in contemporary labour relations.pdf par secunderbadtirumalgi
collective bargaining, as practised in contemporary labour relations.pdfcollective bargaining, as practised in contemporary labour relations.pdf
collective bargaining, as practised in contemporary labour relations.pdf
Colbert opera un servicio de catering en el m�todo de acumulaci�n. E.pdf par secunderbadtirumalgi
Colbert opera un servicio de catering en el m�todo de acumulaci�n. E.pdfColbert opera un servicio de catering en el m�todo de acumulaci�n. E.pdf
Colbert opera un servicio de catering en el m�todo de acumulaci�n. E.pdf
Colin est� tratando de decidir si debe hacer su contribuci�n a la cu.pdf par secunderbadtirumalgi
Colin est� tratando de decidir si debe hacer su contribuci�n a la cu.pdfColin est� tratando de decidir si debe hacer su contribuci�n a la cu.pdf
Colin est� tratando de decidir si debe hacer su contribuci�n a la cu.pdf
Coeficiente de variaci�nFabricaci�n de Metales ha aislado cuatro a.pdf par secunderbadtirumalgi
Coeficiente de variaci�nFabricaci�n de Metales ha aislado cuatro a.pdfCoeficiente de variaci�nFabricaci�n de Metales ha aislado cuatro a.pdf
Coeficiente de variaci�nFabricaci�n de Metales ha aislado cuatro a.pdf
CODE THIS IN C# AND USE VISUAL STUDIOAdditional logic required by .pdf par secunderbadtirumalgi
CODE THIS IN C# AND USE VISUAL STUDIOAdditional logic required by .pdfCODE THIS IN C# AND USE VISUAL STUDIOAdditional logic required by .pdf
CODE THIS IN C# AND USE VISUAL STUDIOAdditional logic required by .pdf
Code using Java Programming. Show your code and output.Create a pe.pdf par secunderbadtirumalgi
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

Dernier

GCSE Music par
GCSE MusicGCSE Music
GCSE MusicWestHatch
45 vues50 diapositives
Collective Bargaining and Understanding a Teacher Contract(16793704.1).pptx par
Collective Bargaining and Understanding a Teacher Contract(16793704.1).pptxCollective Bargaining and Understanding a Teacher Contract(16793704.1).pptx
Collective Bargaining and Understanding a Teacher Contract(16793704.1).pptxCenter for Integrated Training & Education
95 vues57 diapositives
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptx par
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxGopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxDebapriya Chakraborty
695 vues81 diapositives
Classification of crude drugs.pptx par
Classification of crude drugs.pptxClassification of crude drugs.pptx
Classification of crude drugs.pptxGayatriPatra14
101 vues13 diapositives
REPRESENTATION - GAUNTLET.pptx par
REPRESENTATION - GAUNTLET.pptxREPRESENTATION - GAUNTLET.pptx
REPRESENTATION - GAUNTLET.pptxiammrhaywood
138 vues26 diapositives
unidad 3.pdf par
unidad 3.pdfunidad 3.pdf
unidad 3.pdfMarcosRodriguezUcedo
117 vues38 diapositives

Dernier(20)

Classification of crude drugs.pptx par GayatriPatra14
Classification of crude drugs.pptxClassification of crude drugs.pptx
Classification of crude drugs.pptx
GayatriPatra14101 vues
REPRESENTATION - GAUNTLET.pptx par iammrhaywood
REPRESENTATION - GAUNTLET.pptxREPRESENTATION - GAUNTLET.pptx
REPRESENTATION - GAUNTLET.pptx
iammrhaywood138 vues
The basics - information, data, technology and systems.pdf par JonathanCovena1
The basics - information, data, technology and systems.pdfThe basics - information, data, technology and systems.pdf
The basics - information, data, technology and systems.pdf
JonathanCovena1146 vues
11.28.23 Social Capital and Social Exclusion.pptx par mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239312 vues
Monthly Information Session for MV Asterix (November) par Esquimalt MFRC
Monthly Information Session for MV Asterix (November)Monthly Information Session for MV Asterix (November)
Monthly Information Session for MV Asterix (November)
Esquimalt MFRC72 vues
Pharmaceutical Inorganic chemistry UNIT-V Radiopharmaceutical.pptx par Ms. Pooja Bhandare
Pharmaceutical Inorganic chemistry UNIT-V Radiopharmaceutical.pptxPharmaceutical Inorganic chemistry UNIT-V Radiopharmaceutical.pptx
Pharmaceutical Inorganic chemistry UNIT-V Radiopharmaceutical.pptx
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively par PECB
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
PECB 623 vues
CUNY IT Picciano.pptx par apicciano
CUNY IT Picciano.pptxCUNY IT Picciano.pptx
CUNY IT Picciano.pptx
apicciano54 vues
Retail Store Scavenger Hunt.pptx par jmurphy154
Retail Store Scavenger Hunt.pptxRetail Store Scavenger Hunt.pptx
Retail Store Scavenger Hunt.pptx
jmurphy15446 vues

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.