This blog post and probably the next one will be about winsock servers.
Winsock like many other ways to code servers is a hassle and are not worth the trouble unless you are seriously considering creating one.
This post will only outline my idea of how the server is going to be designed in a simple way to interact with a user trying to connect to the server.
I will show some code but as I am only at the accept phase I wont be going into detail from there on.
So, creating a web server!
Networking is done very close to how we communicate outside the digital era.
It needs to be initialized first with
#include
int main(int argc, char* argv[])
{
WSAData init;
WSAStartup(MAKEWORD(2, 2), &init);
you then need to create a mailbox with an address
struct sockaddr_in server_addr;
here you create a mailbox with the name of the address as “server_addr(ess).
server_addr.sin_family = AF_INET;
server_addr.sin_addr.S_un.S_addr = INADDR_ANY;
server_addr.sin_port = htons(8080);
memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
here you give the title of the mailbox as an internet based one, you give the addresscode to be your IP address you pick your zipcode(port) and you
So now you have a mailbox of the internet type with your address(localIP) and your zip code(port).
Now its time to create a route for your mailman. We can this a socket, this socket will be listening on the zip code you have to see whether or not you got mail(hihi).
SOCKET listener = INVALID_SOCKET;
p_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
here we declare the socket and define it as a TCP one. This means we use the TCP protocol as the route for our mailman.
After this we give our mailman a badge, a title, a bag and a paycheck to keep working until we close down shop.
In networking terms we call this bind.
bind(p_sock, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in));
here we bind the socket our address. Its like hiring a mailman for our mailbox.
listen(p_sock, SOMAXCONN);
these are his instructions, he is to see if there are any requests to send mail to us and signals us whenever that happens.
From here on out there wont be any code so I’ll just follow the metaphor for people to understand the inner workings.
So we got our mailbox with an address and a zip code, we have a mailman that goes our zip code route to see if we get any requests to send mail as well.
Now its time to wait…
we create a while loop from here on out to look for visitors. In this loop we check if our mailman ever comes back with some mail and if he does we look at the envelope to see its return address and accept it before we open the mail.
This mean we have to create a socket and an sockaddr_in and when we do get a request we use the accept method and bind the socket and sockaddr_in to the user.
while (true)
{
SOCKET ns = INVALID_SOCKET;
struct sockaddr_in remote;
int size = sizeof(struct sockaddr_in);
if (WSAWaitForMultipleEvents(1, &hacc, TRUE, 10, FALSE) == WSA_WAIT_EVENT_0)
{
std::cout << “accepterar” << std::endl;
ns = accept(listener, (sockaddr*)&remote, &size);
}
}
This is what it could look like.
When you’ve done this part you have accepted the mail and you know the return address.
You know accept connections to your server.
When this is done you now allow requests form that user so you receive anything they send in a manner you find welcoming, send the requested information and wait for them to send an acknowledgment.
This is what TCP call a three way handshake.
Now to have a good server you might want to save each user after you accept them in a list, keep their connection alive(see if they have data) and delete users whenever you haven’t gotten anything from then in a while.
When you finally close down shop you should end with this:
WSACleanup();
but you need to close down any sockets you’ve open like so:
closesocket();