Hi,

I've browsed lots of resources on the topic and looked at lots of code, but I'm having a bit of a noob moment here trying to figure something out.

I've downloaded microsoft's CAsyncServer example here:
http://download.microsoft.com/downlo...S/MFCAsync.exe

It's an MFC-based client and server app, where the server listens to port 9898 and the client can connect to the server by providing the IP address. The client can send messages to the server which the server can display. Pretty straightforward stuff.

I tried writing my own console-based program to be able to communicate with the server but am having no luck.

1. The server is subclassing CAsyncSocket. Does my code have to use CAsyncSocket to connect as well, or can I use CSocket? Yes, I'm aware that CSocket blocks; I'm just asking. :-) That is, is there a 1-to-1 mapping between the server and client as to whether you can use CSocket vs. CAsyncSocket?

2. My code, regardless of using CSocket or CAsyncSocket doesn't connect. Here's the code:


Code:
// TCPTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "TCPTest.h"
#include <afxsock.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;


	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{
		// Initialize the AfxSocket
		if( !(AfxSocketInit(NULL))) {
			cout << "Cannot initialize socket!" << endl;
			return false;
		}


		CSocket connection;
		if( connection == NULL ) {
			cout << "crap!" << endl;
			return false;
		}
		
		cout << "Creating connection" << endl;
		// create the connection
		if( !connection.Create() ) {
			cout << "Error: Cannot create socket!" << endl;
			return false;
		}

		// try to connect it to the listener
		if( !connection.Connect( (LPCTSTR)"10.100.20.8", 9898 )) {
			LPVOID lpMsgBuf;

			DWORD dw = GetLastError(); 

			FormatMessage(
				FORMAT_MESSAGE_ALLOCATE_BUFFER | 
				FORMAT_MESSAGE_FROM_SYSTEM |
				FORMAT_MESSAGE_IGNORE_INSERTS,
				NULL, dw,
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
				(LPTSTR) &lpMsgBuf,	0, NULL );

			CString msg = (LPTSTR)lpMsgBuf;
			cout << "Error: Cannot connect to listener, maybe server not running?" << endl;
			connection.ShutDown( 2 );
			return false;
		}


			CString cmd;
			cmd = "hi there";
			connection.Send( cmd, cmd.GetLength(), 0 );
			cout << "done!";
			
		// close the connection
		//connection.ShutDown(2);

		connection.Close();
		//delete connection;
		}

		return nRetCode;
}

The app successfully gets to the connect part, but hangs, then throws the "Error: Cannot connect to listener, maybe server not running?" message.

I am running this app on my laptop. The server is running on my desktop. I know there's no firewall issue as I can run the client app (the one that comes with the server app) on my laptop and can connect and send messages back and forth.

So basically, I've looked at other examples, and all I really need to do is

AfxSocketInit()
create a Socket/ASyncSocket
run Socket.Create()
Socket.Connect( server, port )
Socket.Send( msg, msg.GetLength() )
Socket.Close()

That's essentially what's happening above, but it just doesn't connect! Bah!