Friday, November 21, 2014

My first Webserver!,*cough*...kind of.

So this week I have been working on assignment 2, making a web server using C++ and Winsock2 API. It went quite well at the start and in basically one day I got a simple server (if you can call it that) that could handle one connection. I received data from the user and I displayed some text in the web browser to the user.

 


As you can see the browser in the back and in the front is the C++ console that shows the IP address of the user. I also show how many bytes I received and how many I sent. So far so good, but as you can see in the picture there are two new visitors from same IP address, that’s something I need to fix because right now I just grab anything I got, delete it and grab it again for some reason I have yet to figure out. I also I also don't handle time outs correctly and a bunch of other things but I don't care. It's a great feeling just getting something on screen. It made my day!

 Below is part of my main loop. First I had the myServer.listenOnSocket() function inside the loop but after asking our teacher Tommi how the listen() function for Winsock2 works I moved it out from the loop. Apparently the function opens up a listener for sockets that keep listening until closed. So it doesn't need to be in the loop since it keeps listening.




I then call my accept client function and accept the client and creates a client struct that holds all the data of the client, like the IP address and such. After that I call my update client connections which I want to use for updating retrieving and sending data as well as checking for time outs from the clients but I won't show that in detail because it's not working properly yet.

When I built this I thought about going around it like a game loop because a connection to a website/ server is in my mind is always open like a game that is always running.  Today I almost managed to handle multiple connections. The only trouble I have is when one new user connects the other one gets timed out. I'm not sure how to solve that but I will have to do some more code sniffing. So this post isn’t so analytic but I can’t help it because right now I’m just playing around with all the functions in this API trying to figure out how it all works.  Maybe I will dive deeper into this in my next post.

By the way, Last week I mentioned I had some problems with templates. This one:
template<typename T>               //this is the way I tried with no success
class LinkedList<T>;

and:
bool VerifyListErase(LinkedList<T> _List, const std::string& _Message);
Well apparently my error was not how I wrote these. They are all correct. The error was in a completely other place in the code. That’s the difficult thing with templates. I will use them more carefully in the future because the errors you get don’t make sense.



No comments:

Post a Comment