Version: 3.2.5
wxSocketBase Class Reference

#include <wx/socket.h>

+ Inheritance diagram for wxSocketBase:

Detailed Description

wxSocketBase is the base class for all socket-related objects, and it defines all basic IO functionality.

Note
When using wxSocket from multiple threads, even implicitly (e.g. by using wxFTP or wxHTTP in another thread) you must initialize the sockets from the main thread by calling Initialize() before creating the other ones.

Events emitted by this class

The following event handler macros redirect the events to member function handlers 'func' with prototypes like:

void handlerFuncName(wxSocketEvent& event)

Event macros for events emitted by this class:

Library:  wxNet
Category:  Networking
See also
wxSocketEvent, wxSocketClient, wxSocketServer, Sockets Sample, wxSocketFlags, wxSocketEventFlags, wxSocketError

Construction and Destruction

 wxSocketBase ()
 Default constructor. More...
 
virtual ~wxSocketBase ()
 Destructor. More...
 
bool Destroy ()
 Destroys the socket safely. More...
 
static bool Initialize ()
 Perform the initialization needed in order to use the sockets. More...
 
static void Shutdown ()
 Shut down the sockets. More...
 

Public Member Functions

Socket State
bool Error () const
 Returns true if an error occurred in the last IO operation. More...
 
virtual bool GetLocal (wxSockAddress &addr) const
 Return the local address of the socket. More...
 
virtual bool GetPeer (wxSockAddress &addr) const
 Return the peer address field of the socket. More...
 
long GetTimeout () const
 Return the socket timeout in seconds. More...
 
bool IsConnected () const
 Returns true if the socket is connected. More...
 
bool IsData ()
 Check if the socket can be currently read or written. More...
 
bool IsDisconnected () const
 Returns true if the socket is not connected. More...
 
bool IsOk () const
 Returns true if the socket is initialized and ready and false in other cases. More...
 
wxUint32 LastCount () const
 Returns the number of bytes read or written by the last IO call. More...
 
wxUint32 LastReadCount () const
 Returns the number of bytes read by the last Read() or ReadMsg() call (receive direction only). More...
 
wxUint32 LastWriteCount () const
 Returns the number of bytes written by the last Write() or WriteMsg() call (transmit direction only). More...
 
wxSocketError LastError () const
 Returns the last wxSocket error. More...
 
void RestoreState ()
 Restore the previous state of the socket, as saved with SaveState(). More...
 
void SaveState ()
 Save the current state of the socket in a stack. More...
 
Basic I/O
virtual bool Close ()
 Shut down the socket, disabling further transmission and reception of data and disable events for the socket and frees the associated system resources. More...
 
void ShutdownOutput ()
 Shuts down the writing end of the socket. More...
 
wxSocketBaseDiscard ()
 Delete all bytes in the incoming queue. More...
 
wxSocketFlags GetFlags () const
 Returns current IO flags, as set with SetFlags() More...
 
void InterruptWait ()
 Use this function to interrupt any wait operation currently in progress. More...
 
wxSocketBasePeek (void *buffer, wxUint32 nbytes)
 Peek into the socket by copying the next bytes which would be read by Read() into the provided buffer. More...
 
wxSocketBaseRead (void *buffer, wxUint32 nbytes)
 Read up to the given number of bytes from the socket. More...
 
wxSocketBaseReadMsg (void *buffer, wxUint32 nbytes)
 Receive a message sent by WriteMsg(). More...
 
void SetFlags (wxSocketFlags flags)
 Use SetFlags to customize IO operation for this socket. More...
 
virtual bool SetLocal (const wxIPV4address &local)
 Set the local address and port to use. More...
 
void SetTimeout (long seconds)
 Set the default socket timeout in seconds. More...
 
wxSocketBaseUnread (const void *buffer, wxUint32 nbytes)
 Put the specified data into the input queue. More...
 
bool Wait (long seconds=-1, long millisecond=0)
 Wait for any socket event. More...
 
bool WaitForLost (long seconds=-1, long millisecond=0)
 Wait until the connection is lost. More...
 
