class Socket

Encapsulates TCP socket I/O communication. More...

Definition#include <Socket.h>
InheritsQObject
Inherited byClientSocket
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

Public Methods

Signals

Public Members

Protected Methods

Protected Slots

Protected Members


Detailed Description

Encapsulates basic TCP socket communication. It is the base class for the ClientSocket and ServerSocket classes. Socket does not provide methods to connect to an IP/port or to listen on a port for connections. These specific tasks are included in ClientSocket and ServerSocket.

See also: ClientSocket, ServerSocket

Socket ()

Default constructor.

Socket (Socket&)

Copy constructor. A Socket object created with this constructor has the same state as the object it was copied from. The new object also shares the FD of the object it was copied from.

Socket (const int fileDescriptor)

Initialize a socket object from a standard UNIX file descriptor. This use is depreciated, and can cause problems if, for instance, the file descriptor is not valid or open.

void closeSocket ()

Terminates communication on an open socket connection. Calling closeSocket() drops the connection and empties the socket's data buffer. Any data waiting to be read when closeSocket() is called is lost.

PRECONDITIONS: isConnected()

POSTCONDITIONS: !isConnected()

bool isConnected ()
[const]

Determine wether the socket object is currently connected.

Returns: true if the object is in a valid connected state, false otherwise.

int error ()
[const]

Get the current error code of a socket object.

(0) NoError: The socket object has not encountered any errors so far.

(-1) ReadError: An error occured while the object was receiving data.

(-2) WriteError: An error occured while the object was sending data.

(111) ConnRefused: A connection request was refused.

If an error occured, then the socket was disconnected at that moment. Use this method if you want to know what caused of a disconnection.

Returns: Possible values are:

int fileDesc ()

Get the UNIX file descriptor associated with the socket object. This is used mostly for copy purposes; the socket's internal file descriptor should NOT be used externaly!

Returns: The file descriptor of the socket, or 0 if the socket is not connected.

bool sendTo (const QString& data)

Write data contained in a string to an open socket. In case of a write error, this method returns false, error() is set to WriteError, and the socket is disconnected.

PRECONDITIONS: isConnected()

Parameters:
dataThe QString object containing the data to write.

Returns: true if the write operation succeeded, false if a write error occured.

bool sendTo (const char* data)

Write data contained in an old format string to an open socket. In case of a write error, this method returns false, error() is set to WriteError, and the socket is disconnected.

PRECONDITIONS: isConnected()

Parameters:
dataA pointer to the buffer containing the data to write. The buffer must be null-terminated.

Returns: true if the write operation succeeded, false if a write error occured.

bool sendLine (const QString& data)

Write data contained in a string to an open socket. In case of a write error, this method returns false, error() is set to WriteError, and the socket is disconnected.

This method differs from sendTo in the fact that it adds an end-of-line ('\n') character after the data.

PRECONDITIONS: isConnected()

Parameters:
dataThe QString object containing the data to write.

Returns: true if the write operation succeeded, false if a write error occured.

bool sendLine (const char* data)

Write data contained in an old format string to an open socket. In case of a write error, this method returns false, error() is set to WriteError, and the socket is disconnected.

This method differs from sendTo in the fact that it adds an end-of-line ('\n') character after the data.

PRECONDITIONS: isConnected()

Parameters:
dataA pointer to the buffer containing the data to write. The buffer must be null-terminated.

Returns: true if the write operation succeeded, false if a write error occured.

void setFD (const int FD)

manually set the file descriptor for the socket object to use. This method is depreciated.

bool setNonBlocking ()

Set the socket to asynchronous (non blocking) mode. This will make methods like dataWaiting() and read() return immediately, instead of waiting for information to arrive.

This method is platform-dependant, and currently only works on Linux.

bool dataWaiting ()

Read data that is waiting on a socket object. Even if you use blocking sockets, dataWaiting() must be called to actually read the data from the socket. Here is how sockets should be used, no matter if they are blocking or not:


 if (mySocket->dataWaiting()) {
   QString dataRead = mySocket->read();
   cout << dataRead << endl;
 }

Returns: true if there is data to be read on the socket, false otherwise.

QString read ()

Get a string containing the data that is in the socket's buffer. This method should only be called after a successful dataWaiting() call.

Returns: a QString object containing the data the socket has in its buffer.

Socket& operator<< (const QString& data)

Write data contained in a QString to an open socket. After using this operator, you should check if any errors occured. This operator calls sendLine(), meaning that every string sent using it will add a '\n' at the end of the sent data.

PRECONDITIONS: isConnected()

Parameters:
dataA string containing the data to write.

Socket& operator<< (const char* data)

Write data contained in an old format string to an open socket. After using this operator, you should check if any errors occured. This operator calls sendLine(), meaning that every string sent using it will add a '\n' at the end of the sent data.

PRECONDITIONS: isConnected()

Parameters:
dataA pointer to the buffer containing the data to write. The buffer must be null-terminated.

static const int NoError

error() returns this value when no error occured during an operation.

static const int ReadError

error() returns this value when an error occured while the socket was reading data. A socket that had a read error is disconnected.

static const int WriteError

error() returns this value when an error occured while the socket was writing data. A socket that had a write error is disconnected.

static const int ConnRefused

error() returns this value when an attempted connection could not be established. This error is set by ClientSocket 's connectTo() method.