%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Name:        socket.tex
%% Purpose:     wxSocket docs
%% Author:      Guillermo Rodriguez Garcia <guille@iies.es>
%% Modified by:
%% Created:     1999
%% RCS-ID:      $Id: socket.tex,v 1.44 2005/05/31 09:18:02 JS Exp $
%% Copyright:   (c) wxWidgets team
%% License:     wxWindows license
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{\class{wxSocketBase}}\label{wxsocketbase}

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

Note: (Workaround for implementation limitation for wxWidgets up to 2.5.x)
If you want to use sockets or derived classes such as wxFTP in a secondary thread,
call wxSocketBase::Initialize() (undocumented) from the main thread before creating 
any sockets - in wxApp::OnInit for example. 
See http://wiki.wxwidgets.org/wiki.pl?WxSocket or
http://www.litwindow.com/knowhow/knowhow.html for more details.

\wxheading{Derived from}

\helpref{wxObject}{wxobject}

\wxheading{Include files}

<wx/socket.h>

\wxheading{wxSocket errors}

\twocolwidtha{7cm}
\begin{twocollist}\itemsep=0pt
\twocolitem{{\bf wxSOCKET\_NOERROR}}{No error happened.}
\twocolitem{{\bf wxSOCKET\_INVOP}}{Invalid operation.}
\twocolitem{{\bf wxSOCKET\_IOERR}}{Input/Output error.}
\twocolitem{{\bf wxSOCKET\_INVADDR}}{Invalid address passed to wxSocket.}
\twocolitem{{\bf wxSOCKET\_INVSOCK}}{Invalid socket (uninitialized).}
\twocolitem{{\bf wxSOCKET\_NOHOST}}{No corresponding host.}
\twocolitem{{\bf wxSOCKET\_INVPORT}}{Invalid port.}
\twocolitem{{\bf wxSOCKET\_WOULDBLOCK}}{The socket is non-blocking and the operation would block.}
\twocolitem{{\bf wxSOCKET\_TIMEDOUT}}{The timeout for this operation expired.}
\twocolitem{{\bf wxSOCKET\_MEMERR}}{Memory exhausted.}
\end{twocollist}

\wxheading{wxSocket events}

\twocolwidtha{7cm}
\begin{twocollist}\itemsep=0pt
\twocolitem{{\bf wxSOCKET\_INPUT}}{There is data available for reading.}
\twocolitem{{\bf wxSOCKET\_OUTPUT}}{The socket is ready to be written to.}
\twocolitem{{\bf wxSOCKET\_CONNECTION}}{Incoming connection request (server), or successful connection establishment (client).}
\twocolitem{{\bf wxSOCKET\_LOST}}{The connection has been closed.}
\end{twocollist}

A brief note on how to use these events:

The {\bf 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 
{\bf wxSOCKET\_INPUT} event, as more events will be produced as
necessary.

The {\bf wxSOCKET\_OUTPUT} event is issued when a socket is first
connected with \helpref{Connect}{wxsocketclientconnect} or accepted
with \helpref{Accept}{wxsocketserveraccept}. After that, new
events will be generated only after an output operation fails
with {\bf 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 {\bf wxSOCKET\_WOULDBLOCK} 
error occurs; after this, whenever the socket becomes writable
again the application will be notified with another 
{\bf wxSOCKET\_OUTPUT} event.

The {\bf wxSOCKET\_CONNECTION} event is issued when a delayed connection
request completes successfully (client) or when a new connection arrives
at the incoming queue (server).

The {\bf 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 connection request fails.

\wxheading{Event handling}

To process events coming from a socket object, use the following event
handler macro to direct events to member functions that take
a \helpref{wxSocketEvent}{wxsocketevent} argument.

\twocolwidtha{7cm}%
\begin{twocollist}\itemsep=0pt
\twocolitem{{\bf EVT\_SOCKET(id, func)}}{Process a wxEVT\_SOCKET event.}
\end{twocollist}

\wxheading{See also}

\helpref{wxSocketEvent}{wxsocketevent}, 
\helpref{wxSocketClient}{wxsocketclient}, 
\helpref{wxSocketServer}{wxsocketserver}, 
\helpref{Sockets sample}{samplesockets}

% ---------------------------------------------------------------------------
% Function groups
% ---------------------------------------------------------------------------

\latexignore{\rtfignore{\wxheading{Function groups}}}

\membersection{Construction and destruction}\label{socketconstruction}

\helpref{wxSocketBase}{wxsocketbaseconstruct}\\
\helpref{\destruct{wxSocketBase}}{wxsocketbasedestruct}\\
\helpref{Destroy}{wxsocketbasedestroy}

\membersection{Socket state}\label{socketstate}

Functions to retrieve current state and miscellaneous info.

\helpref{Error}{wxsocketbaseerror}\\
\helpref{GetLocal}{wxsocketbasegetlocal}\\
\helpref{GetPeer}{wxsocketbasegetpeer}
\helpref{IsConnected}{wxsocketbaseisconnected}\\
\helpref{IsData}{wxsocketbaseisdata}\\
\helpref{IsDisconnected}{wxsocketbaseisdisconnected}\\
\helpref{LastCount}{wxsocketbaselastcount}\\
\helpref{LastError}{wxsocketbaselasterror}\\
\helpref{Ok}{wxsocketbaseok}\\
\helpref{SaveState}{wxsocketbasesavestate}\\
\helpref{RestoreState}{wxsocketbaserestorestate}

\membersection{Basic IO}\label{socketbasicio}

Functions that perform basic IO functionality.

\helpref{Close}{wxsocketbaseclose}\\
\helpref{Discard}{wxsocketbasediscard}\\
\helpref{Peek}{wxsocketbasepeek}\\
\helpref{Read}{wxsocketbaseread}\\
\helpref{ReadMsg}{wxsocketbasereadmsg}\\
\helpref{Unread}{wxsocketbaseunread}\\
\helpref{Write}{wxsocketbasewrite}\\
\helpref{WriteMsg}{wxsocketbasewritemsg}

Functions that perform a timed wait on a certain IO condition.

\helpref{InterruptWait}{wxsocketbaseinterruptwait}\\
\helpref{Wait}{wxsocketbasewait}\\
\helpref{WaitForLost}{wxsocketbasewaitforlost}\\
\helpref{WaitForRead}{wxsocketbasewaitforread}\\
\helpref{WaitForWrite}{wxsocketbasewaitforwrite}\\

and also:

\helpref{wxSocketServer::WaitForAccept}{wxsocketserverwaitforaccept}\\
\helpref{wxSocketClient::WaitOnConnect}{wxsocketclientwaitonconnect}

Functions that allow applications to customize socket IO as needed.

\helpref{GetFlags}{wxsocketbasegetflags}\\
\helpref{SetFlags}{wxsocketbasesetflags}\\
\helpref{SetTimeout}{wxsocketbasesettimeout}

\membersection{Handling socket events}\label{socketevents}

Functions that allow applications to receive socket events.

\helpref{Notify}{wxsocketbasenotify}\\
\helpref{SetNotify}{wxsocketbasesetnotify}\\
\helpref{GetClientData}{wxsocketbasegetclientdata}\\
\helpref{SetClientData}{wxsocketbasesetclientdata}\\
\helpref{SetEventHandler}{wxsocketbaseseteventhandler}


% ---------------------------------------------------------------------------
% Members here
% ---------------------------------------------------------------------------

\helponly{\insertatlevel{2}{

\wxheading{Members}

}}

\membersection{wxSocketBase::wxSocketBase}\label{wxsocketbaseconstruct}

\func{}{wxSocketBase}{\void}

Default constructor. Don't use it directly; instead, use 
\helpref{wxSocketClient}{wxsocketclient} to construct a socket client, or 
\helpref{wxSocketServer}{wxsocketserver} to construct a socket server.

\membersection{wxSocketBase::\destruct{wxSocketBase}}\label{wxsocketbasedestruct}

\func{}{\destruct{wxSocketBase}}{\void}

Destructor. Do not destroy a socket using the delete operator directly;
use \helpref{Destroy}{wxsocketbasedestroy} instead. Also, do not create
socket objects in the stack.


%
% Close
%
\membersection{wxSocketBase::Close}\label{wxsocketbaseclose}

\func{void}{Close}{\void}

This function shuts down the socket, disabling further transmission and
reception of data; it also disables 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.

\wxheading{Remark/Warning}

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.

%
% Destroy
%
\membersection{wxSocketBase::Destroy}\label{wxsocketbasedestroy}

\func{bool}{Destroy}{\void}

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 \helpref{Close}{wxsocketbaseclose} automatically.

\wxheading{Return value}

Always true.

%
% Discard
%
\membersection{wxSocketBase::Discard}\label{wxsocketbasediscard}

\func{wxSocketBase\&}{Discard}{\void}

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 \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually discarded.

If you use \helpref{Error}{wxsocketbaseerror}, it will always return false.

%
% Error
%
\membersection{wxSocketBase::Error}\label{wxsocketbaseerror}

\constfunc{bool}{Error}{\void}

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
%
\membersection{wxSocketBase::GetClientData}\label{wxsocketbasegetclientdata}

\constfunc{void *}{GetClientData}{\void}

Returns a pointer of the client data for this socket, as set with 
\helpref{SetClientData}{wxsocketbasesetclientdata}

%
% GetLocal
%
\membersection{wxSocketBase::GetLocal}\label{wxsocketbasegetlocal}

\constfunc{bool}{GetLocal}{\param{wxSockAddress\& }{addr}}

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, ...).

\wxheading{Return value}

true if no error happened, false otherwise.

%
% GetFlags
%
\membersection{wxSocketBase::GetFlags}\label{wxsocketbasegetflags}

\constfunc{wxSocketFlags}{GetFlags}{\void}

Returns current IO flags, as set with \helpref{SetFlags}{wxsocketbasesetflags}

%
% GetPeer
%
\membersection{wxSocketBase::GetPeer}\label{wxsocketbasegetpeer}

\constfunc{bool}{GetPeer}{\param{wxSockAddress\& }{addr}}

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, ...).

\wxheading{Return value}

true if no error happened, false otherwise.

%
% InterruptWait
%
\membersection{wxSocketBase::InterruptWait}\label{wxsocketbaseinterruptwait}

\func{void}{InterruptWait}{\void}

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 \helpref{Close}{wxsocketbaseclose} a socket (and thus also upon
socket destruction), so you don't need to use it in these cases.

\helpref{wxSocketBase::Wait}{wxsocketbasewait}, 
\helpref{wxSocketServer::WaitForAccept}{wxsocketserverwaitforaccept}, 
\helpref{wxSocketBase::WaitForLost}{wxsocketbasewaitforlost}, 
\helpref{wxSocketBase::WaitForRead}{wxsocketbasewaitforread}, 
\helpref{wxSocketBase::WaitForWrite}{wxsocketbasewaitforwrite}, 
\helpref{wxSocketClient::WaitOnConnect}{wxsocketclientwaitonconnect}

%
% IsConnected
%
\membersection{wxSocketBase::IsConnected}\label{wxsocketbaseisconnected}

\constfunc{bool}{IsConnected}{\void}

Returns true if the socket is connected.

%
% IsData
%
\membersection{wxSocketBase::IsData}\label{wxsocketbaseisdata}

\constfunc{bool}{IsData}{\void}

This function waits 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 {\bf wxSOCKET\_WAITALL} flag
is set, in which case the operation might still block).