bool WaitForRead (long seconds=-1, long millisecond=0)
 Wait until the socket is readable. More...
 
bool WaitForWrite (long seconds=-1, long millisecond=0)
 Wait until the socket becomes writable. More...
 
wxSocketBaseWrite (const void *buffer, wxUint32 nbytes)
 Write up to the given number of bytes to the socket. More...
 
wxSocketBaseWriteMsg (const void *buffer, wxUint32 nbytes)
 Sends a buffer which can be read using ReadMsg(). More...
 
Handling Socket Events
void * GetClientData () const
 Returns a pointer of the client data for this socket, as set with SetClientData() More...
 
void Notify (bool notify)
 According to the notify value, this function enables or disables socket events. More...
 
void SetClientData (void *data)
 Sets user-supplied client data for this socket. More...
 
void SetEventHandler (wxEvtHandler &handler, int id=-1)
 Sets an event handler to be called when a socket event occurs. More...
 
void SetNotify (wxSocketEventFlags flags)
 Specifies which socket events are to be sent to the event handler. More...
 
wxSOCKET_T GetSocket () const
 Returns the native socket descriptor. More...
 
- Public Member Functions inherited from wxObject
 wxObject ()
 Default ctor; initializes to NULL the internal reference data. More...
 
 wxObject (const wxObject &other)
 Copy ctor. More...
 
virtual ~wxObject ()
 Destructor. More...
 
virtual wxClassInfoGetClassInfo () const
 This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar). More...
 
wxObjectRefDataGetRefData () const
 Returns the wxObject::m_refData pointer, i.e. the data referenced by this object. More...
 
bool IsKindOf (const wxClassInfo *info) const
 Determines whether this class is a subclass of (or the same class as) the given class. More...
 
bool IsSameAs (const wxObject &obj) const
 Returns true if this object has the same data pointer as obj. More...
 
void Ref (const wxObject &clone)
 Makes this object refer to the data in clone. More...
 
void SetRefData (wxObjectRefData *data)
 Sets the wxObject::m_refData pointer. More...
 
void UnRef ()
 Decrements the reference count in the associated data, and if it is zero, deletes the data. More...
 
void UnShare ()
 This is the same of AllocExclusive() but this method is public. More...
 
void operator delete (void *buf)
 The delete operator is defined for debugging versions of the library only, when the identifier __WXDEBUG__ is defined. More...
 
void * operator new (size_t size, const wxString &filename=NULL, int lineNum=0)
 The new operator is defined for debugging versions of the library only, when the identifier __WXDEBUG__ is defined. More...
 

Additional Inherited Members

- Protected Member Functions inherited from wxObject
void AllocExclusive ()
 Ensure that this object's data is not shared with any other object. More...
 
virtual wxObjectRefDataCreateRefData () const
 Creates a new instance of the wxObjectRefData-derived class specific to this object and returns it. More...
 
virtual wxObjectRefDataCloneRefData (const wxObjectRefData *data) const
 Creates a new instance of the wxObjectRefData-derived class specific to this object and initializes it copying data. More...
 
- Protected Attributes inherited from wxObject
wxObjectRefDatam_refData
 Pointer to an object which is the object's reference-counted data. More...
 

Constructor & Destructor Documentation

◆ wxSocketBase()

wxSocketBase::wxSocketBase ( )

Default constructor.

Don't use it directly; instead, use wxSocketClient to construct a socket client, or wxSocketServer to construct a socket server.

◆ ~wxSocketBase()

virtual wxSocketBase::~wxSocketBase ( )
virtual

Destructor.

Do not destroy a socket using the delete operator directly; use Destroy() instead. Also, do not create socket objects in the stack.

Member Function Documentation

◆ Close()

virtual bool wxSocketBase::Close ( )
virtual

Shut down the socket, disabling further transmission and reception of data and disable events for the socket and frees the associated system resources.

Upon socket destruction, Close() is automatically called, so in most cases you won't need to do it yourself, unless you explicitly want to shut down the socket, typically to notify the peer that you are closing the connection.

