Notes on the
C++ Sockets Library
Setup
Some basic notes:
I've been setting this library up under Visual C++ 6.0, because I needed to integrate it with another project (an EON node) that requires me to use
VisualStudio 6.0. Here's the rundown:
This library is built into the statically liked library Sockets.lib. By default it uses the 'Multithreaded' runtime library, but for many (most?) applications this will need to be changed to Multithreaded DLL (in properties->C/C++->Category 'Code Generation'->'Use runtime library...".
I don't need SSL support, so I undefined the preprocessor definition of HAVE_OPENSSL
I was having a name conflict with widows Mutex stuff, so I defined SOCKETS_NAMESPACE to be Sockets for all of my projects by adding the preprocessor definition SOCKETS_NAMESPACE=Sockets to my project settings for each project.
Overrideable Methods in TcpSocket? :
OnRead?
called when the file descriptor has something to read.
Sample Implementation:
//OnRead of TcpSocket actually reads the data from the socket
//and moves it to the input buffer (ibuf)
TcpSocket::OnRead();
//get number of bytes in input buffer
size_t n = ibuf.GetLength();
if (n > 0)
{
char tmp[RSIZE];// <--- tmp's here
ibuf.Read(tmp,n);
printf("Read %d bytes:\n",n);
for (size_t i = 0; i < n; i++)
{
printf("%c",isprint(tmp[i]) ? tmp[i] : '.');
}
printf("\n");
}
OnRawData?
Used to bypass the input buffer of
OnRead? ()
OnLine?
called when a line has been read.
SetLineProtocol() must be called before starting to read data.
Sample Implementation:
DisplayLineSocket::DisplayLineSocket(ISocketHandler& h) : TcpSocket(h)
{
SetLineProtocol();
}
void DisplayLineSocket::OnLine(const std::string& line)
{
printf("Incoming: %s\n",line.c_str());
}
OnAccept?
Only used when socket is used as a server socket. Called when a connection is established.
Sample Implementation
void StatusSocket::OnAccept()
{
Send("Local hostname : " + Utility::GetLocalHostname() + "\n");
Send("Local address : " + Utility::GetLocalAddress() + "\n");
Send("Number of sockets in list : " + Utility::l2string(Handler().GetCount()) + "\n");
Send("\n");
}
OnConnect?
Called when a socket successfully connects
OnConnectFailed?
Called when a connection attempt fails
OnDelete?
Called before a socket class is deleted by the
SocketHandler?
Overrideable Methods in SocketHandler?
--
SamPreston - 24 Mar 2007