Game Programming III – the fourth week, webserver part one (1)
|
Hello! This week I’ve been mostly working on the second assignment for this course, which is to make a simple web server. The web server needs to use the TCP protocol and handle HTTP/1.1 headers. The first thing you need to make a web server is to make a socket for it. A socket is, in short, an IP adress and a port number that are bound together to make an endpoint for communication over a network. I will make use of a variant of Berkerly Socket called WinSock. The code for running the server I created in the main file looks like this: #include ”HTTPOperations.hpp” int main(int arc, char* argv[]) if (server.Startup()) return 0; In this post I will focus on the startup method and the member methods it calls on: bool HTTPOperations::Startup() return false; Some external resources were included in the project. In the header file of our server class I have: #include In the source file: #include ”HTTPOperations.hpp” #include #include #include Then we need to initialize WinSock: bool HTTPOperations::Initialize() std::cout << ”Initializing winsock… ”; std::cout << ”done!” << std::endl; When I start up I also set the server’s IP adress to the same as the machine it is run on and the port number is set to 8080 as it is a standard port number for using the TCP protocol. I now need to create a listener socket. A listener socket is the server socket that listens for incomming connections. We will then bind the socket to the IP adress and port number for the server before setting it to listen. Create socket: bool HTTPOperations::CreateListenerSocket() std::cout << ”done!” << std::endl; Bind the socket: bool HTTPOperations::BindListenerSocket() std::cout << ”done!” << std::endl; Set socket to listen: bool HTTPOperations::Listen() Now I have what I need to look for incoming connections. Next we need to add code that handles the incoming connections. To be contiued… |
