Contents Up Previous Next

wxSocketBase

Derived from

wxEvtHandler

Include files

<wx/socket.h>

wxSocket errors

wxSOCKET_NOERROR No error happened.
wxSOCKET_INVOP Invalid operation.
wxSOCKET_IOERR Input/Output error.
wxSOCKET_INVADDR Invalid address passed to wxSocket.
wxSOCKET_INVSOCK Invalid socket (uninitialized).
wxSOCKET_NOHOST No corresponding host.
wxSOCKET_INVPORT Invalid port.
wxSOCKET_WOULDBLOCK The socket is non-blocking and the operation would block.
wxSOCKET_TIMEDOUT The timeout for this operation expired.
wxSOCKET_MEMERR Memory exhausted.

wxSocket events

wxSOCKET_INPUT There is data available for reading.
wxSOCKET_OUTPUT The socket is ready to be written to.
wxSOCKET_CONNECTION Incoming connection (server), or connection establishment (client).
wxSOCKET_LOST The connection has been closed.

A brief note on how to use these events:

The wxSOCKET_INPUT event will be issued whenever there is data available for reading. This will be the case if the input queue was empty and new data arrives, or if the application has read some data yet there is still more data available. This means that the application does not need to read all available data in response to a wxSOCKET_INPUT event, as more events will be produced as necessary.

The wxSOCKET_OUTPUT event is issued when a socket is first connected with Connect or accepted with Accept. After that, new events will be generated only after an output operation fails with wxSOCKET_WOULDBLOCK and buffer space becomes available again. This means that the application should assume that it can write data to the socket until an wxSOCKET_WOULDBLOCK error occurs; after this, whenever the socket becomes writable again the application will be notified with another wxSOCKET_OUTPUT event.

The wxSOCKET_CONNECTION event is issued when a delayed connection request completes succesfully (client) or when a new connection arrives at the incoming queue (server).

The wxSOCKET_LOST event is issued when a close indication is received for the socket. This means that the connection broke down or that it was closed by the peer. Also, this event will be issued if a delayed connection request fails.

Event handling

To process events from a socket, use the following event handler macro to direct input to member functions that take a wxSocketEvent argument.

EVT_SOCKET(id, func) A socket event occured.

See also

wxSocketEvent, wxSocketClient, wxSocketServer, Sockets sample

Members

wxSocketBase::wxSocketBase
wxSocketBase::~wxSocketBase
wxSocketBase::SetFlags
wxSocketBase::SetNotify
wxSocketBase::SetTimeout
wxSocketBase::Notify
wxSocketBase::Ok
wxSocketBase::Error
wxSocketBase::IsConnected
wxSocketBase::IsData
wxSocketBase::IsDisconnected
wxSocketBase::LastCount
wxSocketBase::LastError
wxSocketBase::Peek
wxSocketBase::Read
wxSocketBase::Write
wxSocketBase::WriteMsg
wxSocketBase::ReadMsg
wxSocketBase::Unread
wxSocketBase::Discard
wxSocketBase::Wait
wxSocketBase::WaitForRead
wxSocketBase::WaitForWrite
wxSocketBase::WaitForLost
wxSocketBase::RestoreState
wxSocketBase::SaveState
wxSocketBase::GetLocal
wxSocketBase::GetPeer
wxSocketBase::SetEventHandler
wxSocketBase::Callback
wxSocketBase::CallbackData


wxSocketBase::wxSocketBase

wxSocketBase()

Default constructor. Don't use it; use wxSocketClient or wxSocketServer.


wxSocketBase::~wxSocketBase

~wxSocketBase()

Destructor.


wxSocketBase::SetFlags

void SetFlags(wxSocketBase::wxSockFlags flags)

wxSOCKET_NONE Normal functionality.
wxSOCKET_NOWAIT Read/write as much data as possible and return immediately.
wxSOCKET_WAITALL Wait for all required data to be read/written unless an error occurs.
wxSOCKET_BLOCK Block the GUI (do not yield) while reading/writing data.

A brief overview on how to use these flags follows.

