The Unix socket class

class SWUnixSocket
Constructor
bind()
connect()
Typical Use
Server side example
Client side example


SWUnixSocket

Derived (public) from SWBaseSocket.
This class implements Unix sockets. Unix sockets are used to communicate between processes on the same machine efficiently. See man unix(7) for more information.
This class is not available under MS Windows!

Constructor:
SWUnixSocket(block_type block=blocking)
You can specify the blocking mode for this socket, see enum block_type for more information.

Methods:
virtual bool bind(std::string path, SWBaseError *error = NULL)
Bind to a local socket file. The socket file pointed to in "path" will be created if it doesn't already exist. Note that the created socket file not will be deleted after use. Returns true on success. Possible error types: notConnected and fatal.

virtual bool connect(std::string path, SWBaseError *error = NULL)
Connect to an existing local socket file. Returns true on success. Possible error types: notConnected, noResponse, portInUse (socket file in use), notReady and fatal.


Typical Use

Here are the two examples from SWBaseSocket again, now with SWUnixSocket.

Server side example:

#include "SocketW.h"
...
SWUnixSocket listener;
SWUnixSocket *mySocket;

listener.bind("/tmp/socketfile");
listener.listen();

mySocket = (SWUnixSocket *)listener.accept();

// do something with mySocket...
mySocket->sendmsg("Hello Client!");

// disconnect and clean up
mySocket->disconnect();
delete mySocket;


Client side example:

#include "SocketW.h"
...
SWUnixSocket mySocket;

mySocket.connect("/tmp/socketfile");

// do something with mySocket...
string msg = mySocket.recvmsg();

// disconnect
mySocket.disconnect();





Copyright © 2003 Anders Lindström
Last updated 031024