Page 1 of 1

Networking tutorial exchange data between Client and Server using a stream socket Rate Topic: ****- 3 Votes

#1 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Post icon  Posted 07 November 2009 - 06:48 AM

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


Is This A Good Question/Topic? 4
  • +

Replies To: Networking tutorial

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 13 November 2009 - 11:40 PM

Well Done. :^:
Was This Post Helpful? 0
  • +
  • -

#3 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Posted 21 December 2009 - 01:06 PM

View Postborn2c0de, on 13 Nov, 2009 - 10:40 PM, said:

Well Done. :^:

thx btw why the C++ tutorials section is bugged ??
when i enter it i cannot see no my tutorials no the other tutorials ?
Was This Post Helpful? 0
  • +
  • -

#4 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Posted 15 February 2010 - 03:58 AM

View PostJames_Alex, on 21 December 2009 - 12:06 PM, said:

View Postborn2c0de, on 13 Nov, 2009 - 10:40 PM, said:

Well Done. :^:

thx btw why the C++ tutorials section is bugged ??
when i enter it i cannot see no my tutorials no the other tutorials ?

finally i can see my tutorial
Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Posted 16 February 2010 - 10:55 PM

Tutorials here require moderator approval.
Was This Post Helpful? 1
  • +
  • -

#6 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Posted 29 March 2010 - 08:02 PM

I know this tutorial is older, but it would be helpful if the server and client files were added in there entirety. All that's shown for the server file is a StartServer function which takes an int. It would be nice to see how it is implemented.
Was This Post Helpful? 0
  • +
  • -

#7 laserbeak43  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 134
  • Joined: 07-November 09

Posted 13 April 2010 - 09:49 PM

View PostEd_Bighead, on 29 March 2010 - 07:02 PM, said:

I know this tutorial is older, but it would be helpful if the server and client files were added in there entirety. All that's shown for the server file is a StartServer function which takes an int. It would be nice to see how it is implemented.

yep, that's what I thought I was going to see when I started reading this.
Was This Post Helpful? 0
  • +
  • -

#8 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Posted 18 July 2010 - 02:22 PM

This is an update of the tutorial
Steps:

  • Multithreaded server
  • Control of the server
  • Checking for client disconnection and connection lose
  • Clients ID system


Multithreaded server

as many of you has noticed, in the tutorial examples above, your programme will only wait for connections to accept, but what if we make it do many things such as checking for the client connection lose, disconnections requests...

all we have to do is just start many threads, starting a thread isn't that hard, all we have to do is first, include the process header
#include <process.h>


now we can use the "_beginthread()" function
-void( *start_address )( void * ) is the function that the thread will be running
-unsigned stack_size Stack size for a new thread, generally make a NULL for it
-void *arglist List of arguments that will be passed to the thread function

now we know what parameters "_beginthread" function have
#include <process.h"

void AcceptClients(void *vArgs); // This is the function that the thread will call, void *vArgs, is the list of arguments that the "_beginthread" function has passed

// remember our "StartServer function"
int StartServer(int Port)
{
   // ... function code (look above)
   _beginthread(AcceptClients, NULL, NULL); // In the bottom of the function instead of the clients accepting code
}

void AcceptClients(void *vArgs)
{
    while(1) // Keep the thread alive
    {
	for(int i = 0; i < 4; i ++)
	{
		if(clients < 4)
		{
			int iSocketLen = sizeof(i_sock2);
			sock2[clients] = accept(socke (sockaddr *)&i_sock2, &so2len);
			if(sock2[clients] == INVALID_SOCKET)
			    continue;
			printf("a client has joined the server(IP: %s)\n", i_sock2.sin_addr.s_addr);
			clients++;
			continue;
		}
		else
		{
			break;
		}
	}
    }
    _endthread(); // End the thread when exitng
}



Control of the server

now we know how to accept incoming connections, but what if we want to ban a client ? or check for connections lose, or simply tell the client that the server is full ? so we should create a custom control of our server, ok to start, we create an enum function, you can use that with simple integers if you have experience, but with enums is more safety
enum eNetworkCommand
{
    CONNECTION_ACCEPTED,
    SERVER_FULL,
    CONNECTION_BANNED
};


