0.207879576350761908546955619834978770033877841631769608075135…
stHTTPs - Single threaded HTTP server written in C++ (C++11).
As promised, here’s the link to the mess that I’m still working on. main.cpp is kind of the “unit test” file, a proof of concept, if you may.
It’s gonna be extended and rewritten significantly in the closest future!
I really need to get to know how to work with Makefiles. The concept is fairly easy, but it can be tricky sometimes with the dependencies.
A simple networking library for c++11.Here’s what I’ve been working on today. Enjoy.
Alright, the following comes from a guy who has struggled quite a lot whilst writing network apps in C++. I’m not a pro, nor am I guru or anything. These are just some questions to let you think about
1. Global Init() and Quit() funcs? Not in a namespace or class?
2. TCPServerSocket is a friend of a TCPSocket? Why not is it deriving TCPSocket? Open() and Close() could be virtual.
3. Opening socket in a constructor? What if it fails to open?
4. In Server socket you can fill socket() args by yourself without addrinfo, thus less memory usage, faster, no need to free the linked list of addrinfos
5. Why doesn’t Accept() return the pointer to the socket? I understand that you want it to be non-blocking, but there are better ways to do accept.
TCPSocket socket;
// Listen does not block
listen(socket.GetHandle(), /* whatever*/ );
// Here's the fun part
fd_set master;
FD_ZERO(&master);
FD_SET(socket.GetHandle(), &master);
// If i remember correctly, SOCK type is just an int in WIndows. Correct me if I'm wrong
while (true) {
select(socket.GetHandle() + 1, &master, NULL, NULL, 0);
if (FD_ISSET(socket.GetHandle(), &master)) {
// Accept here, accept() won't block
}
}P. S. I really wanna know what Chris would say
1. I did think of using a namespace.
2. Inheritance facilitates polymorphism, not code reuse. TCPSocket and TCPServerSocket may be similar, but they are distinct, with very different purposes.
3. This is something Chris has badgered me into doing. Normally I wouldn’t, and I might change this at some point. Also, I need to replace a lot of returns with exceptions for error checking.
4. From what I’ve read, using addrinfo is quicker and easier (and less error prone) than doing it by hand.
5. You’re right, that would be the better option. For now though, I just want it at at functional level. I had int as a return because of the archaic error system I’ve used. I’ll probably do that when I switch to exceptions.
As for that code, that’s pretty much what I did write, except I’ve allowed for an optional parameter to define X microseconds of blocking after all.
Edit: feel free to make these modifications yourself. I’d love the input. Just add your name to the copyright, and it’s fine.
Inheritance also allows you to see another level of abstraction. Like Socket() -> TCPSocket() -> TCPServerSocket. Moreover, TCPServerSocket is a TCPSocket in the end with the addition of being able to be bound, listen and accept. You’ll still need Open(), Close(), Send(), Receive() functions in both server and client sockets sockets.
Polymorphism is just another “plus” of an inheritance. I might be wrong, but for me inheritance is a way of extending base class, adding new functionality. Although polymorphism is really important.
Even taking your example you could store the list of all sockets and you need to send to everyone the message that you’re closing. Without inheritance you’d need two lists for server and client. But with inheritance you’d need only one.
I’m in a process of writing a server (single threaded HTTP server) myself, so I’ll get the repo up on github and send you the link. Still trying to figure stuff out too :)
A simple networking library for c++11.Here’s what I’ve been working on today. Enjoy.
Alright, the following comes from a guy who has struggled quite a lot whilst writing network apps in C++. I’m not a pro, nor am I guru or anything. These are just some questions to let you think about
1. Global Init() and Quit() funcs? Not in a namespace or class?
2. TCPServerSocket is a friend of a TCPSocket? Why not is it deriving TCPSocket? Open() and Close() could be virtual.
3. Opening socket in a constructor? What if it fails to open?
4. In Server socket you can fill socket() args by yourself without addrinfo, thus less memory usage, faster, no need to free the linked list of addrinfos
5. Why doesn’t Accept() return the pointer to the socket? I understand that you want it to be non-blocking, but there are better ways to do accept.
TCPSocket socket;
// Listen does not block
listen(socket.GetHandle(), /* whatever*/ );
// Here's the fun part
fd_set master;
FD_ZERO(&master);
FD_SET(socket.GetHandle(), &master);
// If i remember correctly, SOCK type is just an int in WIndows. Correct me if I'm wrong
while (true) {
select(socket.GetHandle() + 1, &master, NULL, NULL, 0);
if (FD_ISSET(socket.GetHandle(), &master)) {
// Accept here, accept() won't block
}
}
P. S. I really wanna know what Chris would say
This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science.
I’m having way too much fun writing an HTTP server in C++!
Although I’m quite new to the socket programming, it’s really fun to try new things and get them work.
At least for now I can connect to the server from the browser and return the page.
Does anyone wants to play Minecraft with me on my server (direwolf20 modpack)?
It’s a brand new server, I barely explored the map and the spawn is right next to the NPC village. And there’s a big cave under the village!
2b || !2b
See, we, programmers, can do neat stuff too!
It’s sad that it’s still improper C++ code.
Although it might be valid C++11 code with user-defined literals!