Remarks
Although Close() immediately disables events for the socket, it is possible that event messages may be waiting in the application's event queue. The application must therefore be prepared to handle socket event messages even after calling Close().

Reimplemented in wxFTP.

◆ Destroy()

bool wxSocketBase::Destroy ( )

Destroys the socket safely.

Use this function instead of the delete operator, since otherwise socket events could reach the application even after the socket has been destroyed. To prevent this problem, this function appends the wxSocket to a list of object to be deleted on idle time, after all events have been processed. For the same reason, you should avoid creating socket objects in the stack.

Destroy() calls Close() automatically.

Returns
Always true.

◆ Discard()

wxSocketBase& wxSocketBase::Discard ( )

Delete 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.

◆ Error()

bool wxSocketBase::Error ( ) const

Returns true if an error occurred in the last IO operation.

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

◆ GetClientData()

void* wxSocketBase::GetClientData ( ) const

Returns a pointer of the client data for this socket, as set with SetClientData()

◆ GetFlags()

wxSocketFlags wxSocketBase::GetFlags ( ) const

Returns current IO flags, as set with SetFlags()

◆ GetLocal()

virtual bool wxSocketBase::GetLocal ( wxSockAddress addr) const
virtual

Return the local address of the socket.

Returns
true if no error happened, false otherwise.

◆ GetPeer()

virtual bool wxSocketBase::GetPeer ( wxSockAddress addr) const
virtual

Return the peer address field of the socket.

Returns
true if no error happened, false otherwise.

◆ GetSocket()

wxSOCKET_T wxSocketBase::GetSocket ( ) const

Returns the native socket descriptor.

This is intended to use with rarely used specific platform features that can only be accessed via the actual socket descriptor.

Do not use this for reading or writing data from or to the socket as this would almost surely interfere with wxSocket code logic and result in unexpected behaviour.

The socket must be successfully initialized, e.g. connected for client sockets, before this method can be called.

Returns
Returns the native socket descriptor.
Since
2.9.5

◆ GetTimeout()

long wxSocketBase::GetTimeout ( ) const

Return the socket timeout in seconds.

The timeout can be set using SetTimeout() and is 10 minutes by default.

◆ Initialize()

static bool wxSocketBase::Initialize ( )
static

Perform the initialization needed in order to use the sockets.

This function is called from wxSocket constructor implicitly and so normally doesn't need to be called explicitly. There is however one important exception: as this function must be called from the main (UI) thread, if you use wxSocket from multiple threads you must call Initialize() from the main thread before creating wxSocket objects in the other ones.

It is safe to call this function multiple times (only the first call does anything) but you must call Shutdown() exactly once for every call to Initialize().

This function should only be called from the main thread.

Returns
true if the sockets can be used, false if the initialization failed and sockets are not available at all.

◆ InterruptWait()

void wxSocketBase::InterruptWait ( )

Use this function to interrupt any wait operation currently in progress.

Note that this is not intended as a regular way to interrupt a Wait call, but only as an escape mechanism for exceptional situations where it is absolutely necessary to use it, for example to abort an operation due to some exception or abnormal problem. InterruptWait is automatically called when you Close() a socket (and thus also upon socket destruction), so you don't need to use it in these cases.

See also
Wait(), WaitForLost(), WaitForRead(), WaitForWrite(), wxSocketServer::WaitForAccept(), wxSocketClient::WaitOnConnect()

◆ IsConnected()

bool wxSocketBase::IsConnected ( ) const

Returns true if the socket is connected.

◆ IsData()

bool wxSocketBase::IsData ( )

Check if the socket can be currently read or written.

This might mean that queued data is available for reading or, for streamed sockets, that the connection has been closed, so that a read operation will complete immediately without blocking (unless the wxSOCKET_WAITALL flag is set, in which case the operation might still block).

◆ IsDisconnected()

bool wxSocketBase::IsDisconnected ( ) const

Returns true if the socket is not connected.

◆ IsOk()

