CSocket class by lazy coder for lazy coders
Updated Sept 11,2002

I created this WinSock wrapper when I came back from Java socket programming to raw WinSock API. It does not use MFC nor implement sexy features about sockets, but it definitely provides what most people might be looking for when programming sockets.


To listen for incomming connections on a local port number call CSocket( int iPort) constructor.
Note : Connection problem during constructor call will raise a CSocketException, so you can catch it.

Example :
CSocket *pSocket = new CSocket( 2000); // create the listenning socket
CSocket *pDataSocket = pSocket->Accept(); // blocking call to wait for incomming connections
// now you can do pretty much what you want with Read(); Write(); and Close();


To connect a remote machine currently listenning for incomming connections, call CSocket( char * szRemoteMachine, int iPort) constructor.
See previous note on CSocketException.

Example :
CSocket *pSocket = new CSocket( "localhost", 2000);
// now you can do pretty much what you want with Read(); Write(); and Close();


CSocket.cpp and CSocket.h, the class source code
Main.cpp This file demonstrates a dumb client/server data exchange.
release/CSocket.exe The compiled dumb example, see below for instructions


Add CSocket.h to your source file and CSocket.cpp to your project. Both file should compile without warnings.
You should also add WSOCK32.LIB to your project as this is what CSocket is wrapping. This file is not in the package as it comes with your compiler. Check the /lib subfolder of VisualC++ directory.


CSocket.exe is a console application.

You should First run a server instance of the program :
start CSocket.exe 2000

Now you can run as many clients as desired (at no extra cost) :
start CSocket localhost 2000

Client and server will exchange dumb informations, then client will stop


You can reach me at jc@jollant.net for question, comments, suggestions.


CSocket.zip


This stuff is absolutely not copyrighted by Jean-Cedric Jollant. You may distribute it as you wish. I honestly don't care. :)