\membersection{wxSocketBase::IsDisconnected}\label{wxsocketbaseisdisconnected}

%
% IsDisconnected
%
\constfunc{bool}{IsDisconnected}{\void}

Returns true if the socket is not connected.

\membersection{wxSocketBase::LastCount}\label{wxsocketbaselastcount}

%
% LastCount
%
\constfunc{wxUint32}{LastCount}{\void}

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.

%
% LastError
%
\membersection{wxSocketBase::LastError}\label{wxsocketbaselasterror}

\constfunc{wxSocketError}{LastError}{\void}

Returns the last wxSocket error. See \helpref{wxSocket errors}{wxsocketbase}.

Please note that 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 \helpref{Error}{wxsocketbaseerror} first, in order to determine
if the last IO call failed. If this returns true, use LastError
to discover the cause of the error.

%
% Notify
%
\membersection{wxSocketBase::Notify}\label{wxsocketbasenotify}

\func{void}{Notify}{\param{bool}{ notify}}

According to the {\it notify} value, this function enables
or disables socket events. If {\it notify} is true, the events
configured with \helpref{SetNotify}{wxsocketbasesetnotify} will
be sent to the application. If {\it notify} is false; no events
will be sent.

% 
% Ok
%
\membersection{wxSocketBase::Ok}\label{wxsocketbaseok}

\constfunc{bool}{Ok}{\void}

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