bool wxSocketBase::IsOk ( ) const

Returns true if the socket is initialized and ready and false in other cases.

Remarks
For wxSocketClient, IsOk() won't return true unless the client is connected to a server. For wxSocketServer, IsOk() will return true if the server could bind to the specified address and is already listening for new connections. IsOk() does not check for IO errors; use Error() instead for that purpose.

◆ LastCount()

wxUint32 wxSocketBase::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: Discard(), Peek(), Read(), ReadMsg(), Unread(), Write(), WriteMsg().

Deprecated:
This function is kept mostly for backwards compatibility. Use LastReadCount() or LastWriteCount() instead. LastCount() is still needed for use with less commonly used functions: Discard(), Peek(), and Unread().

◆ LastError()

wxSocketError wxSocketBase::LastError ( ) const

Returns the last wxSocket error.

See wxSocketError .

Note
This function merely returns the last error code, but it should not be used to determine if an error has occurred (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.

◆ LastReadCount()

wxUint32 wxSocketBase::LastReadCount ( ) const

Returns the number of bytes read by the last Read() or ReadMsg() call (receive direction only).

This function is thread-safe, in case Read() is executed in a different thread than Write(). Use LastReadCount() instead of LastCount() for this reason.

Unlike LastCount(), the functions Discard(), Peek(), and Unread() are currently not supported by LastReadCount().

Since
2.9.5

◆ LastWriteCount()

wxUint32 wxSocketBase::LastWriteCount ( ) const

Returns the number of bytes written by the last Write() or WriteMsg() call (transmit direction only).

This function is thread-safe, in case Write() is executed in a different thread than Read(). Use LastWriteCount() instead of LastCount() for this reason.

Since
2.9.5

◆ Notify()

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

◆ Peek()

wxSocketBase& wxSocketBase::Peek ( void *  buffer,
wxUint32  nbytes 
)

Peek into the socket by copying the next bytes which would be read by Read() into the provided buffer.

Peeking a buffer doesn't delete it from the socket input queue, i.e. calling Read() will return the same data.

Use LastCount() to verify the number of bytes actually peeked.

Use Error() to determine if the operation succeeded.

Parameters
bufferBuffer where to put peeked data.
nbytesNumber of bytes.
Returns
Returns a reference to the current object.
Remarks
The exact behaviour of Peek() depends on the combination of flags being used. For a detailed explanation, see SetFlags()
See also
Error(), LastError(), LastCount(), SetFlags()

◆ Read()

wxSocketBase& wxSocketBase::Read ( void *  buffer,
wxUint32  nbytes 
)

Read up to the given number of bytes from the socket.

Use LastReadCount() to verify the number of bytes actually read. Use Error() to determine if the operation succeeded.

Parameters
bufferBuffer where to put read data.
nbytesNumber of bytes.
Returns
Returns a reference to the current object.
Remarks
The exact behaviour of Read() depends on the combination of flags being used. For a detailed explanation, see SetFlags()
See also
Error(), LastError(), LastReadCount(), SetFlags()

◆ ReadMsg()

wxSocketBase& wxSocketBase::ReadMsg ( void *  buffer,
wxUint32  nbytes 
)

Receive a message sent by WriteMsg().

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 LastReadCount() to verify the number of bytes actually read.

Use Error() to determine if the operation succeeded.

Parameters
bufferBuffer where to put read data.
nbytesSize of the buffer.
Returns
Returns a reference to the current object.
Remarks
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 SetFlags(). For thread safety, in case ReadMsg() and WriteMsg() are called in different threads, it is a good idea to call SetFlags(wxSOCKET_WAITALL|wx_SOCKET_BLOCK) before the first calls to ReadMsg() and WriteMsg() in different threads, as each of these functions will call SetFlags() which performs read/modify/write. By setting these flags before the multi-threading, it will ensure that they don't get reset by thread race conditions.
See also
Error(), LastError(), LastReadCount(), SetFlags(), WriteMsg()

◆ RestoreState()

void wxSocketBase::RestoreState ( )

Restore the previous state of the socket, as saved with SaveState().

Calls to SaveState() and RestoreState() can be nested.

See also
SaveState()

◆ SaveState()

void wxSocketBase::SaveState ( )

Save 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(), user data, as set with SetClientData(). Calls to SaveState and RestoreState can be nested.

See also
RestoreState()

◆ SetClientData()

void wxSocketBase::SetClientData ( void *  data)

Sets user-supplied client data for this socket.

All socket events will contain a pointer to this data, which can be retrieved with the wxSocketEvent::GetClientData() function.

◆ SetEventHandler()

void wxSocketBase::SetEventHandler ( wxEvtHandler handler,
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().

Parameters
handlerSpecifies the event handler you want to use.
idThe id of socket event.
See also
SetNotify(), Notify(), wxSocketEvent, wxEvtHandler

◆ SetFlags()

void wxSocketBase::SetFlags ( wxSocketFlags  flags)

Use SetFlags to customize IO operation for this socket.

The flags parameter may be a combination of flags ORed together. Notice that not all combinations of flags affecting the IO calls (Read() and Write()) make sense, e.g. wxSOCKET_NOWAIT can't be combined with wxSOCKET_WAITALL nor with wxSOCKET_BLOCK.

The following flags can be used:

  • wxSOCKET_NONE:
    Default mode: the socket will read some data in the IO calls and will process events to avoid blocking UI while waiting for the data to become available.
  • wxSOCKET_NOWAIT:
    Don't wait for the socket to become ready in IO calls, read as much data as is available – potentially 0 bytes – and return immediately.
  • wxSOCKET_WAITALL:
    Don't return before the entire amount of data specified in IO calls is read or written unless an error occurs. If this flag is not specified, the IO calls return as soon as any amount of data, even less than the total number of bytes, is processed.
  • wxSOCKET_BLOCK:
    Don't process the UI events while waiting for the socket to become ready. This means that UI will be unresponsive during socket IO.
  • wxSOCKET_REUSEADDR:
    Allows the use of an in-use port (wxServerSocket only).
  • wxSOCKET_BROADCAST:
    Switches the socket to broadcast mode.
  • wxSOCKET_NOBIND:
    Stops the socket from being bound to a specific adapter (normally used in conjunction with wxSOCKET_BROADCAST).

For more information on socket events see wxSocketFlags .

◆ SetLocal()

virtual bool wxSocketBase::SetLocal ( const wxIPV4address local)
virtual

Set the local address and port to use.

This function must always be called for the server sockets but may also be called for client sockets, if it is, bind() is called before connect().

◆ SetNotify()

void wxSocketBase::SetNotify ( wxSocketEventFlags  flags)

Specifies which socket events are to be sent to the event handler.

The flags parameter may be combination of flags ORed together. 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 wxSocketEventFlags .

◆ SetTimeout()

void wxSocketBase::SetTimeout ( long  seconds)

Set 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 timeout is 10 minutes.

◆ Shutdown()

static void wxSocketBase::Shutdown ( )
static

Shut down the sockets.

This function undoes the call to Initialize() and must be called after every successful call to Initialize().

This function should only be called from the main thread, just as Initialize().

◆ ShutdownOutput()

void wxSocketBase::ShutdownOutput ( )

Shuts down the writing end of the socket.

This function simply calls the standard shutdown() function on the underlying socket, indicating that nothing will be written to this socket any more.

◆ Unread()

wxSocketBase& wxSocketBase::Unread ( const void *  buffer,
wxUint32  nbytes 
)

Put the specified data into the input queue.

The data in the buffer will be returned by the next call to Read().

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
bufferBuffer to be unread.
nbytesNumber of bytes.
Returns
Returns a reference to the current object.
See also
Error(), LastCount(), LastError()

◆ Wait()

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

Wait for any socket event.

Possible socket events are:

  • The socket becomes readable.
  • The socket becomes writable.
  • An ongoing connection request has completed (wxSocketClient only)
  • An incoming connection request has arrived (wxSocketServer only)
  • The connection has been closed.

Note that it is recommended to use the individual WaitForXXX() functions to wait for the required condition, instead of this one.

Parameters
secondsNumber of seconds to wait. If -1, it will wait for the default timeout, as set with SetTimeout().
millisecondNumber of milliseconds to wait.
Returns
true when any of the above conditions is satisfied or false if the timeout was reached.
See also
InterruptWait(), wxSocketServer::WaitForAccept(), WaitForLost(), WaitForRead(), WaitForWrite(), wxSocketClient::WaitOnConnect()

◆ WaitForLost()

bool wxSocketBase::WaitForLost ( long  seconds = -1,
long  millisecond = 0 
)

Wait until the connection is lost.

This may happen if the peer gracefully closes the connection or if the connection breaks.

Parameters
secondsNumber of seconds to wait. If -1, it will wait for the default timeout, as set with SetTimeout().
millisecondNumber of milliseconds to wait.
Returns
Returns true if the connection was lost, false if the timeout was reached.
See also
InterruptWait(), Wait()

◆ WaitForRead()

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

Wait until the socket is readable.

This might mean that queued data is available for reading or, for streamed sockets, that the connection has been closed, so that a read operation will complete immediately without blocking (unless the wxSOCKET_WAITALL flag is set, in which case the operation might still block).

Notice that this function should not be called if there is already data available for reading on the socket.

Parameters
secondsNumber of seconds to wait. If -1, it will wait for the default timeout, as set with SetTimeout().
millisecondNumber of milliseconds to wait.
Returns
Returns true if the socket becomes readable, false on timeout.
See also
InterruptWait(), Wait()

◆ WaitForWrite()

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

Wait until the socket becomes writable.

This might mean that the socket is ready to send new data, or for streamed sockets, that the connection has been closed, so that a write operation is guaranteed to complete immediately (unless the wxSOCKET_WAITALL flag is set, in which case the operation might still block).

Notice that this function should not be called if the socket is already writable.

Parameters
secondsNumber of seconds to wait. If -1, it will wait for the default timeout, as set with SetTimeout().
millisecondNumber of milliseconds to wait.
Returns
Returns true if the socket becomes writable, false on timeout.
See also
InterruptWait(), Wait()

◆ Write()

wxSocketBase& wxSocketBase::Write ( const void *  buffer,
wxUint32  nbytes 
)

Write up to the given number of bytes to the socket.

Use LastWriteCount() to verify the number of bytes actually written.

Use Error() to determine if the operation succeeded.

Parameters
bufferBuffer with the data to be sent.
nbytesNumber of bytes.
Returns
Returns a reference to the current object.
Remarks

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

See also
Error(), LastError(), LastWriteCount(), SetFlags()

◆ WriteMsg()

wxSocketBase& wxSocketBase::WriteMsg ( const void *  buffer,
wxUint32  nbytes 
)

Sends a buffer which can be read using ReadMsg().

WriteMsg() sends a short header before the data so that ReadMsg() knows how much data should be actually read.

This function always waits for the entire buffer to be sent, unless an error occurs.

Use LastWriteCount() to verify the number of bytes actually written.

Use Error() to determine if the operation succeeded.

Parameters
bufferBuffer with the data to be sent.
nbytesNumber of bytes to send.
Returns
Returns a reference to the current object.
Remarks

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 SetFlags(). For thread safety, in case ReadMsg() and WriteMsg() are called in different threads, it is a good idea to call

SetFlags(wxSOCKET_WAITALL|wx_SOCKET_BLOCK)
void SetFlags(wxSocketFlags flags)
Use SetFlags to customize IO operation for this socket.
@ wxSOCKET_WAITALL
Wait for all required data to be read/written unless an error occurs.
Definition: socket.h:691

before the first calls to ReadMsg() and WriteMsg() in different threads, as each of these functions calls SetFlags() which performs read/modify/write. By setting these flags before the multi-threading, it will ensure that they don't get reset by thread race conditions.

See also
Error(), LastError(), LastWriteCount(), SetFlags(), ReadMsg()