WINSOCK CLIENT/SERVER IPC CLASS
The CWinSockStreamSendRecv class is a class I have used in more than one project of mine in the past. It should be enough tested for being used with confidence in other projects as well. If you have suggestions and/or corrections about this implementation, please feel free to write to me.
It is intended expressly for client/server IPC purposes: in fact the server code waits and accepts only one connection from the corresponding peer. If you want to write massive server-oriented WinSock applications, consider the use of I/O Completion Ports under Windows NT instead of a raw socket implementation. As you may already know connection-oriented TCP sockets are stream-oriented: if you need a message-based IPC mechanism, take a look at my named pipe classes.
Additionally you should consider controlling the access to its blocking "Send" method with a critical section (for example) if you plan to consume this object from multiple threads simultaneously.
TYPE DEFINITION DETAILS
CWinSockStreamSendRecv( charstring& strHost, unsigned short usPort, PFNRECV pfnRecvCallback, int iRecvBufferSize, DWORD dwRecvCallbackParam, PFNCLOSE pfnCloseCallback = NULL );
BOOL Connect ();
void Disconnect ( BOOL wait = TRUE );
int Send ( CONST BYTE* pbBuffer, int nBufferSize );
VOID SetFilterAddress( in_addr address );
VOID SetPort( unsigned short port );
BOOL IsConnected ();
HOW TO USE
Simply instantiate the class and then call the Connect method. Constructing the instance specifying or not the "strHost" parameter determines whether the resulting object will represent a server or a client socket: if you specify the host name, the object will be the client part, conversely if you omit it, the resulting object will be the server part listening and accepting connections.
All my IPC classes use STL under the hood. Be sure to include the following definitions BEFORE the definitions of all my IPC types:
// Stl Stuff.
typedef std::basic_string< CHAR > charstring;
typedef std::vector< charstring > charstring_vector;
typedef std::basic_string< WCHAR > widestring;
typedef std::vector< widestring > widestring_vector;