Contents Up Previous Next

wxSocketBase

Derived from

wxEvtHandler

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.
See also

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::wxSocketBase

wxSocketBase()

Default constructor but don't use it, you must use wxSocketClient or wxSocketServer.


wxSocketBase::~wxSocketBase

~wxSocketBase()

Destroys the wxSocketBase object.


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.


wxSocketBase::IsConnected

bool IsConnected() const

Returns TRUE if the socket is connected.


wxSocketBase::IsData

bool IsData() const

Returns TRUE if some data is arrived on the socket.


wxSocketBase::IsDisconnected

bool IsDisconnected() const

Returns TRUE if the socket is disconnected.


wxSocketBase::IsNoWait

bool IsNoWait() const

Returns TRUE if the socket mustn't wait.


wxSocketBase::LastCount

size_t LastCount() const

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


wxSocketBase::LastError

int LastError() const

Returns an error in the errno format (see your C programmer's guide).


wxSocketBase::Peek

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

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


wxSocketBase::SetFlags

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

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

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

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

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

wxSocketBase& Discard()

This function simply deletes all bytes in the incoming queue. This function doesn't wait.


wxSocketBase::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


wxSocketBase::WaitForRead

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


wxSocketBase::WaitForWrite

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


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


wxSocketBase::RestoreState

void RestoreState()

This function restores a previously saved state.

See also

wxSocketBase::SaveState


wxSocketBase::SaveState

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

wxSocketBase::RestoreState


wxSocketBase::SetEventHandler

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

wxSocketEvent