\wxheading{Remark/Warning}

For \helpref{wxSocketClient}{wxsocketclient}, Ok won't return true unless
the client is connected to a server.

For \helpref{wxSocketServer}{wxsocketserver}, Ok will return true if the
server could bind to the specified address and is already listening for
new connections.

Ok does not check for IO errors;
use \helpref{Error}{wxsocketbaseerror} instead for that purpose.

%
% RestoreState
%
\membersection{wxSocketBase::RestoreState}\label{wxsocketbaserestorestate}

\func{void}{RestoreState}{\void}

This function restores the previous state of the socket, as saved
with \helpref{SaveState}{wxsocketbasesavestate}

Calls to SaveState and RestoreState can be nested.

\wxheading{See also}

\helpref{wxSocketBase::SaveState}{wxsocketbasesavestate}

%
% SaveState
%
\membersection{wxSocketBase::SaveState}\label{wxsocketbasesavestate}

\func{void}{SaveState}{\void}

This function saves the current state of the socket in a stack. Socket
state includes flags, as set with \helpref{SetFlags}{wxsocketbasesetflags},
event mask, as set with \helpref{SetNotify}{wxsocketbasesetnotify} and 
\helpref{Notify}{wxsocketbasenotify}, user data, as set with 
\helpref{SetClientData}{wxsocketbasesetclientdata}.

Calls to SaveState and RestoreState can be nested.

\wxheading{See also}

\helpref{wxSocketBase::RestoreState}{wxsocketbaserestorestate}

%
% SetClientData
%
\membersection{wxSocketBase::SetClientData}\label{wxsocketbasesetclientdata}

