The Inet socket class

class SWInetSocket
Constructor
bind()
connect()
get_peerAddr()
get_peerPort()
get_peerName()
get_hostAddr()
get_hostPort()
get_hostName()
Typical Use
Server side example
Client side example
Network Byte Order
Sending two 32bit integers example


SWInetSocket

Derived (public) from SWBaseSocket.
This class implements TCP/IP sockets. See man ip(7) for more information.

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

Methods:
virtual bool bind(int port, SWBaseError *error = NULL)
virtual bool bind(int port, std::string host, SWBaseError *error = NULL)
Bind to a local network host and port. The socket will bind to any local host if you don't specify a specific host. You can specify port zero to get any free port. Returns true on success. Possible error types: notConnected and fatal.

virtual bool connect(int port, std::string hostname, SWBaseError *error = NULL)
Connect to an local or remote hostname on the specified port. Returns true on success. Possible error types: notConnected, noResponse, portInUse, notReady and fatal.

virtual std::string get_peerAddr(SWBaseError *error = NULL)
Returns a string with the peer IP address in standard numbers-and-dots notation. Returns an empty string on failure. Possible error types: notConnected and fatal.

virtual int get_peerPort(SWBaseError *error = NULL)
Returns the port used on the peer machine. Returns -1 on failure. Possible error types: notConnected and fatal.

virtual std::string get_peerName(SWBaseError *error = NULL)
Returns the hostname used by the peer. Returns an empty string on failure. Possible error types: notConnected and fatal.

virtual std::string get_hostAddr(SWBaseError *error = NULL)
Returns a string with the local IP address in standard numbers-and-dots notation. Returns an empty string on failure. Possible error types: notConnected and fatal

virtual int get_hostPort(SWBaseError *error = NULL)
Returns the port used on the local machine. Returns -1 on failure. Possible error types: notConnected and fatal

virtual std::string get_hostName(SWBaseError *error = NULL)
Returns the hostname of the local machine. Returns an empty string on failure. Possible error types: notConnected and fatal.


Typical Use

Here are the two examples from SWBaseSocket again, now a bit more SWInetSocket specific.

Server side example:

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

listener.bind(5555);  // or do bind(5555, "localhost") if you only
listener.listen();    // want to accept local connections

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

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

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


Client side example:

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

mySocket.connect(5555, "localhost");  // or do connect(80, "slashdot.org")
                                      // if you want to talk to a webserver
// do something with mySocket...
string msg = mySocket.recvmsg();

// disconnect
mySocket.disconnect();


Network Byte Order

On most machines, like ix86, the host byte order is Least Significant Byte first, whereas the network byte order, as used on the Internet, is Most Significant Byte first. This is important if you want to transmit 16bit or 32bit data as apposed to 8bit (byte) data. The methods send() and recv() work with 8bit data so you must transmit your 16bit or 32bit data as bytes in the correct order. Your system provides several functions (in netinet/in.h) to help you do this:
Sending two 32bit integers example:

#include "SocketW.h"
...
SWInetSocket mySocket;
Uint32 buf[2];
...
// fill buf[0] and buf[1] with 32bit data...
...
// convert to network byte order
buf[0] = htonl(buf[0]);
buf[1] = htonl(buf[1]);

// send
mySocket.send( (char *)buf, 8);  // send 2*4 bytes
...

Note that the receiving side needs to know that two 32bit integers where sent so that it can convert back to host byte order.

If you're sure that the host byte order is the same on the two machines then you could ignore this, but your code would not be portable any longer and the network monster might bite you :-).





Copyright © 2003 Anders Lindström
Last updated 031024