📄 socket.tex
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Name: socket.tex
%% Purpose: wxSocket docs
%% Author: Guillermo Rodriguez Garcia <guille@iies.es>
%% Modified by:
%% Created: 1999
%% RCS-ID: $Id: socket.tex,v 1.47 2006/03/02 13:59:27 MR 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}\\
\helpref{SetLocal}{wxsocketbasesetlocal}\\
\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).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -