Derived from
Include files
<wx/socket.h>
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::Ok
wxSocketBase::Error
wxSocketBase::IsConnected
wxSocketBase::IsData
wxSocketBase::IsDisconnected
wxSocketBase::IsNoWait
wxSocketBase::LastCount
wxSocketBase::LastError
wxSocketBase::Peek
wxSocketBase::Read
wxSocketBase::SetFlags
wxSocketBase::Write
wxSocketBase::WriteMsg
wxSocketBase::ReadMsg
wxSocketBase::UnRead
wxSocketBase::Discard
wxSocketBase::Wait
wxSocketBase::WaitForRead
wxSocketBase::WaitForWrite
wxSocketBase::WaitForLost
wxSocketBase::RestoreState
wxSocketBase::SaveState
wxSocketBase::SetEventHandler
wxSocketBase()
Default constructor but don't use it, you must use wxSocketClient or wxSocketServer.
~wxSocketBase()
Destroys the wxSocketBase object.
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.
bool IsConnected() const
Returns TRUE if the socket is connected.
bool IsData() const
Returns TRUE if some data is arrived on the socket.
bool IsDisconnected() const
Returns TRUE if the socket is disconnected.
bool IsNoWait() const
Returns TRUE if the socket mustn't wait.
size_t LastCount() const
Returns the number of bytes read or written by the last IO call.
int LastError() const
Returns an error in the errno format (see your C programmer's guide).
wxSocketBase& Peek(char * buffer, size_t 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.
Parameters
buffer
nbytes
Return value
Returns a reference to the current object.
See also
wxSocketBase::Error
wxSocketBase::LastCount
wxSocketBase::LastError
wxSocketBase& Read(char * buffer, size_t nbytes)
This function reads a buffer of nbytes bytes from the socket.
Parameters
buffer
nbytes
Return value
Returns a reference to the current object.
Remark/Warning
By default, Read uses an internal asynchronous manager: it will send data when the socket requests them. It is particularly interesting when you enter a long data transfer (e.g. a big file, an image, ...). But it is also buggy when you simply discuss with the peer using user data. In this case, wxSocket prepares itself to send data (Write wait for them to be sent) and during a GUI refresh the user enters new data, which involves a new Read call though the previous isn't finished. Well, in most cases it can work but it might fail too. So I advise you to use the SPEED flag, which disables the asynchronous manager, when you just want to discuss with the peer.
This remark is also valid for all IO call.
See also
wxSocketBase::Error, wxSocketBase::LastCount, wxSocketBase::LastError
void SetFlags(wxSockFlags flags)
wxSocketBase::NONE | Normal functionnalities. |
wxSocketBase::NOWAIT | Get the available data in the input queue and exit immediately. |
wxSocketBase::WAITALL | Wait for all required data unless an error occured. |
wxSocketBase::SPEED | Disable the asynchronous IO functionnality. |
wxSocketBase& Write(const char * buffer, size_t nbytes)
This function writes a buffer of nbytes bytes from the socket.
Remark/Warning
By default, Write uses an internal asynchronous manager: it will send data when the socket requests them. It is particularly interesting when you enter a long data transfer (e.g. a big file, an image, ...). But it is also buggy when you simply discuss with the peer using user data. In this case, wxSocket prepares itself to send data (Write wait for them to be sent) and during a GUI refresh the user enters new data, which involves a new Write call though the previous isn't finished. Well, in most cases it can work but it might fail too. So I advise you to use the SPEED flag, which disables the asynchronous manager, when you just want to discuss with the peer.
Parameters
buffer
nbytes
Return value
Returns a reference to the current object.
See also
wxSocketBase::Error
wxSocketBase::LastCount
wxSocketBase::LastError
wxSocketBase& WriteMsg(const char * buffer, size_t 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.
Parameters
buffer
nbytes
Return value
Returns a reference to the current object.
See also
wxSocketBase::Error
wxSocketBase::LastCount
wxSocketBase::LastError
wxSocketBase::ReadMsg
wxSocketBase& ReadMsg(char * buffer, size_t nbytes)
This function reads a buffer sent by WriteMsg on a socket. If the buffer passed to the function isn't big enough, the function filled it and then discard the bytes left. This function always wait for the buffer to be entirely filled.
Parameters
buffer
nbytes
Return value
Returns a reference to the current object.
See also
wxSocketBase::Error
wxSocketBase::LastCount
wxSocketBase::LastError
wxSocketBase::WriteMsg
wxSocketBase& UnRead(const char * buffer, size_t nbytes)
This function unreads a buffer. It means that the buffer is put in the top of the incoming queue. But, it is put also at the end of all unread buffers. It is useful for sockets because we can't seek it.
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.
bool Wait(long seconds = -1, long microsecond = 0)
This function waits for an event: it could be an incoming byte, the possibility for the client to write, a lost connection, an incoming connection, an established connection.
Parameters
seconds
microsecond
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 microsecond = 0)
This function waits for a read event.
Parameters
seconds
microsecond
Return value
Returns TRUE if a byte arrived, FALSE if the timeout was reached.
See also
wxSocketBase::Wait
wxSocketBase::WaitForWrite
wxSocketBase::WaitForLost
bool WaitForWrite(long seconds = -1, long microsecond = 0)
This function waits for a write event.
Parameters
seconds
microsecond
Return value
Returns TRUE if a write event occured, FALSE if the timeout was reached.
See also
wxSocketBase::Wait
wxSocketBase::WaitForRead
wxSocketBase::WaitForLost
bool Wait(long seconds = -1, long microsecond = 0)
This function waits for a "lost" event. For instance, the peer may have closed the connection, or the connection may have been broken.
Parameters
seconds
microsecond
Return value
Returns TRUE if a "lost" event occured, FALSE if the timeout was reached.
See also
wxSocketBase::WaitForRead
wxSocketBase::WaitForWrite
wxSocketBase::WaitForLost
void RestoreState()
This function restores a previously saved state.
See also
void SaveState()
This function saves the current state of the socket object in a stack: actually it saves all flags and the state of the asynchronous callbacks.
See also
void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1)
Sets an event handler to be called when a socket event occured.
Parameters
evt_hdlr
id
See also