Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello all, I have an question about my program implementation. (c++ ,TCP, unix/b

ID: 3833852 • Letter: H

Question

Hello all, I have an question about my program implementation. (c++ ,TCP, unix/bash)

Question Prompt:

TCP Programming with Sockets - Rudimentary HTTP Server Purpose To practice socket programming with TCP/IP, including the system calls that were learned previously, as well as bind, listen, and accept. Assignment Write a C++ program that implements a basic HTTP server

. If this is done properly, you should be able to connect to it through a web browser, but this will not be one of the criteria for grading. The program will essentially be a loop that goes on forever (until it is killed), waiting for a client to connect. When a client connects, it accepts that connection, calls fork to make a child process, and handles communication from that client in the child process. The parent process should continue to wait for more connections, accepting them and forking as necessary.

The program will accept two command line parameters: 1. the port number on which the server will be listening 2. the path to a directory that will serve as the root directory of the web server For example: % ./TCP 9001 www The requests received by the program will be of the form: GET path where the path is the path, relative to the directory specified as the second command line parameter, of the file that is being requested.

There are several rules on what can constitute a valid path, and they are as follows:

• it must begin with a “/”

• it may contain additional “/” path separators to access subdirectories

• a single “/” character refers to the directory specified in command line

• a trailing “/” in the pathname can be ignored if the path refers to a directory

• any data in the request beyond the path should be ignored

• it may not contain the substring “..”

If the path refers to a directory, then:

• if the file called “index.html” exists in that directory, send the contents of that file to the client

• if not, send a list of the files in the specified directory to the client (do not include files that start with “.”) If the path refers to a file, then the content of the file should be sent to the client.

Error Checking If the command line arguments are incomplete, or if the path to the web server’s root is invalid, print an error message to stderr and exit with an error code. If any of the system calls fail, the program should use perror to report what happened and exit with an error code. If the path in the GET request is invalid, or if a file or directory cannot be accessed, then an appropriate error message should be sent to the client to notify them. 1

Other Notes

• This is a simple TCP server. If you need a tool to test it, you can use the telnet command to connect to your server at the specified port. Type your command and hit enter, and the client should display what the server returns.

SAMPLE OUTPUT:

% telnet localhost 9001 Connected to localhost. Escape character is '^]'. GET / fileOne.html fileTwo.html Connection closed by foreign host. % telnet localhost 9001 Connected to localhost. Escape character is '^]'. GET fileOne Error: fileOne not found Connection closed by foreign host. % telnet localhost 9001 Connected to localhost. Escape character is '^]'. GET fileOne.html ... contents of file ... Connection closed by foreign host.

• Make sure that the assignment is contained in a single file called “TCP.cxx” • Make sure that your program compiles, links, and runs properly

Explanation / Answer

TCP/IP protocols

Application layer (telnet, ssh, http, ftp, etc)

•The things that we use daily.

•Main functionality: run application protocols

Transport layer (TCP, UDP)

•Allows processes (on different machines) to communicate (reliably or unreliably) with each other.

•Main functionality: reliability

Network layer (IPv4, IPv6)

•Allows machines to talk to one another

•Main functionality: routing/fragmentation/internetworking

Host to Network layer (Ethernet)

•Allows directly connected machines to talk to one another

•Main functionality: medium access, encoding

About TCP/IP

•Each layer provides services to the upper layer defined by the service interface.

– tcp service interface is defined by a set of system calls: socket, bind, listen, accept, connect, read and write.

Socket Programming

–The use of TCP and UDP

•Can also access the lower layers directly (raw socket)

•UDP: User Datagram Protocol

Interface

socket, bind, sendto, recvfrom

•Can also use the TCP interface (connect/read/write)

–Unreliable/connectionless service

•Data may be lost, duplicated and reordered.

•Client/server don’t have any long term relation.

–Can perform (unreliable) multicast.

•TCP: Transmission control protocol.

–Interface: socket, bind, listen, accept, connect, read, write, close.

–An analogy:

Socket: telephone

Bind: assign telephone number to a telephone

Listen: turn on the ringer so that you can hear the phone call

Connect: dial a phone number

Accept: answer the phone

Read/write: talking

Close: ???

C++ program which is connect to the web server

/// An application class which says hello to the identified user

The application class stores a reference to the webem server. This allows it to look after registering its own methods with the server when it is constructed, and to call the cWebem class FindValue() to extract the value of the field entered into the form.

The application class must register two methods, one to save the entered user name when the submit button is clicked, one to display the stored user name when the web page is assembled and sent to the browser.

The action methods must return the webpage which should be displayed in response to the click on the submit button.

Note that all action methods are invoked by Webem before the include methods, so that the web page always displays updated data.

Socket Client programming

In the above program, we create a client which will connect to the server and receive date and time from it. In the above piece of code :

Now execute the client as shown below.

We can see that we successfully got the date and time from server. We need to send the IP address of the server as an argument for this example to run. If you are running both server and client example on the same machine for testing purpose, use the loop back ip address as shown above.

To conclude, In this article we studied the basics of socket programming through a live example that demonstrated communication between a client and server processes capable of running on two different machines.

Test your connection to another computer

Step 1: Identify the computer's network information

To test your connection to another computer, you must use the other computer to identify its IP address and port.

Identify the IP address

Identify the other computer's IP address from its CrashPlan app:

Identify the port

Identify the port used by the other computer's CrashPlan app:

The port is the number on the line that starts with Port. For example, this example shows a CrashPlan app using port 4242:

Step 2: Run telnet

Step 3: Read the telnet response

If your command prompt shows the following Connected message, your CrashPlan app can successfully connect to the other computer:

If your command prompt shows anything else, your CrashPlan app cannot connect to the other computer. Common failure messages include:

Step 4: Troubleshoot a failed connection

If your Telnet test did not connect to the other computer, your issue is caused by the computers or network configuration, not by the CrashPlan app. Check the following common causes of this issue:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote