I am still pretty new to C++ and I want to start with networking. I have a tiny understanding of how this code works and was hoping someone can explain it a little better.
#include<iostream>
#include<Windows.h>
#include<stdio.h>
#pragma comment(lib,"ws2_32.lib")
usingnamespace std;
SOCKET sock; // this is the socket that we will use
SOCKET sock2[200]; // this is the sockets that will be recived from the Clients and sended to them
SOCKADDR_IN i_sock2; // this will contain information about the clients connected to the server
SOCKADDR_IN i_sock; // this will containt some informations about our socket
WSADATA Data; // this is to save our socket version
int clients = 0; // we will use it in the accepting clients
int server(int port)
{
int err;
WSAStartup(MAKEWORD(2, 2), &Data);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
Sleep(4000);
exit(0);
}
}
Depends on what you mean by the definition 'works'.
The server method initialises the Winsock DLL (v 2.2).
A socket is created. First argument, AF_INET, specifies that we're using IPv4 addresses. The second is the stream type. Looks like you're wanting to use TCP since you've specified SOCK_STREAM. The third argument doesn't look right to me. This should be a protocol arg. I'd expect this to be something like IPPROTO_TCP. Not sure what'll happen if it's 0.
Aside from that, nothing useful is happening in the method.
The SOCKADDR_IN structure that you're not currently using is where you'd specify some address details (address, port etc). You'd usually bind the address and the socket with a call to the bind() method.