Networking Tutorial
Steps:
Server:
- Create a socket
- Accept the socket
- send a recive data in the socket
- exchange packets
- end the socket
Client:
- Create a socket
- send a recive data in the socket
- exchange packets
- end the socket
Introduction:
This Tutorial will show you how to send data between 2 computers or more, this networking is needed in everything related to Internet(Multiplayer games, surfing the internet, IRC...) even the internet browsers are Clients that connects to the website servers to exchange the files
so with this tutorial, we will learn how to do it
things needed to di this tutorial:
- Microsoft Visual Studio 2008 Professional Edition or Visual C++ 2008
- A knowledge of the C++ basics(if, else, union, struct, basics functions...)
The Server
1-Create a socket
juste to start we have to got this includes in the top of our file
#include <windows.h> #include <stdio.h>
we have to import this library "ws2_32.lib"
#pragma comment(lib,"ws2_32.lib")
now we have to put this under the library
SOCKET sock; // this is the socket that we will use it SOCKET sock2[200]; // this is the sockets that will be recived from the Clients and sended to them SOCKADDR_IN i_sock2; // this will containt informations 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
now we have to start creating our socket, like this
int StartServer(int Port) // this is the function
{
int err;
WSAStartup(MAKEWORD(2, 2), &Data);
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == INVALID_SOCKET)
{
Sleep(4000);
exit(0);
return 0;
}
now WSAStartup is to initialize the socket and set our socket version
-MAKEWORD(2, 2): we have choosed version 2.2
-&Data: is to save the socket version
socket function is to set the socket types
-AF_INET: The address family specification. AF_INET is for IPv4 and AF_INET6 is for IPv6
-SOCK_STREAM: is the type stream of the socket(A socket type that provides sequenced), there 3 others type such as: SOCK_DGRAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET but we will use the stream socket now
-0: is the protocol, we have choosed 0(NULL) so that it will automaticly use the UDP port, you can put (IPPROTO_UDP[UDP Port], IPPROTO_TCP[TCP Port], IPPROTO_RM[Multicast type], BTHPROTO_RFCOMM[Bluetooth Radio Frequency Communications])
now this is to set informations about our socket
i_sock.sin_family = AF_INET;
i_sock.sin_addr.s_addr = htonl(INADDR_ANY);
i_sock.sin_port = htons(Port);
err = bind(sock,(LPSOCKADDR)&i_sock,sizeof(i_sock));
if(err != 0)
{
return 0;
}
i_sock.sin_family = AF_INET; is The address family specification(see the example in the socket function)
i_socke.sin_addr.s_addr = htonl(INADDR_ANY); is to start the server at your IP
i_socke.sin_port = htons(Port); is to set the Server port(htons(Port)) "Port" is in "StartServer(int Port)"
now the bind function is to associates a local address with a socket.
-sock is the socket that was created in the socket function
-(LPSOCKADDR)&i_sock this is the information about the socket
-sizeof(i_sock) the size of the socket information
now we have to listen to our socket
err = listen(sock, 2);
if(err == SOCKET_ERROR)
{
return 0;
}
-sock is the socket that was created in the socket function
-2 is the maximum of clients supported
2-Accept
while(1)
{
for(int i = 0; i < 4; i ++)
{
if(clients < 4)
{
int so2len = sizeof(i_sock2);
sock2[clients] = accept(sock, (sockaddr *)&i_sock2, &so2len);
if(sock2[clients] == INVALID_SOCKET)
{
return 0;
}
printf("a client has joined the server(IP: %s)\n", i_sock2.sin_addr.s_addr);
clients++;
continue;
}
else
{
break;
}
}
}
return 1;
now accpet function is to accept the inconming connections
-sock is the socket that was created in the socket function
-(sockaddr *)&i_sock2 is the information of the accepted client
-so2len is the size of the informations about the accepted client
the loop is to accept more than one client(for(int i = 0; i < 4; i ++)) you can change the 4 with your maximum numbers of client
if(client < 4) is to check if there is less than 4 clients(you can change the 4 with your maximum numbers of client)
sock2[clients]: clients is in the top of your project and it returns the client that we like to exchange with him informations(so if a client is connected it gives him ID 0, then clients++: is to up the clients number, so the next will be ID 1)
so now we have to got our file like this
#include <windows.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
SOCKET sock; // this is the socket that we will use it
SOCKET sock2[200]; // this is the sockets that will be recived from the Clients and sended to them
SOCKADDR_IN i_sock2; // this will containt informations 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 StartServer(int Port)
{
int err;
WSAStartup(MAKEWORD(2, 2), &Data);
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == INVALID_SOCKET)
{
Sleep(4000);
exit(0);
return 0;
}
i_sock.sin_family = AF_INET;
i_sock.sin_addr.s_addr = htonl(INADDR_ANY);
i_sock.sin_port = htons(Port);
err = bind(sock,(LPSOCKADDR)&i_sock,sizeof(i_sock));
if(err != 0)
{
return 0;
}
err = listen(sock, 2);
if(err == SOCKET_ERROR)
{
return 0;
}
while(1)
{
for(int i = 0; i < 4; i ++)
{
if(clients < 4)
{
int so2len = sizeof(i_sock2);
sock2[clients] = accept(socke (sockaddr *)&i_sock2, &so2len);
if(sock2[clients] == INVALID_SOCKET)
{
return 0;
}
printf("a client has joined the server(IP: %s)\n", i_sock2.sin_addr.s_addr);
clients++;
continue;
}
else
{
break;
}
}
}
return 1;
}
3-Send a recive data in the socket
now that our server has been created, we have to understand how to exchange informations between the Client and the server
now we create a function named "Send"
int Send(char *Buf, int len, int Client)
{
}
the int len is the size of the data that will be sended in char *Buf
and int Client is the client ID that the data will be sended to him
slen = send(sock2[Client], Buf, len, 0);
if(slen < 0)
{
printf("Cannot send data !");
return 1;
}
return slen;
in the send function there is
-sock2[Client] is the accepted client ID, Client is in "int Send(char *Buf, int len, int Client)"
-Buf the data that will be sended, Buf is in "int Send(char *Buf, int len, int Client)"
-len is the size of data that will be sended, len is in "int Send(char *Buf, int len, int Client)"
-0 A set of flags that specify the way in which the call is made
and in return slen; our function will return the bytes of the sended data
now we know how to send data we have to know also how to recive it
we create now a function named "Recive"
int Recive(char *Buf, int len, int Client)
{
}
the int len is the size of the data that will be recived in char *Buf
and int Client is the client ID that the data will be recived from him
slen = recv(sock2[Client], Buf, len, 0);
if(slen < 0)
{
printf("Cannot send data !");
return 1;
}
return slen;
in the recv function there is
-sock2[Client] is the accepted client ID, Client is in "int Recive(char *Buf, int len, int Client)"
-Buf the data that will be recived, Buf is in "int Recive(char *Buf, int len, int Client)"
-len is the size of data that will be recived, len is in "int Recive(char *Buf, int len, int Client)"
-0 A set of flags that specify the way in which the call is made
and in return slen; our function will return the bytes of the recived data
4-exchange packets
now that we understand how to create a server, send and recive data, we have also to know how to send and recive something that is not a message
we will start by using a packet
struct MyPacket
{
int mylong;
char mystring[256];
};
this is my packet code(you must undertsand it, if you like to follow our tutorial)
now to send it, is so easy, do this
MyPacket packet; // this is the struct description Send((char *)&packet, sizeof(packet), 1); // our function that we have created to send data
this is to send the packet to the Client ID 1, if you like to send it to all the clients, so call the function in a loop, like this
for(int i = 0; i < 4; i ++)
{
MyPacket packet; // this is the struct description
Send((char *)&packet, sizeof(packet), i); // our function that we have created to send data
}
you can always change 4 with the maximum supported Clients
now to recive it, is so easy, do this
MyPacket packet; // this is the struct description Recive((char *)&packet, sizeof(packet), 1); // our function that we have created to recive data
this is to recive the packet to the Client ID 1, if you like to recive from all the clients, so call the function in a loop, like this
for(int i = 0; i < 4; i ++)
{
MyPacket packet; // this is the struct description
Recive((char *)&packet, sizeof(packet), i); // our function that we have created to recive data
}
you can always change 4 with the maximum supported Clients
Warning: to recive the packet you have to do the same struct in your Client, so if you send for example in the client this "MyPacket pack" and in the server you like to recive this "MyPacket packet" it will not work, so you have to send "MyPacket packet" from the cleint, and recive "MyPacket packet" in the server
5-end the socket
now to end or close our socket we have to do this
create a function named "EndSocket"
int EndSocket()
{
}
now do this
int EndSocket()
{
closesocket(sock);
WSACleanup();
return 1;
}
in closecoket function we see
-sock is the socket that was created in the top using the socket function
and WSACleanup is to end the socket and clean everything
The Client
1-Create a socket
to create the socket it's so easy, like the server
#include <windows.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
SOCKET sock; // this is the socket that we will use it
SOCKADDR_IN i_sock; // this will containt some informations about our socket
WSADATA Data; // this is to save our socket version
int Connect(char *IP, int Port)
{
WSAStartup(MAKEWORD(2, 2), &Data);
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == INVALID_SOCKET)
{
return 1;
}
i_sock.sin_family = AF_INET;
i_sock.sin_addr.s_addr = inet_addr(IP);
i_sock.sin_port = htons(Port);
int ss = connect(sock, (struct sockaddr *)&i_sock, sizeof(i_sock));
if(ss != 0)
{
printf("Cannot connect");
return 0;
}
printf("Succefully connected");
return 1;
we have juste changed "INADDR_ANY" by "inet_addr(IP);"
so this will contiant the IP that we will connect
in the connect function we have
-sock is the socket that was created in the socket function
-(struct sockaddr *)&i_sock this is to connect to the server(we have putted his informations in i_sock.sin...)
-sizeof(i_sock) the size of the server informations
and we have removed the: bind, accept, listen functions
2-Send a recive data in the socket
to send or receive data in the Client, we have to use the same function that we have created in the server:
Send(char *Buf, int len)
Recive(char *Buf, int len)
but there is a little modification in the send like this
int slen;
slen = send(sock, Buf, len, 0);
if(slen < 0)
{
printf("cannot send data");
return 1;
}
return slen;
and in the recive like this
int slen;
slen = recv(sock, Buf, len, 0);
if(slen < 0)
{
printf("cannot recive data");
return 1;
}
return slen;
we have juste to change "sock2[Client]" with "sock" only
3-exchange packets
always the same as the server, to exchange packets, look to the step 4 in the server
4-end the socket
always the same thing to do it you have to look to the step 5 in the server
I hope to enjoy this tutorial






MultiQuote








|