If no flag is specified (this is the same as wxSOCKET_NONE), IO calls will return after some data has been read or written, even when the transfer might not be complete. This is the same as issuing exactly one blocking low-level call to recv() or send(). Note that blocking here refers to when the function returns, not to whether the GUI blocks during this time.

If wxSOCKET_NOWAIT is specified, IO calls will return immediately. Read operations will retrieve only available data. Write operations will write as much data as possible, depending on how much space is available in the output buffer. This is the same as issuing exactly one nonblocking low-level call to recv() or send(). Note that nonblocking here refers to when the function returns, not to whether the GUI blocks during this time.

If wxSOCKET_WAITALL is specified, IO calls won't return until ALL the data has been read or written (or until an error occurs), blocking if necessary, and issuing several low level calls if necessary. This is the same as having a loop which makes as many blocking low-level calls to recv() or send() as needed so as to transfer all the data. Note that blocking here refers to when the function returns, not to whether the GUI blocks during this time.

The wxSOCKET_BLOCK flag controls whether the GUI blocks during IO operations. If this flag is specified, the socket will not yield during IO calls, so the GUI will remain blocked until the operation completes. If it is not used, then the application must take extra care to avoid unwanted reentrance.

So:

wxSOCKET_NONE will try to read at least SOME data, no matter how much.

wxSOCKET_NOWAIT will always return immediately, even if it cannot read or write ANY data.

wxSOCKET_WAITALL will only return when it has read or written ALL the data.

wxSOCKET_BLOCK has nothing to do with the previous flags and it controls whether the GUI blocks.


wxSocketBase::SetNotify

void SetNotify(wxSocketEventFlags flags)

SetNotify specifies which socket events are to be sent to the event handler. The flags parameter is a combination of flags ORed toghether. The following flags can be used:

wxSOCKET_INPUT_FLAG to receive wxSOCKET_INPUT
wxSOCKET_OUTPUT_FLAG to receive wxSOCKET_OUTPUT
wxSOCKET_CONNECTION_FLAG to receive wxSOCKET_CONNECTION
wxSOCKET_LOST_FLAG to receive wxSOCKET_LOST

For example:

  sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
  sock.Notify(TRUE);
In this example, the user will be notified about incoming socket data and whenever the connection is closed.

For more information on socket events see wxSocket events.


wxSocketBase::SetTimeout

void SetTimeout(int seconds)

This function sets the default socket timeout in seconds. This timeout applies to all IO calls, and also to the Wait family of functions if you don't specify a wait interval. Initially, the default is set to 10 minutes.


wxSocketBase::Notify

void Notify(bool notify)

According to the notify value, this function enables or disables socket events. If notify is TRUE, the events configured with SetNotify will be sent to the application. If notify is FALSE; no events will be sent.


wxSocketBase::Ok

bool Ok() const

Returns TRUE if the socket is initialized and ready and FALSE in other cases.


wxSocketBase::Error

bool Error() const

Returns TRUE if an error occured in the last IO operation.

Use this function to check for an error condition after one of the following calls: Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard.


wxSocketBase::IsConnected

bool IsConnected() const

Returns TRUE if the socket is connected.


wxSocketBase::IsData

bool IsData() const

Returns TRUE if there is data available to be read.


wxSocketBase::IsDisconnected

bool IsDisconnected() const

Returns TRUE if the socket is not connected.


wxSocketBase::LastCount

wxUint32 LastCount() const

Returns the number of bytes read or written by the last IO call.

Use this function to get the number of bytes actually transferred after using one of the following IO calls: Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard.


wxSocketBase::LastError

wxSocketError LastError() const

Returns the last wxSocket error. See wxSocket errors.

Please note that this function merely returns the last error code, but it should not be used to determine if an error has occured (this is because successful operations do not change the LastError value). Use Error first, in order to determine if the last IO call failed. If this returns TRUE, use LastError() to discover the cause of the error.


wxSocketBase::Peek

wxSocketBase& Peek(char * buffer, wxUint32 nbytes)

This function peeks a buffer of nbytes bytes from the socket. Peeking a buffer doesn't delete it from the socket input queue.

Use LastCount to verify the number of bytes actually peeked.

Use Error to determine if the operation succeeded.

Parameters

buffer

nbytes

Return value

Returns a reference to the current object.