\func{void}{SetClientData}{\param{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 \helpref{wxSocketEvent::GetClientData}{wxsocketeventgetclientdata} function.

%
% SetEventHandler
%
\membersection{wxSocketBase::SetEventHandler}\label{wxsocketbaseseteventhandler}

\func{void}{SetEventHandler}{\param{wxEvtHandler\&}{ handler}, \param{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 \helpref{SetNotify}{wxsocketbasesetnotify} and 
\helpref{Notify}{wxsocketbasenotify}.

\wxheading{Parameters}

\docparam{handler}{Specifies the event handler you want to use.}

\docparam{id}{The id of socket event.}

\wxheading{See also}

\helpref{wxSocketBase::SetNotify}{wxsocketbasesetnotify}, 
\helpref{wxSocketBase::Notify}{wxsocketbasenotify}, 
\helpref{wxSocketEvent}{wxsocketevent}, 
\helpref{wxEvtHandler}{wxevthandler}

%
% SetFlags
%
\membersection{wxSocketBase::SetFlags}\label{wxsocketbasesetflags}

\func{void}{SetFlags}{\param{wxSocketFlags}{ flags}}

Use SetFlags to customize IO operation for this socket.
The {\it flags} parameter may be a combination of flags ORed together.
The following flags can be used:

\twocolwidtha{7cm}
\begin{twocollist}\itemsep=0pt
\twocolitem{{\bf wxSOCKET\_NONE}}{Normal functionality.}
\twocolitem{{\bf wxSOCKET\_NOWAIT}}{Read/write as much data as possible and return immediately.}
\twocolitem{{\bf wxSOCKET\_WAITALL}}{Wait for all required data to be read/written unless an error occurs.}
\twocolitem{{\bf wxSOCKET\_BLOCK}}{Block the GUI (do not yield) while reading/writing data.}
\twocolitem{{\bf wxSOCKET\_REUSEADDR}}{Allows the use of an in-use port (wxServerSocket only)}
\end{twocollist}

A brief overview on how to use these flags follows.

If no flag is specified (this is the same as {\bf 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 {\it blocking} here refers to when the function returns, not
to whether the GUI blocks during this time.

If {\bf 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 {\it nonblocking} here
refers to when the function returns, not to whether the GUI blocks during
this time.

If {\bf 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 {\it blocking} here refers to when the function returns, not
to whether the GUI blocks during this time.

The {\bf 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.

The {\bf wxSOCKET\_REUSEADDR} flag controls the use of the SO\_REUSEADDR standard
setsockopt() flag. This flag allows the socket to bind to a port that is already in use.
This is mostly used on UNIX-based systems to allow rapid starting and stopping of a server - 
otherwise you may have to wait several minutes for the port to become available.
This option can have surprising platform dependent behavior, check the documentation for
your platform's implementation of setsockopt().

So:

{\bf wxSOCKET\_NONE} will try to read at least SOME data, no matter how much.

{\bf wxSOCKET\_NOWAIT} will always return immediately, even if it cannot
read or write ANY data.

{\bf wxSOCKET\_WAITALL} will only return when it has read or written ALL
the data.

{\bf wxSOCKET\_BLOCK} has nothing to do with the previous flags and
it controls whether the GUI blocks.

{\bf wxSOCKET\_REUSEADDR} controls special platform-specific behavior for wxServerSocket.

%
% SetNotify
%
\membersection{wxSocketBase::SetNotify}\label{wxsocketbasesetnotify}

\func{void}{SetNotify}{\param{wxSocketEventFlags}{ flags}}

SetNotify specifies which socket events are to be sent to the event handler.
The {\it flags} parameter may be combination of flags ORed together. The
following flags can be used:

\twocolwidtha{7cm}
\begin{twocollist}\itemsep=0pt
\twocolitem{{\bf wxSOCKET\_INPUT\_FLAG}}{to receive wxSOCKET\_INPUT}
\twocolitem{{\bf wxSOCKET\_OUTPUT\_FLAG}}{to receive wxSOCKET\_OUTPUT}
\twocolitem{{\bf wxSOCKET\_CONNECTION\_FLAG}}{to receive wxSOCKET\_CONNECTION}
\twocolitem{{\bf wxSOCKET\_LOST\_FLAG}}{to receive wxSOCKET\_LOST}
\end{twocollist}

For example:

\begin{verbatim}
  sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
  sock.Notify(true);
\end{verbatim}

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 \helpref{wxSocket events}{wxsocketbase}.

%
% SetTimeout
%
\membersection{wxSocketBase::SetTimeout}\label{wxsocketbasesettimeout}

\func{void}{SetTimeout}{\param{int }{seconds}}

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

%
% Peek
%
\membersection{wxSocketBase::Peek}\label{wxsocketbasepeek}

\func{wxSocketBase\&}{Peek}{\param{void *}{ buffer}, \param{wxUint32}{ nbytes}}

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

Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually peeked.

Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.

\wxheading{Parameters}

\docparam{buffer}{Buffer where to put peeked data.}

\docparam{nbytes}{Number of bytes.}

\wxheading{Return value}

Returns a reference to the current object.

\wxheading{Remark/Warning}

The exact behaviour of wxSocketBase::Peek depends on the combination
of flags being used. For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}

\wxheading{See also}

\helpref{wxSocketBase::Error}{wxsocketbaseerror}, 
\helpref{wxSocketBase::LastError}{wxsocketbaselasterror}, 
\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount}, 
\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}

%
% Read
%
\membersection{wxSocketBase::Read}\label{wxsocketbaseread}

\func{wxSocketBase\&}{Read}{\param{void *}{ buffer}, \param{wxUint32}{ nbytes}}

This function reads a buffer of {\it nbytes} bytes from the socket.

Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually read.

Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.

\wxheading{Parameters}

\docparam{buffer}{Buffer where to put read data.}

\docparam{nbytes}{Number of bytes.}

\wxheading{Return value}

Returns a reference to the current object.

\wxheading{Remark/Warning}

The exact behaviour of wxSocketBase::Read depends on the combination
of flags being used. For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}.

\wxheading{See also}

\helpref{wxSocketBase::Error}{wxsocketbaseerror}, 
\helpref{wxSocketBase::LastError}{wxsocketbaselasterror}, 
\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount}, 
\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}

%
% ReadMsg
%
\membersection{wxSocketBase::ReadMsg}\label{wxsocketbasereadmsg}

\func{wxSocketBase\&}{ReadMsg}{\param{void *}{ buffer}, \param{wxUint32}{ nbytes}}

This function reads a buffer sent by \helpref{WriteMsg}{wxsocketbasewritemsg} 
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 \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually read.

Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.

\wxheading{Parameters}

\docparam{buffer}{Buffer where to put read data.}

\docparam{nbytes}{Size of the buffer.}

\wxheading{Return value}

Returns a reference to the current object.

\wxheading{Remark/Warning}

wxSocketBase::ReadMsg will behave as if the {\bf wxSOCKET\_WAITALL} flag
was always set and it will always ignore the {\bf wxSOCKET\_NOWAIT} flag.
The exact behaviour of ReadMsg depends on the {\bf wxSOCKET\_BLOCK} flag.
For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}.

\wxheading{See also}

\helpref{wxSocketBase::Error}{wxsocketbaseerror}, 
\helpref{wxSocketBase::LastError}{wxsocketbaselasterror}, 
\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount}, 
\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}, 
\helpref{wxSocketBase::WriteMsg}{wxsocketbasewritemsg}

