Question
How do I do the following in c++? Proposed way for solving Problem 1A: Copy the tcpServer example into main and use port 80. Add
How do I do the following in c++?
Proposed way for solving Problem 1A:
- Copy the tcpServer example into main and use port 80.
- Add a recv call after the accept and print the received message to see what the browser sends to the program.
- After this, respond using the send function, and the example GET response from the readme. At this point, you should already be able to open the website with your browser a single time after you started the program. Email us for help if it doesnt work.
- Next, you need to adapt the program to the requirements
- You can try to alter the content of the message, depending on the link the browser requested.
- After this, use a while loop to handle multiple requests. Make sure to not put the accept call in the while loop.
MY CODE:
#include
//create the server socket int tcp_server_socket; //variable for the socket descriptor tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0); //calling the socket function. Params: Domain of the socket (Internet in this case), type of socket stream (TCP), Protocol (default, 0 for TCP)
//define the server address struct sockaddr_in tcp_server_address; //declaring a structure for the address tcp_server_address.sin_family = AF_INET; //Structure Fields' definition: Sets the address family of the address the client would connect to tcp_server_address.sin_port = htons(PORT); //Passing the port number - converting in right network byte order tcp_server_address.sin_addr.s_addr = INADDR_ANY; //Connecting to 0.0.0.0
// binding the socket to the IP address and port bind(tcp_server_socket, (struct sockaddr *) &tcp_server_address, sizeof(tcp_server_address)); //Params: which socket, cast for server address, its size
//listen for simultaneous connections listen(tcp_server_socket, 5); //which socket, how many connections //client socket created int tcp_client_socket; char buffer[1024]; struct sockaddr_in tcp_client_address; //int clientAddrSize = sizeof(tcp_client_address); tcp_client_socket = accept(tcp_server_socket, NULL, NULL); // server socket to interact with client, structure like before - if you know - else NULL for local connection if ((tcp_client_socket = accept(tcp_server_socket, NULL, NULL)) != 0) { std::cout << "Client connected!" << std::endl; } recv(tcp_server_socket, tcp_server_message , 256 , 0);
//send data stream send(tcp_client_socket, tcp_server_message, strlen(tcp_server_message), 0); // send where, what, how much, flags (optional)
//Receive a reply from the server
//close the socket close(tcp_server_socket); return 0; }
MORE DETAILS IF NEEDED:
Implementation details
The exact implementation is up to you. The only condition is that you use Linux socket programming. This means that the functions for networking should be socket, bind, listen, accept, recv, send, and so on. You are completely free on how to deal with the rest, e. g. using c or c++ strings, determining the address, etc.
-
Do not change anything in the .github folder. Doing so will be considered attempted deception. If you change or delete it by accident, ask the teacher or one of the TAs for help.
-
Have a look at the examples examples/tcpClient3-1.c and examples/tcpServer3-1.c. Your implementation should follow these and use no additional libraries.
-
Put your code into src/main.cpp. It should run with the code in run.bash.
bash run.bash
Test the webserver using your web browser at http://localhost:8080/message. If you wonder, the password to decrypt the message is Cybernauts.
-
A typical GET request the Server will receive looks like this
GET / HTTP/1.1 Host: 127.0.0.1:5678 User-Agent: Go-http-client/1.1 Accept-Encoding: gzip
-
A typical GET response the Server should send is
HTTP/1.1 200 OK Content-Length: 217 Content-Type: text/html; charset=utf-8
Highly sensitive data encryptedMessage
or when the request url is invalid
HTTP/1.1 404 Object Not Found Content-Length: 210 Content-Type: text/html; charset=utf-8
Highly sensitive data Not found
Make sure that Content-Length is the actual length of the content. The content length is then exactly the length of the string ...