This article provides a C++ wrapper to the WINSOCK functions. The code consists of four classes; they are:
CSocketException. These classes try to imitate Java socket classes whenever possible. They are designed to provide a very simple interface to sockets and I am sure you will find them useful in some way.
Using the Code
The first step in using the socket classes is to initialize the Windows socket library. This can by done by calling the
Initialize() static function. After using the socket classes, the library must be unloaded by calling the
Finalize() static function.
To create a server, declare an object
CServerSocket and call its
Accept() function. There are three constructors to
CServerSocket. The default constructor that takes no argument and initializes the server to listen on port 80. A server also has a queue size, i.e. the number of connections that will wait in an internal queue for processing. This defaults to 10. Another constructor takes the port number as an argument. There is yet another constructor that receives the port number and queue size as arguments.
Accept() function returns a pointer to
CSocket. The application can use this pointer to communicate with the client. Use the
Send() functions for this.
Read() takes two arguments: a pointer to a character buffer and an integer that specifies the number of bytes to read. It is the caller's duty to initialize this buffer.
Send() takes a pointer to a character buffer as its argument. This is the data to write to the network. After all reading and writing is over, it is the caller's duty to delete the
CSocket object. The following snippet illustrates a simple server that accepts a single connection.
#include < string>
#include < vector>
using namespace std;
#include < windows.h>
using namespace openutils;
CServerSocket server(100); 0
CSocket* client = server.Accept(); t
char *buffer = new char;
delete client; r
In a real-world project,
Accept() should be put in a separate thread and each new client socket should be handled by a new thread. For more details on creating threads in C++, see my article Synchronized multi-threading in C++ (No MFC!) on CodeProject.com. To create a client socket, call the
Connect() function of the
CSocket class. Here is a short example:
client.Send("GET / HTTP 1.1 \r\n");
Of course, the above code does not send a valid HTTP request, but it demonstrates how a
CSocket object can be used to connect to a running server and communicate with it. There is also a utility class called
CSocket class has an embedded object of
CSocketAddress. You can get a pointer to this object by calling the
GetAddress() function of
CSocket. Using this pointer, you can retrieve useful information about the server that the
CSocket is connected to, like its DSN, aliases (if any), IP address, etc. Also keep in mind that all socket code should be enclosed in a try-catch block that handles
- Created: July 08, 2004
- Updated: November 15, 2007