%
% Unread
%
\membersection{wxSocketBase::Unread}\label{wxsocketbaseunread}

\func{wxSocketBase\&}{Unread}{\param{const void *}{ buffer}, \param{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 \helpref{LastCount}{wxsocketbaselastcount}, it will always return {\it nbytes}.

If you use \helpref{Error}{wxsocketbaseerror}, it will always return false.

\wxheading{Parameters}

\docparam{buffer}{Buffer to be unread.}

\docparam{nbytes}{Number of bytes.}

\wxheading{Return value}

Returns a reference to the current object.

\wxheading{See also}

\helpref{wxSocketBase::Error}{wxsocketbaseerror}, 
\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount}, 
\helpref{wxSocketBase::LastError}{wxsocketbaselasterror}

%
% Wait
%
\membersection{wxSocketBase::Wait}\label{wxsocketbasewait}

\func{bool}{Wait}{\param{long}{ seconds = -1}, \param{long}{ millisecond = 0}}

This function waits until any of the following conditions is true:
                                            
\begin{itemize}
\item The socket becomes readable.
\item The socket becomes writable.
\item An ongoing connection request has completed (\helpref{wxSocketClient}{wxsocketclient} only)
\item An incoming connection request has arrived (\helpref{wxSocketServer}{wxsocketserver} only)
\item The connection has been closed.
\end{itemize}

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

\wxheading{Parameters}

\docparam{seconds}{Number of seconds to wait.
If -1, it will wait for the default timeout,
as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}

\docparam{millisecond}{Number of milliseconds to wait.}

\wxheading{Return value}

Returns true when any of the above conditions is satisfied,
false if the timeout was reached.

\wxheading{See also}

\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait}, 
\helpref{wxSocketServer::WaitForAccept}{wxsocketserverwaitforaccept}, 
\helpref{wxSocketBase::WaitForLost}{wxsocketbasewaitforlost}, 
\helpref{wxSocketBase::WaitForRead}{wxsocketbasewaitforread}, 
\helpref{wxSocketBase::WaitForWrite}{wxsocketbasewaitforwrite}, 
\helpref{wxSocketClient::WaitOnConnect}{wxsocketclientwaitonconnect}

%
% WaitForLost
%
\membersection{wxSocketBase::WaitForLost}\label{wxsocketbasewaitforlost}

\func{bool}{Wait}{\param{long}{ seconds = -1}, \param{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.

\wxheading{Parameters}

\docparam{seconds}{Number of seconds to wait.
If -1, it will wait for the default timeout,
as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}

\docparam{millisecond}{Number of milliseconds to wait.}

\wxheading{Return value}

Returns true if the connection was lost, false if the timeout was reached.

\wxheading{See also}

\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait},
\helpref{wxSocketBase::Wait}{wxsocketbasewait}

%
% WaitForRead
%
\membersection{wxSocketBase::WaitForRead}\label{wxsocketbasewaitforread}

\func{bool}{WaitForRead}{\param{long}{ seconds = -1}, \param{long}{ millisecond = 0}}

This function waits 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 {\bf wxSOCKET\_WAITALL} flag
is set, in which case the operation might still block).

\wxheading{Parameters}

\docparam{seconds}{Number of seconds to wait.
If -1, it will wait for the default timeout,
as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}

\docparam{millisecond}{Number of milliseconds to wait.}

\wxheading{Return value}

Returns true if the socket becomes readable, false on timeout.

\wxheading{See also}

\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait}, 
\helpref{wxSocketBase::Wait}{wxsocketbasewait}

%
% WaitForWrite
%
\membersection{wxSocketBase::WaitForWrite}\label{wxsocketbasewaitforwrite}

\func{bool}{WaitForWrite}{\param{long}{ seconds = -1}, \param{long}{ millisecond = 0}}