Remark/Warning

The exact behaviour of wxSocketBase::Peek() depends on the combination of flags being used. For a detailed explanation, see wxSocketBase::SetFlags

See also

wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount, wxSocketBase::SetFlags


wxSocketBase::Read

wxSocketBase& Read(char * buffer, wxUint32 nbytes)

This function reads a buffer of nbytes bytes from the socket.

Use LastCount to verify the number of bytes actually read.

Use Error to determine if the operation succeeded.

Parameters

buffer

nbytes

Return value

Returns a reference to the current object.

Remark/Warning

The exact behaviour of wxSocketBase::Read() depends on the combination of flags being used. For a detailed explanation, see wxSocketBase::SetFlags.

See also

wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount, wxSocketBase::SetFlags


wxSocketBase::Write

wxSocketBase& Write(const char * buffer, wxUint32 nbytes)

This function writes a buffer of nbytes bytes to the socket.

Use LastCount to verify the number of bytes actually written.

Use Error to determine if the operation succeeded.

Parameters

buffer

nbytes

Return value

Returns a reference to the current object.

Remark/Warning

The exact behaviour of wxSocketBase::Write() depends on the combination of flags being used. For a detailed explanation, see wxSocketBase::SetFlags.

See also

wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount, wxSocketBase::SetFlags


wxSocketBase::WriteMsg

wxSocketBase& WriteMsg(const char * buffer, wxUint32 nbytes)

This function writes a buffer of nbytes bytes from the socket, but it writes a short header before so that ReadMsg knows how much data should it actually read. So, a buffer sent with WriteMsg must be read with ReadMsg. This function always waits for the entire buffer to be sent, unless an error occurs.

Use LastCount to verify the number of bytes actually written.

Use Error to determine if the operation succeeded.

Parameters

buffer

nbytes

Return value

Returns a reference to the current object.

Remark/Warning

wxSocketBase::WriteMsg() will behave as if the wxSOCKET_WAITALL flag was always set and it will always ignore the wxSOCKET_NOWAIT flag. The exact behaviour of WriteMsg depends on the wxSOCKET_BLOCK flag. For a detailed explanation, see wxSocketBase::SetFlags.

See also

wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount, wxSocketBase::SetFlags, wxSocketBase::ReadMsg


wxSocketBase::ReadMsg

wxSocketBase& ReadMsg(char * buffer, wxUint32 nbytes)

This function reads a buffer sent by WriteMsg on a socket. If the buffer passed to the function isn't big enough, the remaining bytes will be discarded. This function always waits for the buffer to be entirely filled, unless an error occurs.

Use LastCount to verify the number of bytes actually read.

Use Error to determine if the operation succeeded.

Parameters

buffer

nbytes

Return value

Returns a reference to the current object.

Remark/Warning

wxSocketBase::ReadMsg() will behave as if the wxSOCKET_WAITALL flag was always set and it will always ignore the wxSOCKET_NOWAIT flag. The exact behaviour of ReadMsg depends on the wxSOCKET_BLOCK flag. For a detailed explanation, see wxSocketBase::SetFlags.

See also

wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount, wxSocketBase::SetFlags, wxSocketBase::WriteMsg


wxSocketBase::Unread

wxSocketBase& Unread(const char * buffer, wxUint32 nbytes)

This function unreads a buffer. That is, the data in the buffer is put back in the incoming queue. This function is not affected by wxSocket flags.

If you use LastCount, it will always return nbytes.

If you use Error, it will always return FALSE.

Parameters

buffer

nbytes

Return value

Returns a reference to the current object.

See also

wxSocketBase::Error, wxSocketBase::LastCount, wxSocketBase::LastError


wxSocketBase::Discard

wxSocketBase& Discard()

This function simply deletes all bytes in the incoming queue. This function always returns immediately and its operation is not affected by IO flags.

Use LastCount to verify the number of bytes actually discarded.

If you use Error, it will always return FALSE.


wxSocketBase::Wait

bool Wait(long seconds = -1, long millisecond = 0)

This function waits until one of the following conditions is TRUE. Note that it is recommended to use the individual Wait functions to wait for the required condition, instead of this one.