now we represent the commands in an enum, after we should recreate our accepting code to make it a little bit controlled
void CloseConnection(int iClientID)
{
    closesocket(sock2[iClientID]);
}

int Send(int iClientID, char *SendBuf, int SendLen)
{
	// Send
	int SendByte = send(sock2[iClientID], SendBuf, SendLen, 0);
	// If succeded
	if(SendByte < 0)
	{
		return -1;
	}
	// If not
	else
	{
		return SendByte;
	}
}

bool Accept(int iClientID)
{
    int iSockLen = sizeof(sock2[iClientID]);
    // Accept the connection
    accept(sock2[iClientID], (sockaddr *)&i_sock2, &iSockLen);
    // If nothing to accept then return false
    if(sock2[iClientID] == INVALID_SOCKET)
    {
	return false;
    }
    else
    {
	// Display incoming connection message
	print("Accepting connection from %s:%d...", inet_ntoa(sock2[iClientID].sin_addr), ntohs(sock2[iClientID].sin_port)); // display the message with the client IP & Port
	// Add +1 to current connected clients
	Clients++;
	return true;
    }
}

void AcceptClients(void *vArgs)
{
    eNetworkCommand NetCmd; // Create ou enum instance
	while(1)
	{
		// Accept the incoming connection
	    if(Accept(Clients) == true) // Now check if we have a new connection
	    {
			// Check if the server is not full
	        if(Clients < 4)
		    {
				// Tell the client that the connection is accepted
			    NetCmd = CONNECTION_ACCEPTED;
			    // Send the the message
			    if(Send(Clients, (char *)&NetCmd, sizeof NetCmd) != -1) // Check if we have sended the info
	            {
					print("Client has successfully joined the server\n");
			        continue;
                }
                // You can do some failed code here with else
		    }
		    else
		    {
				// Inform him that the server is full
		        NetCmd = SERVER_FULL;
				// Send him the info :)/>
				Send(Clients, (char *)&NetCmd, sizeof NetCmd);
                                CloseConnection(Clients); // Close the client connection
                 continue;
		    }
        }
	    else
	    {
			continue;
	    }
	}
	// Exit the thread
	_endthread();
}


now we have made it check many things, you can add a ban system, by for example saving the ban addresses to a file, then load the file when start, check the connection IP, and send him the CONNECTION_BANNED

This is the client side code, you should do the same enum as the server, then after the connection attempt success, you start a thread, then wait for the receive thing
eNetworkCommand NetCmd;
while(1)
{
	if(recv(sock, (PCHAR)&NetCmd, sizeof NetCmd, 0) < 0)
    {
		switch(NetCmd)
        {
			case CONNECTION_ACCEPTED:
				printf("Successfully connected.\n");
				break;
			case SERVER_FULL:
				printf("Server is full.\n");
				break;
			case CONNECTION_BANNED:
				printf("Connection banned.\n");
				break;
		}
	}
	continue;
}
_endthread();


now your client is done

Checking for client disconnection and connection lose

coming soon...
i have to go so i'll post onlt 50% of the tutorial right now, i'll continue this tutorial and add some more useful stuff

This post has been edited by James_Alex: 18 July 2010 - 02:27 PM

Was This Post Helpful? 1
  • +
  • -

#9 AMnova  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 15-November 09

Posted 22 July 2010 - 06:56 AM

View PostJames_Alex, on 07 November 2009 - 05:48 AM, said:

Networking Tutorial



3-Send a recive data in the socket

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;

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;



Im confused with the recive and Send functions, are those prototypes or something or just a misplacement of curly brackets? Where do you get the recv and send functions from? is already defined in a header? All I get is an error that they the arguements in the recv functions do not exist (which i understand why) and i also get a linking error "unresolved external symbol_main referenced in function _tmainCTRStartup". I made sure i created the project under console application. Did i do something wrong? I get that error when i try to build the creation of the server only meaning i erase the parts about recieving and send data....

