Ejemplo n.º 1
0
// Function for server
void vServerConnection( int iListenPort )
{
	SocketObject	ServerSocketObject;
	SocketObject	ClientSocketObject;
	//Data can be greater than 128 in length, it's just set to 128 for efficiency.
	//char			DataPacket[128];				// Data packet to receive//original version
	char			DataPacket[1024];				// Data packet to receive
	memset(DataPacket, 0, 1024);
	int				iBytesReceived = 0;				// # of bytes received
	
	cout << "<Server> Attempting to listen on Port " << iListenPort << endl;
	
	// Attempt to start the server
	if ( ServerSocketObject.Bind( iListenPort ) )
	{
		cout << "<Server> Listening" << endl;
		
		// Listen for connection on the Listen port, 
		ServerSocketObject.Listen();
		
		// Accept the connection
		ServerSocketObject.Accept( ClientSocketObject );
		cout << "<Server> Client Connected to Port " << iListenPort << endl;
		
		// Receive data
		//iBytesReceived = ClientSocketObject.Recv(&DataPacket[0], 128, 0);//original version
		iBytesReceived = ClientSocketObject.Recv(&DataPacket[0], 1024, 0);
		printf("%s\n", strerror(errno));
		cout << "<Server> Received " << iBytesReceived << " Bytes" << endl;
		cout << "<Server> Data Received = " << DataPacket << endl;

		// Disconnect the client
		ClientSocketObject.Disconnect();
		cout << "<Server> Client Disconnected" << endl;
	}
	else
	{
		cout << "<Server> Failed to Listen" << endl;
	}
}
Ejemplo n.º 2
0
DWORD WINAPI InspectionManager(LPVOID lpParameter)
{
	HANDLE hEvent = CreateEvent(
		NULL,
		FALSE,
		FALSE,
		EVENT_NAME
		);
	ThreadObject* tpThreads[DRIVES];
	for (int iIndex = 0; iIndex < DRIVES; iIndex++)
		tpThreads[iIndex] = NULL;
	DetectAlreadyConnectedDevices(tpThreads);
	try
	{
		int iOptionValue = 1;
		SocketObject* spSocketServer = new SocketObject();
		spSocketServer->SetSockOpt(SOL_SOCKET, SO_REUSEADDR, (char*)&iOptionValue, sizeof(iOptionValue));
		spSocketServer->Bind(IP, PORT);
		spSocketServer->Listen(BACKLOG);
		SetEvent(hEvent);
		SocketObject* spSocketClient = spSocketServer->Accept();

		char szData[BUFFER];
		bool bRunning = true;
		while (bRunning)
		{
			int iLength = spSocketClient->Recv(szData, BUFFER);
			szData[iLength] = '\0';
			if (strstr(szData, IN_DRIVE) != NULL)
			{				
				char cDrive = szData[strlen(IN_DRIVE)];
				int iIndex = cDrive - 'A';
				printf("%c:\\ will be watched\n", cDrive);
				tpThreads[iIndex] = new ThreadObject(Inspection, (LPVOID)cDrive);
				tpThreads[iIndex]->Start();
			}
			else if (strstr(szData, OUT_DRIVE) != NULL)
			{
				char cDrive = szData[strlen(OUT_DRIVE)];
				int iIndex = cDrive - 'A';
				/*
				If the drive is already being watched
				by the ReadDirectoryChangesW function
				than stop watching it.
				*/
				if (tpThreads[iIndex] != NULL)
				{
					printf("%c:\\ will no longer be watched\n", cDrive);
					tpThreads[iIndex]->Kill();
					delete tpThreads[iIndex];
					tpThreads[iIndex] = NULL;
				}
			}
			else
			{
				spSocketClient->Close();
				bRunning = false;
			}
		}
	}
	catch (SocketError& socketError)
	{
		printf("Main [%d] %s", socketError.GetErrorCode(), socketError.ErrorFormat());
	}
	return 0;
}