Parameters

seconds

millisecond

Return value

Returns TRUE when any of the above conditions is satisfied, FALSE if the timeout was reached.

See also

wxSocketBase::WaitForRead, wxSocketBase::WaitForWrite, wxSocketBase::WaitForLost


wxSocketBase::WaitForRead

bool WaitForRead(long seconds = -1, long millisecond = 0)

This function waits until there is data available to be read, or until an error occurs.

Parameters

seconds

millisecond

Return value

Returns TRUE if there is data to be read, FALSE if the timeout was reached or an error occured.

See also

wxSocketBase::Wait, wxSocketBase::WaitForWrite, wxSocketBase::WaitForLost


wxSocketBase::WaitForWrite

bool WaitForWrite(long seconds = -1, long millisecond = 0)

This function waits until the socket is ready to send data, or until an error occurs.

Parameters

seconds

millisecond

Return value

Returns TRUE if you can write to the socket, FALSE if the timeout was reached or an error occured.

See also

wxSocketBase::Wait, wxSocketBase::WaitForRead, wxSocketBase::WaitForLost


wxSocketBase::WaitForLost

bool Wait(long seconds = -1, long millisecond = 0)

This function waits until the connection is lost. This may happen if the peer gracefully closes the connection or if the connection breaks.

Parameters

seconds

millisecond

Return value

Returns TRUE if the connection was lost, FALSE if the timeout was reached.

See also

wxSocketBase::WaitForRead, wxSocketBase::WaitForWrite, wxSocketBase::WaitForLost


wxSocketBase::RestoreState

void RestoreState()

This function restores the previous state of the socket, as saved with SaveState

Calls to SaveState and RestoreState can be nested.

See also

wxSocketBase::SaveState


wxSocketBase::SaveState

void SaveState()

This function saves the current state of the socket in a stack. Socket state includes flags, as set with SetFlags, event mask, as set with SetNotify and Notify, and current settings for the asynchronous callbacks, as set with Callback and CallbackData.

Calls to SaveState and RestoreState can be nested.

See also

wxSocketBase::RestoreState


wxSocketBase::GetLocal

bool GetLocal(wxSockAddress& addr_man) const

This function returns the local address field of the socket. The local address field contains the complete local address of the socket (local address, local port, ...).

Return value

It returns TRUE if no errors happened, FALSE otherwise.


wxSocketBase::GetPeer

bool GetPeer(wxSockAddress& addr_man) const

This function returns the peer address field of the socket. The peer address field contains the complete peer host address of the socket (address, port, ...).

Return value

It returns TRUE if no errors happened, FALSE otherwise.


wxSocketBase::SetEventHandler

void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1)

Sets an event handler to be called when a socket event occurs. The handler will be called for those events for which notification is enabled with SetNotify and Notify.

You can also specify a callback function to be called when an event occurs, although if possible, events should be used instead of callbacks. See Callback and CallbackData.

Parameters

evt_hdlr

id

See also

wxSocketBase::SetNotify, wxSocketBase::Notify, wxSocketEvent, wxEvtHandler, wxSocketBase::Callback, wxSocketBase::CallbackData


wxSocketBase::Callback

wxSocketBase::wxSockCbk Callback(wxSocketBase::wxSockCbk callback)

You can setup a callback function to be called when an event occurs. The function will be called only for those events for which notification has been enabled with Notify and SetNotify. The prototype of the callback must be as follows:

void SocketCallback(wxSocketBase& sock, wxSocketNotify evt, char *cdata);
The first parameter is a reference to the socket object in which the event occured. The second parameter tells you which event occured. (See wxSocket events). The third parameter is the user data you specified using CallbackData.

Note that events are preferred over callbacks where possible.

Return value

A pointer to the previous callback.

See also

wxSocketBase::CallbackData, wxSocketBase::SetNotify, wxSocketBase::Notify


wxSocketBase::CallbackData

char * CallbackData(char *cdata)

This function sets the the user data which will be passed to a callback function set via Callback.

Note that events are preferred over callbacks where possible.

Return value

A pointer to the previous user data.

wxSocketBase::Callback, wxSocketBase::SetNotify, wxSocketBase::Notify