#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
SOCKET sock;			//sock is the name of the socket we will use
SOCKET sock2[200];		//sock2 will be the socket clients connect to this server will use, MAX. SUPPORT == 200 clients
SOCKADDR_IN i_sock2;	//contain information pertaining to the clients connected to this server
SOCKADDR_IN i_sock;		//contain information pertaining to our socket
WSADATA	data;			//will be used to save the socket version
int clients=0;			//will count the amount of clients that are connect to this server, as well assign each client a number via a for loop

int StartServer(int Port)
{
	int err;   //will be used as to check for errors in our program

	/*initializes our socket, "MAKEWORD" function chooses our socket type (in this case 2.2). the address of "data" is 
	passed to the "WSASTARTUP" where the function will save the socket version into the "data" variable*/
	WSAStartup(MAKEWORD(2, 2), &data);

	/*the socket function obtains information from AF_INET (address family specifications for IPv4), SOCK_STREAM (which is the stream socket),
	0 is protocol, 0 (which is NULL) was chosen so that the program will automatically use the UDP port */
	sock = socket(AF_INET, SOCK_STREAM, 0);

	/*checks for errors in our newly initialized socket */
	if(sock == INVALID_SOCKET)
	{
		Sleep(4000);
		exit(0);
		return 0;
	}


	i_sock.sin_family = AF_INET;				  //assigns the address family specification(AF_INET) to "i_sock.sin_family"
	i_sock.sin_addr.s_addr = htonl(INADDR_ANY);   //starts the server in your IP address
	i_sock.sin_port = htons(Port);				  //sets the Server Port "htons(Port)"

	/* three arguements are passed to the bind function, "sock" the variable we created under the SOCKET data type, "(LPSOCKADDER)&i_sock" 
	which is just information pertaining to the socket we created, and the size of the data inside "i_sock" */
	err = bind(sock, (LPSOCKADDR)&i_sock, sizeof(i_sock));
	if(err != 0)
	{
		return 0;
	}

	/*listens to our sockets "sock", maximum amount of clients is 4 */
	err = listen(sock, 4);
	if(err = SOCKET_ERROR)
	{
		return 0;
	}

	while(1)
	{
		
		for(int i=0; i<4; i++) //insures that the loop does not run more than 4 times
		{
			if(clients < 4) //maximum amount of clients supported is 4, breaks loop if clients surpasses 3
			{
				int so2len = sizeof(i_sock2); //assigns the size of the data being held in "i_sock2" to the int var "so2len"

				/*the accept function is used to gather information from incoming connections from clients. 3 arguments are passed to
				the accept function. sock is the socket we created, "(sockaddr *)&i_sock2" holds information from client, and "&so2len"
				is the amount of data in "i_sock2" (we assigned it this duty in the first line of this if-loop). All this information is
				then assigned to sock2[clients] */
				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;


/***************************************THIS MARKS THE END OF THE CREATION PHASE OF OUR SERVER***********************************************/
	/*A function to send data to clients */
	int Sent(char *Buf, int len, int Client)
	{}

	slen = sent(sock2[Client], Buf, len, 0);
	if(slen < 0)
	{
		printf("Cannot send data!!!");
		return 1;
	}
	
	return slen; 


	/*A function to recieve data from clients */
	int Recive(char *Buff, int len, int Client){}

	slen = recv(sock2[Client], Buf, len, 0);
	if(slen < 0)
	{
		printf("Cannot Recieve Data!!!");
		return 1;
	}
	return slen;

	struct MyPacket
	{
		int mylong;
		char mystring[256];
	};

	MyPacket packet;								//creates a variable of the type "MyPacket"
	Sent((char *)&packet, sizeof(packet), 1);		//uses the send function to send the variable "packet" to client #1

	MyPacket packet2;								//creates a variable of teh type "MyPacket"
	Recive((char *)&packet2, sizeof(packet2), 2);	//uses the recive function to recieve the variable "packet2" from client #2


/************************We are now done using the sockets, all we need to do now is do a little cleanup*************************************/

	int EndSocket(){}

	int EndSocket()
	{
		closesocket(sock);
		WSACleanup();
		return 1;
	}
}


I tried to add in my own comments to make it more clear for me but i have limited c++ experience and even less socket programming experience (this being my first attempt and first tutorial) so any corrections or clarifications on some of my comments would be more than welcome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1