Derived from
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_INPUT | Some data has arrived to the socket. |
wxSOCKET_OUTPUT | The socket is ready to be written to. |
wxSOCKET_CONNECTION | Incoming connection arrival (server), or connection establishment (client). |
wxSOCKET_LOST | The connection has been closed. |
wxSOCKET_MAX_EVENT | This should never happen but the compiler may complain about it. |
The wxSOCKET_INPUT event will be issued when the incoming queue was empty and new data arrives, but NOT if new data arrives when there was data waiting in the incoming queue.
The wxSOCKET_OUTPUT event is issued when a socket is first connected with Connect or accepted with Accept, and then, only after an output operation fails because the output buffer was full, and buffer space becomes available again.
The wxSOCKET_CONNECTION event is issued when a connection request completes (client) or when a new connection arrives at the pending connections 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.
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. |
wxSocketEvent, wxSocketClient, wxSocketServer
Members
wxSocketBase::wxSocketBase
wxSocketBase::~wxSocketBase
wxSocketBase::SetFlags
wxSocketBase::SetNotify
wxSocketBase::SetTimeout
wxSocketBase::Notify
wxSocketBase::Ok
wxSocketBase::Error
wxSocketBase::IsConnected
wxSocketBase::IsData
wxSocketBase::IsDisconnected
wxSocketBase::IsNoWait
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()
Default constructor. Don't use it; use wxSocketClient or wxSocketServer.
~wxSocketBase()
Destroys the wxSocketBase object.
void SetFlags(wxSocketBase::wxSockFlags flags)
wxSOCKET_NONE | Normal functionnality. |
wxSOCKET_NOWAIT | Get the available data in the input queue and return immediately. |
wxSOCKET_WAITALL | Wait for all required data unless an error occurs. |
wxSOCKET_BLOCK | Block the GUI (do not wxYield) 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 controls whether the GUI blocks during IO operations. If this flag is not used, then the application must take extra care to avoid unwanted reentrance.
So:
wxSOCKET_NONE will try to read 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 control whether the GUI blocks.
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 |
sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);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.
void SetTimeout(int seconds)
This function sets the default socket timeout in seconds. This timeout applies to IO calls and also to Wait functions if you don't specify a wait interval. If you never use SetTimeout, the default timeout will be 10 minutes.
void Notify(bool notify)
Notify will enable (notify is TRUE) or disable (notify is FALSE) the propagation of socket events.
bool Ok() const
Returns TRUE if the socket is initialized and ready and FALSE in other cases.
bool Error() const
Returns TRUE if an error occured in the last IO operation.
The following operations update the Error() status: Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard.
bool IsConnected() const
Returns TRUE if the socket is connected.
bool IsData() const
Returns TRUE if there is data available to be read.
bool IsDisconnected() const
Returns TRUE if the socket is disconnected.
bool IsNoWait() const
Returns TRUE if the socket mustn't wait.
wxUint32 LastCount() const
Returns the number of bytes read or written by the last IO call.
The following operations update the LastCount() value: Read, Write, ReadMsg, WriteMsg, Peek, Unread, Discard.
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 tha LastError value). Use Error, instead of LastError, to determine if the last IO call failed. If Error returns TRUE, use LastError to discover the cause of the error.
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 system socket in-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(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(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(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 can alloc the right size for the buffer. 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(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_SPEED flag. For a detailed explanation, see wxSocketBase::SetFlags.
See also
wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount, wxSocketBase::SetFlags, wxSocketBase::WriteMsg
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()
This function simply deletes all bytes in the incoming queue. This function doesn't wait. That is, it will behave as if the wxSOCKET_NOWAIT flag was set. The wxSOCKET_SPEED and wxSOCKET_WAITALL flags have no effect on this function.
Use LastCount to see the number of bytes discarded.
If you use Error, it will always return FALSE.
bool Wait(long seconds = -1, long millisecond = 0)
This function waits until one of the following conditions is true: there is data available for reading; the output buffer is empty (you can send new data); the connection has been lost; an incoming connection arrived (only for servers); a connection request has completed (only for clients).
Parameters
seconds
millisecond
Return value
Returns TRUE if an event occured, FALSE if the timeout was reached.
See also
wxSocketBase::WaitForRead, wxSocketBase::WaitForWrite, wxSocketBase::WaitForLost
bool WaitForRead(long seconds = -1, long millisecond = 0)
This function waits until there is data available to be read.
Parameters
seconds
millisecond
Return value
Returns TRUE if there is data to be read, FALSE if the timeout was reached.
See also
wxSocketBase::Wait, wxSocketBase::WaitForWrite, wxSocketBase::WaitForLost
bool WaitForWrite(long seconds = -1, long millisecond = 0)
This function waits until you can write to the socket.
Parameters
seconds
millisecond
Return value
Returns TRUE if you can write to the socket, FALSE if the timeout was reached.
See also
wxSocketBase::Wait, wxSocketBase::WaitForRead, wxSocketBase::WaitForLost
bool Wait(long seconds = -1, long millisecond = 0)
This function waits until the connection is lost. This may happen if the peer 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
void RestoreState()
This function restores the previous state of the socket, as saved with SaveState.
Calls to SaveState / RestoreState can be nested.
See also
void SaveState()
This function saves the current state of the socket object in a stack: actually it saves all flags (those set with SetFlags, SetNotify, Notfy) and the state of the asynchronous callbacks (Callback, CallbackData).
Calls to SaveState / RestoreState can be nested.
See also
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.
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.
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 C callback to be called when an event occurs. See Callback and CallbackData.
Parameters
evt_hdlr
id
See also
wxSocketBase::SetNotify, wxSocketBase::Notify, wxSocketEvent, wxEvtHandler, wxSocketBase::Callback, wxSocketBase::CallbackData
wxSocketBase::wxSockCbk Callback(wxSocketBase::wxSockCbk callback)
You can setup a C callback to be called when an event occurs. The callback 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.
Return value
A pointer to the previous callback.
See also
wxSocketBase::CallbackData, wxSocketBase::SetNotify, wxSocketBase::Notify
char * CallbackData(char *cdata)
This function sets the the user data which will be passed to a C callback.
Return value
A pointer to the previous user data.
wxSocketBase::Callback, wxSocketBase::SetNotify, wxSocketBase::Notify