Page 1 of 1

Is this right?

Posted: Sun Sep 05, 2004 1:11 pm
by Ferno
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;
	}
}

Posted: Sun Sep 05, 2004 2:00 pm
by DCrazy
I don't see a Winsock init anywhere to justify your WSACleanup.

Damn, this BB makes reading code impossible.

Posted: Sun Sep 05, 2004 2:12 pm
by Ferno
ws2_32.lib has already been linked to the project.

Posted: Sun Sep 05, 2004 3:34 pm
by fliptw
whats the error, and which version of VC?

Posted: Sun Sep 05, 2004 3:48 pm
by Ferno
quite a few.. i'll post the revised code and the error log here after I try Borland.

Posted: Sun Sep 05, 2004 3:49 pm
by fliptw
do most of them involve endl in some manner?

Posted: Sun Sep 05, 2004 3:51 pm
by Ferno
here's the output log:

--------------------Configuration: ConnectionTest - Win32 Debug--------------------
Compiling...
ConnectionTest.cpp
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(23) : error C2146: syntax error : missing ';' before identifier 'endl'
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(23) : warning C4551: function call missing argument list
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(34) : error C2059: syntax error : ')'
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(58) : error C2296: '<<' : illegal, left operand has type 'char [19]'
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(58) : error C2297: '<<' : illegal, right operand has type 'class ostream &(__cdecl *)(class ostream &)'
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(66) : error C2001: newline in constant
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(69) : error C2146: syntax error : missing ';' before identifier 'ClientSocketObject'
Error executing cl.exe.

ConnectionTest.exe - 6 error(s), 1 warning(s)

Re: Is this right?

Posted: Sun Sep 05, 2004 4:14 pm
by Lothar

Code: Select all


	if ( ServerSocketObject.Bind( iListenPort ) )
	{
		cout < "<Server> Listening" << endl;
There should be two less-than's after the cout statement. It's trying to get the return value from the cout, and the string on the right, and see which is larger. I think this accounts for:

D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(23) : error C2146: syntax error : missing ';' before identifier 'endl'
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(23) : warning C4551: function call missing argument list

Code: Select all

      cout << "<Server> Client Connected to Port : << iListenPort << endl;
You forgot to put an ending " in.

Clean these two up, and it should be easier to figure out what else is going on.

Posted: Sun Sep 05, 2004 4:27 pm
by Ferno
Lothar.. can you jump on AIM?

EDIT: revised error log:

D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(23) : error C2146: syntax error : missing ';' before identifier 'endl'
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(23) : warning C4551: function call missing argument list
D:\JDS PROGRAMS\ConnectionTest\ConnectionTest.cpp(43) : error C2059: syntax error : ')'

Posted: Sun Sep 05, 2004 4:43 pm
by Lothar
done...

OK, folks, move along now... nothing to see here... move along.