Exemplo n.º 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;
	}
}
Exemplo n.º 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;
	}
}