This function waits 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 {\bf wxSOCKET\_WAITALL} flag is set,
in which case the operation might still block).

\wxheading{Parameters}

\docparam{seconds}{Number of seconds to wait.
If -1, it will wait for the default timeout,
as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}

\docparam{millisecond}{Number of milliseconds to wait.}

\wxheading{Return value}

Returns true if the socket becomes writable, false on timeout.

\wxheading{See also}

\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait}, 
\helpref{wxSocketBase::Wait}{wxsocketbasewait}

%
% Write
%
\membersection{wxSocketBase::Write}\label{wxsocketbasewrite}

\func{wxSocketBase\&}{Write}{\param{const void *}{ buffer}, \param{wxUint32}{ nbytes}}

This function writes a buffer of {\it nbytes} bytes to the socket.

Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually written.

Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.

\wxheading{Parameters}

\docparam{buffer}{Buffer with the data to be sent.}

\docparam{nbytes}{Number of bytes.}

\wxheading{Return value}

Returns a reference to the current object.

\wxheading{Remark/Warning}

The exact behaviour of wxSocketBase::Write depends on the combination
of flags being used. For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}.

\wxheading{See also}

\helpref{wxSocketBase::Error}{wxsocketbaseerror}, 
\helpref{wxSocketBase::LastError}{wxsocketbaselasterror}, 
\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount}, 
\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}

%
% WriteMsg
%
\membersection{wxSocketBase::WriteMsg}\label{wxsocketbasewritemsg}

\func{wxSocketBase\&}{WriteMsg}{\param{const void *}{ buffer}, \param{wxUint32}{ nbytes}}

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

Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually written.

Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.

\wxheading{Parameters}

\docparam{buffer}{Buffer with the data to be sent.}

\docparam{nbytes}{Number of bytes to send.}

\wxheading{Return value}

Returns a reference to the current object.

\wxheading{Remark/Warning}

wxSocketBase::WriteMsg will behave as if the {\bf wxSOCKET\_WAITALL} flag
was always set and it will always ignore the {\bf wxSOCKET\_NOWAIT} flag.
The exact behaviour of WriteMsg depends on the {\bf wxSOCKET\_BLOCK} flag.
For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}.

\wxheading{See also}

\helpref{wxSocketBase::Error}{wxsocketbaseerror}, 
\helpref{wxSocketBase::LastError}{wxsocketbaselasterror}, 
\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount}, 
\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}, 
\helpref{wxSocketBase::ReadMsg}{wxsocketbasereadmsg}


% ---------------------------------------------------------------------------
% CLASS wxSocketClient
% ---------------------------------------------------------------------------

\section{\class{wxSocketClient}}\label{wxsocketclient}

\wxheading{Derived from}

\helpref{wxSocketBase}{wxsocketbase}

\wxheading{Include files}

<wx/socket.h>

\latexignore{\rtfignore{\wxheading{Members}}}

% ---------------------------------------------------------------------------
% Members
% ---------------------------------------------------------------------------
%
% wxSocketClient
%
\membersection{wxSocketClient::wxSocketClient}\label{wxsocketclientctor}

\func{}{wxSocketClient}{\param{wxSocketFlags}{ flags = wxSOCKET\_NONE}}

Constructor.

\wxheading{Parameters}

\docparam{flags}{Socket flags (See \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags})}

%
% ~wxSocketClient
%
\membersection{wxSocketClient::\destruct{wxSocketClient}}\label{wxsocketclientdtor}

\func{}{\destruct{wxSocketClient}}{\void}

Destructor. Please see \helpref{wxSocketBase::Destroy}{wxsocketbasedestroy}.

%
% Connect
%
\membersection{wxSocketClient::Connect}\label{wxsocketclientconnect}

\func{bool}{Connect}{\param{wxSockAddress\&}{ address}, \param{bool}{ wait = true}}

Connects to a server using the specified address.

If {\it wait} is true, Connect will wait until the connection
completes. {\bf Warning:} This will block the GUI.

If {\it wait} is false, Connect will try to establish the connection and
return immediately, without blocking the GUI. When used this way, even if
Connect returns false, the connection request can be completed later.
To detect this, use \helpref{WaitOnConnect}{wxsocketclientwaitonconnect},
or catch {\bf wxSOCKET\_CONNECTION} events (for successful establishment)
and {\bf wxSOCKET\_LOST} events (for connection failure).

