The CSock class provides synchronous send and receive of a buffer, plus ping and resolve address. The class encapsulates a listener thread which you can start and stop.

Advanced CMarkup Developer License

The source code for CSock comes in the firstobject XML Messaging project with the Advanced CMarkup Developer License.

CSock does not provide "asynchronous" messaging. The Messaging Project implements efficient asynchronous messaging using additional threads around CSock. CSock uses the CBuffer Class to contain the message data.

Send

HRESULT Send( CString csAddress, CBuffer& bufMessage );

Call Send(csAddress,bufMessage) to send the message to the specified TCP/IP address.

The address can be of the internet address form "255.255.255.255", the name of the computer on the LAN such as "machine2", or the fully qualified domain name such as "firstobject.com".

The function will return after it has completed sending the message or it fails (i.e. it is synchronous). If it fails, a descriptive error string can be found in the public m_csResults data member.

HRESULT hr = m_sock.Send( "machine2", "hello" );
if ( FAILED(hr) )
	csError = m_sock.m_csResults;

StartListener

HRESULT StartListener( CEvent* pReadEvent );

To use CSock to receive messages, call StartListener(&eventRead) to start listener. Then you can wait for the read event represented by eventRead to occur, at which point you would use the Read method to retrieve the message. This allows you to wait on both the read event and your own exit event (using the Win32 function WaitForMultipleObjects).

Note that it does not currently have the option of notifying you with a Windows Message so you have to either dedicate a thread to waiting for a message to arrive or else poll the read event which would be inefficient. You can see how additional threads around CSock are used in the Messaging Project to implement asynchronous messaging.

Read

HRESULT Read( CBuffer &bufMessage );

You will be notified that a message is ready via the read event that was specified in the StartListener method. At this point the message has already been brought into memory. However, in the current implementation of CSock, the listener will not continue listening until you have retrieved the buffer from your CSock object by calling Read(bufMessage).

In the following example, one message is received if it arrives within 5 seconds:

CSock sock;
CEvent event;
CBuffer bufMessage;
HRESULT hr = sock.StartListener( &event );
if ( SUCCEEDED(hr) )
{
	event.Lock( 5000 );
	hr = sock.Read( bufMessage );
}
if ( SUCCEEDED(hr) )
	Output( bufMessage.GetString() );
else
	Output( sock.m_csResults );
sock.StopListener();

StopListener

HRESULT StopListener();
Call StopListener() to stop the listener in your CSock object. This is also automatically called in the CSock destructor, so it is not necessary to call it explicitly.

Ping

HRESULT Ping( CString csAddress );

Call Ping(csAddress) to test if there is a TCP/IP network connection to the specified address. This is a synchronous function and occassionally under some circumstances there can be a long delay when the address is not resolved quickly.

As with the Send method, the address can be in the form of an internet address string "206.83.84.176", the name of the computer on the LAN such as "machine2", or the fully qualified domain name such as "firstobject.com".

ResolveAddress

HRESULT ResolveAddress( CString csName );

Call ResolveAddress(csName) to determine the IP address of a named remote machine. The csName string refers to a computer on your LAN such as "machine2" or a fully qualified domain name.