Is this right?
Posted: Sun Sep 05, 2004 1:11 pm
I'm trying to get this program to compile for a project I'm working on (2nd revision)
Code: Select all
//###########################################################
//my code
// Standard includes
#include <iostream.h>
#include "..\\socketobject\\SocketObject.h"
void vServerConnection( int iListenPort );
void vClientConnection( char *szServerIP, int iServerListenPort );
//
// ----> Main Program Function (REQUIRED or it may screw up)
//
int main( int argc, char *argv[] )
{
if( argc < 3 ) {
cout << "----------------------------------------------------" << endl;
cout << " ConnectionTest Help " << endl;
cout << "----------------------------------------------------" << endl;
cout << "Usage: ConnectionTest [Client/Server] [ip,port/port]" << endl;
cout << "" << endl;
cout << "Example: ConnectionTest client 198,168.0.1 6000" << endl;
cout << "" endl;
cout << "Example: ConnectionTest server 6000" << endl;
cout << "" << endl;
return ( 0 );
}
//---DCRAZY---
// These two lines start up WinSock
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
//---END DCRAZY---
//
// If user selected server, listen on the given port
// and assign connection to the second port number
//
if( !stricmp( argv[1], "server" ) )
{
vServerConnection( atoi( argv[2] ) ) );
}
//
// User selected client; connect to given port and IP address
//
else {
vClientConnection( argv[2], atoi( argv[3] ) );
}
WSACleanup();
return( 1 );
}
// Function for server
void vServerConnection( int iListenPort )
{
SocketObject ServerSocketObject;
SocketObject ClientSocketObject;
cout << "<Server> Attempting to listen on Port " << iListenPort << endl;
// Attempt to start the Server on port 6000
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;
// Disconnect the client
ClientSocketObject.Disconnect();
cout << "<Server> Client Disconnected" << endl;
}
else {
cout << "<Server> Failed to Listen" << endl;
}
}
//Function for Client
void vClientConnection( char *szServerIP, int iServerListenPort )
{
SocketObject ClientSocketObject;
cout << "<Client> Connecting to " << szServerIP << ", Port " << iServerListenPort << endl;
// Connect to thge IP and Port
if( ClientSocketObject.Connect( szServerIP, iServerListenPort ) )
{
cout << "<Client> Connected" << endl;
// Disconnect from the server
ClientSocketObject.Disconnect();
cout << "<Client> Disconnected from the server" << endl;
}
else {
cout << "<Client> Failed to Connect" << endl;
}
}