Пример #1
0
// Function for client
void vClientConnection( char *szServerIP, int iServerListenPort )
{
	SocketObject	ClientSocketObject;
	char			DataPacket[128];		// Data packet to transmit
	int				iBytesSent = 0;			// # of bytes sent

	cout << "<Client> Connecting to " << szServerIP << ", Port " << iServerListenPort << endl;
	
	// Connect to the IP and Port 
	if( ClientSocketObject.Connect( szServerIP, iServerListenPort ) )
	{
		cout << "<Client> Connected" << endl;

		// Populate the data packet
		//strcpy(DataPacket, "TestData from Client");
		sprintf(DataPacket, "TestData from Client");
		//scanf("%s", &DataPacket);
		
		// Send data
		iBytesSent = ClientSocketObject.Send(DataPacket, 128, 0);
		cout << "<Client> Transmitted " << iBytesSent << " Bytes" << endl;
		
		// Disconnect from the server
		ClientSocketObject.Disconnect();
		cout << "<Client> Disconnected From Server" << endl;
	}
	else
	{
		cout << "<Client> Failed to Connect" << endl;
	}
}
Пример #2
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;
	}
}
Пример #3
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;
}