// Client_Server_Class.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <winsock.h>

#pragma comment(lib,"wsock32.lib")
using namespace std ;


class Server 
{
private:

int SEARCH_PORT;
int DOWN_PORT;
int COM_PORT;
int MAXLISTENSESSIONS;

int Com_Clients;
int Search_Clients;
int Down_Clients;

WSADATA wsa;

SOCKET SocSearch;
SOCKET SocCom;
SOCKET SocDownload;

SOCKADDR_IN addr_search;
SOCKADDR_IN addr_Com;
SOCKADDR_IN addr_Download;

SOCKET Clients[20];
public:

Server();
void Start_Server();
void Start_Com_Session(UINT);
void Start_Search_Session(UINT);
void Start_Download_Session(UINT);


};
Server s;

int kind =1;
//==============================================================
//==============================================================
DWORD WINAPI Accept_Search_Port(PVOID g)
{
s.Start_Search_Session(0);
return 0;
}
DWORD WINAPI Accept_Com_Port(PVOID g)
{
s.Start_Com_Session(0);
return 0;
}
DWORD WINAPI Accept_Download_Port(PVOID g)
{
s.Start_Download_Session(0);
return 0;
}
//==============================================================
//==============================================================

Server::Server()
{
SEARCH_PORT = 2222;
DOWN_PORT = 2223;
COM_PORT = 2222;
MAXLISTENSESSIONS =20;

Com_Clients = 0;
Search_Clients = 0;
Down_Clients = 0;
}

bool con = false;

void Server::Start_Com_Session(UINT PROTOCOL)
{
	cout << "OK"<<endl;
	SocCom = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	addr_Com.sin_port=htons(COM_PORT);
	addr_Com.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
	addr_Com.sin_family = AF_INET;

	
	SOCKET ss;
	//connect(SocCom,(sockaddr*)&addr_Com,sizeof(addr_Com));
	bind(SocCom,(sockaddr*)&addr_Com,sizeof(addr_Com));
	listen(SocCom,MAXLISTENSESSIONS);

	unsigned long b = 1;
	
	char buf[20];
	ZeroMemory(buf,sizeof(buf));
	
	///////////////////////////////////////
	////////////////////////////////////////
	// BLOCKING SOCKET BEGINS HERE ... ////
	///////////////////////////////////////
	.....................................


	ioctlsocket ( SocCom, FIONBIO, &b );
	while(1)
	{
	
	Sleep(100);

	//Clients[Com_Clients] = accept(SocCom,(sockaddr*)&addr_Com,&MAXLISTENSESSIONS);
	ss=accept(SocCom,(sockaddr*)&addr_Com,&MAXLISTENSESSIONS);

	if(ss==0);
	else if(ss==SOCKET_ERROR);
	else
	{
		b= 0;

		//Open If you want back to blocking Socket ....
	//ioctlsocket (SocCom , FIONBIO, &b );

	recv(ss,buf,20,0);
	recv(ss,buf,20,0);
	cout << buf;
	send(ss,buf,20,0);
	}

	/*
	if(Clients[Com_Clients]==0)
	{
		con =false;
	}
	else if(Clients[Com_Clients]==SOCKET_ERROR)
	{
		con = false;
	}
	else
	{
		con=true;
	//Com_Clients++;
	}
	*/

	//Clients[0] = accept(SocCom,(sockaddr*)&addr_Com,&MAXLISTENSESSIONS);
	//if(con)
	
	/*
	recv(Clients[Com_Clients],buf,20,0);
	send(Clients[Com_Clients],buf,20,0);

	*/

		//Sleep(100);
	}

	
	
	
	strcpy(buf,"HGF");

	

	cout << "END"<<endl;
}


void Server::Start_Download_Session(UINT PROTOCOL)
{
	
}


void Server::Start_Search_Session(UINT PROTOCOL)
{
	
}


void Server::Start_Server()
{
	WSAStartup(MAKEWORD(1,1),&wsa);
	DWORD dwThreadId, dwThrdParam = 1; 
    HANDLE hThread; 

//----------------------------------------------
//Communication Port Thread .. 
    hThread = CreateThread( 
        NULL,                        // default security attributes 
        0,                           // use default stack size  
        Accept_Com_Port,                  // thread function 
        &dwThrdParam,                // argument to thread function 
        0,                           // use default creation flags 
        &dwThreadId);                // returns the thread identifi
//-----------------------------------------------
//-----------------------------------------------
//Search Port Thread ...
	dwThrdParam = 5;

	kind =2;
	  hThread = CreateThread( 
        NULL,                        // default security attributes 
        0,                           // use default stack size  
        Accept_Search_Port,                  // thread function 
        &dwThrdParam,                // argument to thread function 
        0,                           // use default creation flags 
        &dwThreadId);                // returns the thread identifi
//-----------------------------------------------
//=----------------------------------------------
//DownLoad Port Thread ...
	 // kind = 3;

	dwThrdParam = 9;
	    hThread = CreateThread( 
        NULL,                        // default security attributes 
        0,                           // use default stack size  
        Accept_Download_Port,                  // thread function 
        &dwThrdParam,                // argument to thread function 
        0,                           // use default creation flags 
        &dwThreadId);                // returns the thread identifi
//-------------------------------------------------
//------------------------------------------------

}


class Client
{
private:
char* m_cConnect_To_Server_Address;

public:
void Connect(UINT type);
};

void Client::Connect(UINT type)
{

}


int _tmain(int argc, _TCHAR* argv[])
{

	s.Start_Server();
	cin.get();
	return 0;
}

