SlideShare a Scribd company logo
1 of 9
Download to read offline
C/C++ echo server
This assignment is designed to introduce network programing and the socket API. You will build
a simple ECHO server, that is a program that accepts incoming connection requests and then
echoes back any string that is sent.
Required Behavior
1. Your submission must be written in C or C++ and must compile and run on the schools Linux
server.
2. Your code should be clean and easy to read with reasonable comments and variable names.
3. You must provide a makefile that compiles your program. It should compile without errors or
warnings to a binary named echo_s.
4. Your program should take one optional command line argument, -v, that invokes verbose
mode causing the program to print diagnostic messages as it operates.
5. When run the program should:
* Create a socket and prepare it to listen for incoming TCP connections on the port of your
choice.
* Print Using port: X to tell the user what port your program is listening on.
* Begin waiting for a new connection
* When a new connection is established:
- Read a block of data from the connection note the length of the data received (blocks can
be up to 1024 bytes long)
- Check the block to see if it starts with the letters CLOSE or QUIT.
+If CLOSE, close the connection and start waiting for another connection (return to
waiting for a connection).
+If QUIT, close the connection and the listening socket and exit your program.
- Send the block back with a write() call.
- Return to waiting for the next block of data from the client.
6. If run with the v flag you should print a diagnostic message at least
* When the socket is opened
* When the socket is bound.
* Before it blocks waiting for a new connection.
* When a new connection is accepted.
* Before it blocks waiting for new data.
* When you get a block of data.
* If the data included the CLOSE command,
* If the data included the QUIT command.
* Any other time you think it would be useful.
Suggested Approach
There is some skeleton code at the end
The skeleton includes a makefile and everything you need to get started.
Once you have the skeleton setup and running you should start filling in the code. You may of
course do this however you like, but I would recommend the following:
* Fill in the sections of main() required to process command line arguments, create, bind,
establish a listening queue and then close a stream type socket.
- If you run the program without the v flag it should print what port was selected, then exit.
- If you run the program with the v you should see each step of the socket creation process.
* Add code to accept new connection requests and as requests are received call the
processConnection() At this point you can test by connect to the program with telnet.
* At this point you should have the functioning skeleton of a TCP server application. Up to this
point the structure of all server applications are almost identical, regardless of the application
protocol they are going to implement.
* Modify the processConnection() function so that it reads a block of input from the network and
checks to see if it contains CLOSE or QUIT.
- If it contains CLOSE, close the connection file descriptor and exit processConnection()
function returning 0.
- If it contains QUIT, close the connection file descriptor and exit processConnection()
returning 1.
- If it doesnt contain either command, write() the block back to the network. Note: you
should only write the number of bytes received.
Testing with netcat
The nc (or netcat) utility is used for just about anything under the sun involving TCP, UDP, or
UNIX-domain sockets. It can open TCP connections, send UDP packets, listen on arbitrary TCP
and UDP ports, do port scanning, and deal with both IPv4 and IPv6. When run from the
command line it makes a connection to a remote host, then reads input from the keyboard. Each
time the user presses return it sends the data on the current line to the remote system. At the same
time it accepts data from the remote system, displaying each line as it arrives. Hence you can use
it to connect to almost any remote TCP application.
Example
I have provided an example of my solution to the assignment which you can use to confirm your
program works as I expect.
Terminal window running server
Terminal window running telnet client
echo.h:
// ********************************************************
// * A common set of system include files needed for socket() programming
// ********************************************************
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// ********************************************************
// * These don't really provide any improved functionality,
// * but IMHO they make the code more readable.
// *
// * There is a much better logging facility included
// * in the BOOST library package. I recommend that for
// * real world applications.
// ********************************************************
extern bool VERBOSE;
#define DEBUG if (VERBOSE) { std::cout
#define FATAL if (true) { std::cout
#define ENDL " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; }
echo.cc:
//
*****************************************************************************
*********
// * Echo Strings (echo_s.cc)
// * -- Accepts TCP connections and then echos back each string sent.
//
*****************************************************************************
*********
#include "echo_s.h"
bool VERBOSE;
//
*****************************************************************************
*********
// * processConnection()
// * - Handles reading the line from the network and sending it back to the client.
// * - Returns true if the client sends "QUIT" command, false if the client sends "CLOSE".
//
*****************************************************************************
*********
int processConnection(int sockFd) {
bool quitProgram = true;
bool keepGoing = true;
while (keepGoing) {
//
// Call read() call to get a buffer/line from the client.
// Hint - don't forget to zero out the buffer each time you use it.
//
//
// Check for one of the commands
//
//
// Call write() to send line back to the client.
//
}
return quitProgram;
}
//
*****************************************************************************
*********
// * main()
// * - Sets up the sockets and accepts new connection until processConnection() returns 1
//
*****************************************************************************
*********
int main (int argc, char *argv[]) {
// ********************************************************************
// * Process the command line arguments
// ********************************************************************
int opt = 0;
while ((opt = getopt(argc,argv,"v")) != -1) {
switch (opt) {
case 'v':
VERBOSE = true;
break;
case ':':
case '?':
default:
std::cout << "useage: " << argv[0] << " -v" << std::endl;
exit(-1);
}
}
// *******************************************************************
// * Creating the inital socket is the same as in a client.
// ********************************************************************
int listenFd = -1;
// Call socket() to create the socket you will use for lisening.
DEBUG << "Calling Socket() assigned file descriptor " << listenFd << ENDL;
// ********************************************************************
// * The bind() and calls take a structure that specifies the
// * address to be used for the connection. On the cient it contains
// * the address of the server to connect to. On the server it specifies
// * which IP address and port to lisen for connections.
// ********************************************************************
struct sockaddr_in servaddr;
srand(time(NULL));
int port = (rand() % 10000) + 1024;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = PF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
// ********************************************************************
// * Binding configures the socket with the parameters we have
// * specified in the servaddr structure. This step is implicit in
// * the connect() call, but must be explicitly listed for servers.
// ********************************************************************
DEBUG << "Calling bind(" << listenFd << "," << &servaddr << "," << sizeof(servaddr) << ")"
<< ENDL;
bool bindSuccesful = false;
while (!bindSuccesful) {
// You may have to call bind multiple times if another process is already using the port
// your program selects.
}
std::cout << "Using port: " << port << std::endl;
// ********************************************************************
// * Setting the socket to the listening state is the second step
// * needed to being accepting connections. This creates a queue for
// * connections and starts the kernel listening for connections.
// ********************************************************************
int listenQueueLength = 1;
DEBUG << "Calling listen(" << listenFd << "," << listenQueueLength << ")" << ENDL;
// ********************************************************************
// * The accept call will sleep, waiting for a connection. When
// * a connection request comes in the accept() call creates a NEW
// * socket with a new fd that will be used for the communication.
// ********************************************************************
bool quitProgram = false;
while (!quitProgram) {
int connFd = 0;
DEBUG << "Calling accept(" << listenFd << "NULL,NULL)." << ENDL;
// The accept() call checks the listening queue for connection requests.
// If a client has already tried to connect accept() will complete the
// connection and return a file descriptor that you can read from and
// write to. If there is no connection waiting accept() will block and
// not return until there is a connection.
DEBUG << "We have recieved a connection on " << connFd << ENDL;
quitProgram = processConnection(connFd);
close(connFd);
}
close(listenFd);
}
Terminal window running server
Terminal window running telnet client$> nc isengard.adjhkf.edu 8518
Trying 138.67.209.20...
Connected to isengard.adjhkf.edu.
Escape character is '^]'.
Hello World
Hello World
123456789
123456789
CLOSE
$> nc isengard.adjhkf.edu 8518
Trying 138.67.209.20...
Connected to isengard.adjhkf.edu.
Escape character is '^]'.
Hello Again
Hello Again
QUIT
$>
$> ~rsmith/pub/bin/echo_s -v
Calling Socket() assigned file descriptor 3 (echo_s.cc:90)
Calling bind(3,0x7,16) (echo_s.cc:113)
Using port 8518
Calling listen(3,1) (echo_s.cc:129)
Calling accept(3NULL,NULL). (echo_s.cc:142)
We have a connection on 4 (echo_s.cc:149)
Calling read(4,0x1363ea0,1024) (echo_s.cc:17)
Received 13 bytes, containing the string "Hello World". (echo_s.cc:26
Calling write(4,0x1363ea0,13) (echo_s.cc:41)
Wrote 13 back to client. (echo_s.cc:46)
Calling read(4,0x1363ea0,1024) (echo_s.cc:17)
Received 11 bytes, containing the string "123456789". (echo_s.cc:26)
Calling write(4,0x1363ea0,11) (echo_s.cc:41)
Wrote 11 back to client. (echo_s.cc:46)
Calling read(4,0x1363ea0,1024) (echo_s.cc:17)
...
...
...
...
...

More Related Content

Similar to CC++ echo serverThis assignment is designed to introduce network .pdf

26.1.7 lab snort and firewall rules
26.1.7 lab   snort and firewall rules26.1.7 lab   snort and firewall rules
26.1.7 lab snort and firewall rulesFreddy Buenaño
 
Components of computer systems often have dependencies--other co.pdf
Components of computer systems often have dependencies--other co.pdfComponents of computer systems often have dependencies--other co.pdf
Components of computer systems often have dependencies--other co.pdfalamodeindia1
 
please code in java- here is a portion of the code if possible please.pdf
please code in java- here is a portion of the code if possible please.pdfplease code in java- here is a portion of the code if possible please.pdf
please code in java- here is a portion of the code if possible please.pdfa1salesagency
 
Developing Real-Time Data Pipelines with Apache Kafka
Developing Real-Time Data Pipelines with Apache KafkaDeveloping Real-Time Data Pipelines with Apache Kafka
Developing Real-Time Data Pipelines with Apache KafkaJoe Stein
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project ReportKavita Sharma
 
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jbCN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jbPRADEEPERUKULLA2
 
Figure 3 TCP Session Hijacking Attack victims to execute the mali.pdf
Figure 3 TCP Session Hijacking Attack victims to execute the mali.pdfFigure 3 TCP Session Hijacking Attack victims to execute the mali.pdf
Figure 3 TCP Session Hijacking Attack victims to execute the mali.pdforderfabfirki
 
AssignmentACSC_424_midterm_fall_2012.pdf1 ACSC42.docx
AssignmentACSC_424_midterm_fall_2012.pdf1  ACSC42.docxAssignmentACSC_424_midterm_fall_2012.pdf1  ACSC42.docx
AssignmentACSC_424_midterm_fall_2012.pdf1 ACSC42.docxssuser562afc1
 
Gas leakage detection system
Gas leakage detection systemGas leakage detection system
Gas leakage detection systemAashiq Ahamed N
 
BUMP implementation in Java.docxThe project is to implemen.docx
BUMP implementation in Java.docxThe project is to implemen.docxBUMP implementation in Java.docxThe project is to implemen.docx
BUMP implementation in Java.docxThe project is to implemen.docxhartrobert670
 
forwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxforwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxbudbarber38650
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using CAjit Nayak
 

Similar to CC++ echo serverThis assignment is designed to introduce network .pdf (20)

26.1.7 lab snort and firewall rules
26.1.7 lab   snort and firewall rules26.1.7 lab   snort and firewall rules
26.1.7 lab snort and firewall rules
 
Components of computer systems often have dependencies--other co.pdf
Components of computer systems often have dependencies--other co.pdfComponents of computer systems often have dependencies--other co.pdf
Components of computer systems often have dependencies--other co.pdf
 
NP-lab-manual.docx
NP-lab-manual.docxNP-lab-manual.docx
NP-lab-manual.docx
 
please code in java- here is a portion of the code if possible please.pdf
please code in java- here is a portion of the code if possible please.pdfplease code in java- here is a portion of the code if possible please.pdf
please code in java- here is a portion of the code if possible please.pdf
 
NP-lab-manual (1).pdf
NP-lab-manual (1).pdfNP-lab-manual (1).pdf
NP-lab-manual (1).pdf
 
NP-lab-manual.pdf
NP-lab-manual.pdfNP-lab-manual.pdf
NP-lab-manual.pdf
 
Developing Real-Time Data Pipelines with Apache Kafka
Developing Real-Time Data Pipelines with Apache KafkaDeveloping Real-Time Data Pipelines with Apache Kafka
Developing Real-Time Data Pipelines with Apache Kafka
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
 
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jbCN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
 
Figure 3 TCP Session Hijacking Attack victims to execute the mali.pdf
Figure 3 TCP Session Hijacking Attack victims to execute the mali.pdfFigure 3 TCP Session Hijacking Attack victims to execute the mali.pdf
Figure 3 TCP Session Hijacking Attack victims to execute the mali.pdf
 
AssignmentACSC_424_midterm_fall_2012.pdf1 ACSC42.docx
AssignmentACSC_424_midterm_fall_2012.pdf1  ACSC42.docxAssignmentACSC_424_midterm_fall_2012.pdf1  ACSC42.docx
AssignmentACSC_424_midterm_fall_2012.pdf1 ACSC42.docx
 
Gas leakage detection system
Gas leakage detection systemGas leakage detection system
Gas leakage detection system
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
Socket programming
Socket programming Socket programming
Socket programming
 
Multi user chat system using java
Multi user chat system using javaMulti user chat system using java
Multi user chat system using java
 
BUMP implementation in Java.docxThe project is to implemen.docx
BUMP implementation in Java.docxThe project is to implemen.docxBUMP implementation in Java.docxThe project is to implemen.docx
BUMP implementation in Java.docxThe project is to implemen.docx
 
forwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxforwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docx
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 

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

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 

Recently uploaded (20)

OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 

CC++ echo serverThis assignment is designed to introduce network .pdf

  • 1. C/C++ echo server This assignment is designed to introduce network programing and the socket API. You will build a simple ECHO server, that is a program that accepts incoming connection requests and then echoes back any string that is sent. Required Behavior 1. Your submission must be written in C or C++ and must compile and run on the schools Linux server. 2. Your code should be clean and easy to read with reasonable comments and variable names. 3. You must provide a makefile that compiles your program. It should compile without errors or warnings to a binary named echo_s. 4. Your program should take one optional command line argument, -v, that invokes verbose mode causing the program to print diagnostic messages as it operates. 5. When run the program should: * Create a socket and prepare it to listen for incoming TCP connections on the port of your choice. * Print Using port: X to tell the user what port your program is listening on. * Begin waiting for a new connection * When a new connection is established: - Read a block of data from the connection note the length of the data received (blocks can be up to 1024 bytes long) - Check the block to see if it starts with the letters CLOSE or QUIT. +If CLOSE, close the connection and start waiting for another connection (return to waiting for a connection). +If QUIT, close the connection and the listening socket and exit your program. - Send the block back with a write() call. - Return to waiting for the next block of data from the client. 6. If run with the v flag you should print a diagnostic message at least * When the socket is opened * When the socket is bound. * Before it blocks waiting for a new connection. * When a new connection is accepted. * Before it blocks waiting for new data. * When you get a block of data. * If the data included the CLOSE command, * If the data included the QUIT command.
  • 2. * Any other time you think it would be useful. Suggested Approach There is some skeleton code at the end The skeleton includes a makefile and everything you need to get started. Once you have the skeleton setup and running you should start filling in the code. You may of course do this however you like, but I would recommend the following: * Fill in the sections of main() required to process command line arguments, create, bind, establish a listening queue and then close a stream type socket. - If you run the program without the v flag it should print what port was selected, then exit. - If you run the program with the v you should see each step of the socket creation process. * Add code to accept new connection requests and as requests are received call the processConnection() At this point you can test by connect to the program with telnet. * At this point you should have the functioning skeleton of a TCP server application. Up to this point the structure of all server applications are almost identical, regardless of the application protocol they are going to implement. * Modify the processConnection() function so that it reads a block of input from the network and checks to see if it contains CLOSE or QUIT. - If it contains CLOSE, close the connection file descriptor and exit processConnection() function returning 0. - If it contains QUIT, close the connection file descriptor and exit processConnection() returning 1. - If it doesnt contain either command, write() the block back to the network. Note: you should only write the number of bytes received. Testing with netcat The nc (or netcat) utility is used for just about anything under the sun involving TCP, UDP, or UNIX-domain sockets. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6. When run from the command line it makes a connection to a remote host, then reads input from the keyboard. Each time the user presses return it sends the data on the current line to the remote system. At the same time it accepts data from the remote system, displaying each line as it arrives. Hence you can use it to connect to almost any remote TCP application. Example I have provided an example of my solution to the assignment which you can use to confirm your program works as I expect. Terminal window running server Terminal window running telnet client
  • 3. echo.h: // ******************************************************** // * A common set of system include files needed for socket() programming // ******************************************************** #include #include #include #include #include #include #include #include #include #include // ******************************************************** // * These don't really provide any improved functionality, // * but IMHO they make the code more readable. // * // * There is a much better logging facility included // * in the BOOST library package. I recommend that for // * real world applications. // ******************************************************** extern bool VERBOSE; #define DEBUG if (VERBOSE) { std::cout #define FATAL if (true) { std::cout #define ENDL " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; } echo.cc: //
  • 4. ***************************************************************************** ********* // * Echo Strings (echo_s.cc) // * -- Accepts TCP connections and then echos back each string sent. // ***************************************************************************** ********* #include "echo_s.h" bool VERBOSE; // ***************************************************************************** ********* // * processConnection() // * - Handles reading the line from the network and sending it back to the client. // * - Returns true if the client sends "QUIT" command, false if the client sends "CLOSE". // ***************************************************************************** ********* int processConnection(int sockFd) { bool quitProgram = true; bool keepGoing = true; while (keepGoing) { // // Call read() call to get a buffer/line from the client. // Hint - don't forget to zero out the buffer each time you use it. // // // Check for one of the commands // //
  • 5. // Call write() to send line back to the client. // } return quitProgram; } // ***************************************************************************** ********* // * main() // * - Sets up the sockets and accepts new connection until processConnection() returns 1 // ***************************************************************************** ********* int main (int argc, char *argv[]) { // ******************************************************************** // * Process the command line arguments // ******************************************************************** int opt = 0; while ((opt = getopt(argc,argv,"v")) != -1) { switch (opt) { case 'v': VERBOSE = true; break; case ':': case '?': default: std::cout << "useage: " << argv[0] << " -v" << std::endl; exit(-1); } }
  • 6. // ******************************************************************* // * Creating the inital socket is the same as in a client. // ******************************************************************** int listenFd = -1; // Call socket() to create the socket you will use for lisening. DEBUG << "Calling Socket() assigned file descriptor " << listenFd << ENDL; // ******************************************************************** // * The bind() and calls take a structure that specifies the // * address to be used for the connection. On the cient it contains // * the address of the server to connect to. On the server it specifies // * which IP address and port to lisen for connections. // ******************************************************************** struct sockaddr_in servaddr; srand(time(NULL)); int port = (rand() % 10000) + 1024; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = PF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(port); // ******************************************************************** // * Binding configures the socket with the parameters we have // * specified in the servaddr structure. This step is implicit in // * the connect() call, but must be explicitly listed for servers. // ******************************************************************** DEBUG << "Calling bind(" << listenFd << "," << &servaddr << "," << sizeof(servaddr) << ")" << ENDL; bool bindSuccesful = false; while (!bindSuccesful) { // You may have to call bind multiple times if another process is already using the port // your program selects. } std::cout << "Using port: " << port << std::endl;
  • 7. // ******************************************************************** // * Setting the socket to the listening state is the second step // * needed to being accepting connections. This creates a queue for // * connections and starts the kernel listening for connections. // ******************************************************************** int listenQueueLength = 1; DEBUG << "Calling listen(" << listenFd << "," << listenQueueLength << ")" << ENDL; // ******************************************************************** // * The accept call will sleep, waiting for a connection. When // * a connection request comes in the accept() call creates a NEW // * socket with a new fd that will be used for the communication. // ******************************************************************** bool quitProgram = false; while (!quitProgram) { int connFd = 0; DEBUG << "Calling accept(" << listenFd << "NULL,NULL)." << ENDL; // The accept() call checks the listening queue for connection requests. // If a client has already tried to connect accept() will complete the // connection and return a file descriptor that you can read from and // write to. If there is no connection waiting accept() will block and // not return until there is a connection. DEBUG << "We have recieved a connection on " << connFd << ENDL; quitProgram = processConnection(connFd); close(connFd); } close(listenFd); } Terminal window running server
  • 8. Terminal window running telnet client$> nc isengard.adjhkf.edu 8518 Trying 138.67.209.20... Connected to isengard.adjhkf.edu. Escape character is '^]'. Hello World Hello World 123456789 123456789 CLOSE $> nc isengard.adjhkf.edu 8518 Trying 138.67.209.20... Connected to isengard.adjhkf.edu. Escape character is '^]'. Hello Again Hello Again QUIT $> $> ~rsmith/pub/bin/echo_s -v Calling Socket() assigned file descriptor 3 (echo_s.cc:90) Calling bind(3,0x7,16) (echo_s.cc:113) Using port 8518 Calling listen(3,1) (echo_s.cc:129) Calling accept(3NULL,NULL). (echo_s.cc:142) We have a connection on 4 (echo_s.cc:149) Calling read(4,0x1363ea0,1024) (echo_s.cc:17) Received 13 bytes, containing the string "Hello World". (echo_s.cc:26 Calling write(4,0x1363ea0,13) (echo_s.cc:41) Wrote 13 back to client. (echo_s.cc:46) Calling read(4,0x1363ea0,1024) (echo_s.cc:17) Received 11 bytes, containing the string "123456789". (echo_s.cc:26)
  • 9. Calling write(4,0x1363ea0,11) (echo_s.cc:41) Wrote 11 back to client. (echo_s.cc:46) Calling read(4,0x1363ea0,1024) (echo_s.cc:17) ... ... ... ... ...