\wxheading{Parameters}

\docparam{address}{Address of the server.}

\docparam{wait}{If true, waits for the connection to complete.}

\wxheading{Return value}

Returns true if the connection is established and no error occurs.

If {\it wait} was true, and Connect returns false, an error occurred
and the connection failed.

If {\it wait} was false, and Connect returns false, you should still
be prepared to handle the completion of this connection request, either
with \helpref{WaitOnConnect}{wxsocketclientwaitonconnect} or by
watching {\bf wxSOCKET\_CONNECTION} and {\bf wxSOCKET\_LOST} events.

\wxheading{See also}

\helpref{wxSocketClient::WaitOnConnect}{wxsocketclientwaitonconnect}, 
\helpref{wxSocketBase::SetNotify}{wxsocketbasesetnotify}, 
\helpref{wxSocketBase::Notify}{wxsocketbasenotify}

%
% WaitOnConnect
%
\membersection{wxSocketClient::WaitOnConnect}\label{wxsocketclientwaitonconnect}

\func{bool}{WaitOnConnect}{\param{long}{ seconds = -1}, \param{long}{ milliseconds = 0}}

Wait until a connection request completes, or until the specified timeout
elapses. Use this function after issuing a call
to \helpref{Connect}{wxsocketclientconnect} with {\it wait} set to false.

\wxheading{Parameters}

\docparam{seconds}{Number of seconds to wait.
If -1, it will wait for the default timeout,
as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}

\docparam{millisecond}{Number of milliseconds to wait.}

\wxheading{Return value}

WaitOnConnect returns true if the connection request completes. This
does not necessarily mean that the connection was successfully established;
it might also happen that the connection was refused by the peer. Use 
\helpref{IsConnected}{wxsocketbaseisconnected} to distinguish between
these two situations.

If the timeout elapses, WaitOnConnect returns false.

These semantics allow code like this:

\begin{verbatim}
// Issue the connection request
client->Connect(addr, false);

// Wait until the request completes or until we decide to give up
bool waitmore = true; 
while ( !client->WaitOnConnect(seconds, millis) && waitmore )
{
    // possibly give some feedback to the user,
    // and update waitmore as needed.
}
bool success = client->IsConnected();
\end{verbatim}

\wxheading{See also}

\helpref{wxSocketClient::Connect}{wxsocketclientconnect}, 
\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait}, 
\helpref{wxSocketBase::IsConnected}{wxsocketbaseisconnected}

% ---------------------------------------------------------------------------
% CLASS: wxSocketEvent
% ---------------------------------------------------------------------------
\section{\class{wxSocketEvent}}\label{wxsocketevent}

This event class contains information about socket events.

\wxheading{Derived from}

\helpref{wxEvent}{wxevent}

\wxheading{Include files}

<wx/socket.h>

\wxheading{Event table macros}

To process a socket event, use these event handler macros to direct input
to member functions that take a wxSocketEvent argument.

\twocolwidtha{7cm}
\begin{twocollist}\itemsep=0pt
\twocolitem{{\bf EVT\_SOCKET(id, func)}}{Process a socket event, supplying the member function.}
\end{twocollist}

\wxheading{See also}

\helpref{wxSocketBase}{wxsocketbase}, 
\helpref{wxSocketClient}{wxsocketclient}, 
\helpref{wxSocketServer}{wxsocketserver}

\latexignore{\rtfignore{\wxheading{Members}}}

\membersection{wxSocketEvent::wxSocketEvent}\label{wxsocketeventctor}

\func{}{wxSocketEvent}{\param{int}{ id = 0}}

Constructor.

\membersection{wxSocketEvent::GetClientData}\label{wxsocketeventgetclientdata}

\func{void *}{GetClientData}{\void}

Gets the client data of the socket which generated this event, as
set with \helpref{wxSocketBase::SetClientData}{wxsocketbasesetclientdata}.

\membersection{wxSocketEvent::GetSocket}\label{wxsocketeventgetsocket}

\constfunc{wxSocketBase *}{GetSocket}{\void}

Returns the socket object to which this event refers to. This makes
it possible to use the same event handler for different sockets.

\membersection{wxSocketEvent::GetSocketEvent}\label{wxsocketeventgetsocketevent}

\constfunc{wxSocketNotify}{GetSocketEvent}{\void}

Returns the socket event type.

