Socket help

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  
#include<iostream>
#include<Windows.h>
#include<stdio.h>
#pragma comment(lib,"ws2_32.lib")
using namespace 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.
What you said Makes a lot of sense. Can you give an example tgat makes sense to you.
Topic archived. No new replies allowed.