Click here to Skip to main content
15,894,955 members
Articles / Programming Languages / C++

MultiThreaed Client Server Communication

Rate me:
Please Sign up or sign in to vote.
2.43/5 (22 votes)
19 Jan 20056 min read 84.9K   5.2K   33  
Client Server communication in multithreaded environment
// Server.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include <winsock.h>

//function declaration
DWORD WINAPI ValidateData(LPVOID Parameter);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	WORD sockVersion;	//Used to store socket version information
	WSADATA wsaData;	//used to store info about socket
	SOCKET s,client;	//used to create client and server socket
	SOCKADDR_IN sin;	//used to specify a local or remote endpoint address to which to connect a socket
	int rVal;	//used to store return value
	
	HANDLE hThread;	//Handle to thread
	DWORD ThreadId;	//used to store the thread id
	
	//Get the current socket version
	sockVersion = MAKEWORD(1,1);
	
	//The Windows Sockets WSAStartup function initiates use of WS2_32.DLL by a process
	WSAStartup(sockVersion, &wsaData);
	//here we are creating the socket
	s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(s == INVALID_SOCKET)
	{
		MessageBox(0,"Invalid socket","Socket Error",MB_OK);
		WSACleanup();
	}
	//fill in sockaddr_in struct 
	sin.sin_family = PF_INET;
	sin.sin_port = htons(8888);
	sin.sin_addr.s_addr = INADDR_ANY;
	
	//bind to the socket
	rVal = bind(s, (LPSOCKADDR)&sin, sizeof(sin));
	if(rVal == SOCKET_ERROR)
	{
		MessageBox(0,"Failed in bind API","Bind Error",MB_OK);
		WSACleanup();
	}

	//Listen to the desire port on which client suppose to connect
	rVal = listen(s, 2);
	if(rVal == SOCKET_ERROR)
	{
		MessageBox(0,"Failed in listen API","Listen Error",MB_OK);
		WSACleanup();
	}
	//infinite loop to serve all the clients which want service
	while(1)
	{
		//Accept the data from the client
		client = accept(s, NULL, NULL);

		//Once the new client come up create a new thread t serve the client
		if(client)
		{
			hThread = CreateThread(NULL,
                            0,
							ValidateData,
                            (LPVOID)client,
                            0,
                            &ThreadId);
		}
		else
			return 0;
	}
	return 0;
}

////////////////////////////////////////////////////////////////////////
// ValidateData
//
//	Function to serve client request
//	In this function i am doing a simple check 
//  like string comparision(for simpicity)
//  you can do your own data validation here
//
/*********************************************
*	Autohor			:	Vishal 
*	Function Name	:	ValidateData
*	Created on		:	19-December-04
*	Last Modified	:	24-December-04
*	Organisation	:	Codeproject.com
*********************************************/
DWORD WINAPI ValidateData(LPVOID Parameter)
{
	//Get the information about client entity
	SOCKET client = (SOCKET)Parameter;
	int rVal;	//Return val
	char buf[11];	//used to send the validated data to client

	//Get the data form client
	rVal = recv(client,buf,11,0);
	//here we are performing simple check, the data came form client
	//is valid or not
	//at this point you can check your own data also, which needs some modification
	if(strcmp(buf,"Data On Socket"))
	{
		//Send back the data to the client
		rVal = send(client,"YES",3,0);
	}
	else
	{
		//Send back the data to the client
		rVal = send(client,"NO",2,0);
	}
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
India India
I'm working as Sr. Software Engineer in INDIA from 9+ years.

Comments and Discussions