]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/socket.tex
added wxTopLevelWindow::RequestUserAttention(); documented it and implemented it...
[wxWidgets.git] / docs / latex / wx / socket.tex
... / ...
CommitLineData
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2%% Name: socket.tex
3%% Purpose: wxSocket docs
4%% Author: Guillermo Rodriguez Garcia <guille@iies.es>
5%% Modified by:
6%% Created: 1999
7%% RCS-ID: $Id$
8%% Copyright: (c) wxWidgets team
9%% License: wxWidgets license
10%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11
12\section{\class{wxSocketBase}}\label{wxsocketbase}
13
14wxSocketBase is the base class for all socket-related objects, and it
15defines all basic IO functionality.
16
17Note: (Workaround for implementation limitation for wxWidgets up to 2.5.x)
18If you want to use sockets or derived classes such as wxFTP in a secondary thread,
19call wxSocketBase::Initialize() (undocumented) from the main thread before creating
20any sockets - in wxApp::OnInit for example.
21See http://wiki.wxwidgets.org/wiki.pl?WxSocket or
22http://www.litwindow.com/knowhow/knowhow.html for more details.
23
24\wxheading{Derived from}
25
26\helpref{wxObject}{wxobject}
27
28\wxheading{Include files}
29
30<wx/socket.h>
31
32\wxheading{wxSocket errors}
33
34\twocolwidtha{7cm}
35\begin{twocollist}\itemsep=0pt
36\twocolitem{{\bf wxSOCKET\_NOERROR}}{No error happened.}
37\twocolitem{{\bf wxSOCKET\_INVOP}}{Invalid operation.}
38\twocolitem{{\bf wxSOCKET\_IOERR}}{Input/Output error.}
39\twocolitem{{\bf wxSOCKET\_INVADDR}}{Invalid address passed to wxSocket.}
40\twocolitem{{\bf wxSOCKET\_INVSOCK}}{Invalid socket (uninitialized).}
41\twocolitem{{\bf wxSOCKET\_NOHOST}}{No corresponding host.}
42\twocolitem{{\bf wxSOCKET\_INVPORT}}{Invalid port.}
43\twocolitem{{\bf wxSOCKET\_WOULDBLOCK}}{The socket is non-blocking and the operation would block.}
44\twocolitem{{\bf wxSOCKET\_TIMEDOUT}}{The timeout for this operation expired.}
45\twocolitem{{\bf wxSOCKET\_MEMERR}}{Memory exhausted.}
46\end{twocollist}
47
48\wxheading{wxSocket events}
49
50\twocolwidtha{7cm}
51\begin{twocollist}\itemsep=0pt
52\twocolitem{{\bf wxSOCKET\_INPUT}}{There is data available for reading.}
53\twocolitem{{\bf wxSOCKET\_OUTPUT}}{The socket is ready to be written to.}
54\twocolitem{{\bf wxSOCKET\_CONNECTION}}{Incoming connection request (server), or successful connection establishment (client).}
55\twocolitem{{\bf wxSOCKET\_LOST}}{The connection has been closed.}
56\end{twocollist}
57
58A brief note on how to use these events:
59
60The {\bf wxSOCKET\_INPUT} event will be issued whenever there is data
61available for reading. This will be the case if the input queue was
62empty and new data arrives, or if the application has read some data
63yet there is still more data available. This means that the application
64does not need to read all available data in response to a
65{\bf wxSOCKET\_INPUT} event, as more events will be produced as
66necessary.
67
68The {\bf wxSOCKET\_OUTPUT} event is issued when a socket is first
69connected with \helpref{Connect}{wxsocketclientconnect} or accepted
70with \helpref{Accept}{wxsocketserveraccept}. After that, new
71events will be generated only after an output operation fails
72with {\bf wxSOCKET\_WOULDBLOCK} and buffer space becomes available
73again. This means that the application should assume that it
74can write data to the socket until an {\bf wxSOCKET\_WOULDBLOCK}
75error occurs; after this, whenever the socket becomes writable
76again the application will be notified with another
77{\bf wxSOCKET\_OUTPUT} event.
78
79The {\bf wxSOCKET\_CONNECTION} event is issued when a delayed connection
80request completes successfully (client) or when a new connection arrives
81at the incoming queue (server).
82
83The {\bf wxSOCKET\_LOST} event is issued when a close indication is
84received for the socket. This means that the connection broke down or
85that it was closed by the peer. Also, this event will be issued if
86a connection request fails.
87
88\wxheading{Event handling}
89
90To process events coming from a socket object, use the following event
91handler macro to direct events to member functions that take
92a \helpref{wxSocketEvent}{wxsocketevent} argument.
93
94\twocolwidtha{7cm}%
95\begin{twocollist}\itemsep=0pt
96\twocolitem{{\bf EVT\_SOCKET(id, func)}}{Process a wxEVT\_SOCKET event.}
97\end{twocollist}
98
99\wxheading{See also}
100
101\helpref{wxSocketEvent}{wxsocketevent},
102\helpref{wxSocketClient}{wxsocketclient},
103\helpref{wxSocketServer}{wxsocketserver},
104\helpref{Sockets sample}{samplesockets}
105
106% ---------------------------------------------------------------------------
107% Function groups
108% ---------------------------------------------------------------------------
109
110\latexignore{\rtfignore{\wxheading{Function groups}}}
111
112\membersection{Construction and destruction}
113
114\helpref{wxSocketBase}{wxsocketbaseconstruct}\\
115\helpref{\destruct{wxSocketBase}}{wxsocketbasedestruct}\\
116\helpref{Destroy}{wxsocketbasedestroy}
117
118\membersection{Socket state}
119
120Functions to retrieve current state and miscellaneous info.
121
122\helpref{Error}{wxsocketbaseerror}\\
123\helpref{GetLocal}{wxsocketbasegetlocal}\\
124\helpref{GetPeer}{wxsocketbasegetpeer}
125\helpref{IsConnected}{wxsocketbaseisconnected}\\
126\helpref{IsData}{wxsocketbaseisdata}\\
127\helpref{IsDisconnected}{wxsocketbaseisdisconnected}\\
128\helpref{LastCount}{wxsocketbaselastcount}\\
129\helpref{LastError}{wxsocketbaselasterror}\\
130\helpref{Ok}{wxsocketbaseok}\\
131\helpref{SaveState}{wxsocketbasesavestate}\\
132\helpref{RestoreState}{wxsocketbaserestorestate}
133
134\membersection{Basic IO}
135
136Functions that perform basic IO functionality.
137
138\helpref{Close}{wxsocketbaseclose}\\
139\helpref{Discard}{wxsocketbasediscard}\\
140\helpref{Peek}{wxsocketbasepeek}\\
141\helpref{Read}{wxsocketbaseread}\\
142\helpref{ReadMsg}{wxsocketbasereadmsg}\\
143\helpref{Unread}{wxsocketbaseunread}\\
144\helpref{Write}{wxsocketbasewrite}\\
145\helpref{WriteMsg}{wxsocketbasewritemsg}
146
147Functions that perform a timed wait on a certain IO condition.
148
149\helpref{InterruptWait}{wxsocketbaseinterruptwait}\\
150\helpref{Wait}{wxsocketbasewait}\\
151\helpref{WaitForLost}{wxsocketbasewaitforlost}\\
152\helpref{WaitForRead}{wxsocketbasewaitforread}\\
153\helpref{WaitForWrite}{wxsocketbasewaitforwrite}\\
154
155and also:
156
157\helpref{wxSocketServer::WaitForAccept}{wxsocketserverwaitforaccept}\\
158\helpref{wxSocketClient::WaitOnConnect}{wxsocketclientwaitonconnect}
159
160Functions that allow applications to customize socket IO as needed.
161
162\helpref{GetFlags}{wxsocketbasegetflags}\\
163\helpref{SetFlags}{wxsocketbasesetflags}\\
164\helpref{SetTimeout}{wxsocketbasesettimeout}
165
166\membersection{Handling socket events}
167
168Functions that allow applications to receive socket events.
169
170\helpref{Notify}{wxsocketbasenotify}\\
171\helpref{SetNotify}{wxsocketbasesetnotify}\\
172\helpref{GetClientData}{wxsocketbasegetclientdata}\\
173\helpref{SetClientData}{wxsocketbasesetclientdata}\\
174\helpref{SetEventHandler}{wxsocketbaseseteventhandler}
175
176Callback functions are also available, but they are provided for backwards
177compatibility only. Their use is strongly discouraged in favour of events,
178and should be considered deprecated. Callbacks may be unsupported in future
179releases of wxWidgets.
180
181\helpref{Callback}{wxsocketbasecallback}\\
182\helpref{CallbackData}{wxsocketbasecallbackdata}
183
184
185% ---------------------------------------------------------------------------
186% Members here
187% ---------------------------------------------------------------------------
188
189\helponly{\insertatlevel{2}{
190
191\wxheading{Members}
192
193}}
194
195\membersection{wxSocketBase::wxSocketBase}\label{wxsocketbaseconstruct}
196
197\func{}{wxSocketBase}{\void}
198
199Default constructor. Don't use it directly; instead, use
200\helpref{wxSocketClient}{wxsocketclient} to construct a socket client, or
201\helpref{wxSocketServer}{wxsocketserver} to construct a socket server.
202
203\membersection{wxSocketBase::\destruct{wxSocketBase}}\label{wxsocketbasedestruct}
204
205\func{}{\destruct{wxSocketBase}}{\void}
206
207Destructor. Do not destroy a socket using the delete operator directly;
208use \helpref{Destroy}{wxsocketbasedestroy} instead. Also, do not create
209socket objects in the stack.
210
211%
212% Callback
213%
214\membersection{wxSocketBase::Callback}\label{wxsocketbasecallback}
215
216\func{wxSocketBase::wxSockCbk}{Callback}{\param{wxSocketBase::wxSockCbk}{ callback}}
217
218You can setup a callback function to be called when an event occurs.
219The function will be called only for those events for which notification
220has been enabled with \helpref{Notify}{wxsocketbasenotify} and
221\helpref{SetNotify}{wxsocketbasesetnotify}. The prototype of the
222callback must be as follows:
223
224\begin{verbatim}
225void SocketCallback(wxSocketBase& sock, wxSocketNotify evt, char *cdata);
226\end{verbatim}
227
228The first parameter is a reference to the socket object in which the
229event occurred. The second parameter tells you which event occurred.
230(See \helpref{wxSocket events}{wxsocketbase}). The third parameter
231is the user data you specified using \helpref{CallbackData}{wxsocketbasecallbackdata}.
232
233\wxheading{Return value}
234
235A pointer to the previous callback.
236
237\wxheading{Remark/Warning}
238
239Note that callbacks are now deprecated and unsupported, and they remain
240for backwards compatibility only. Use events instead.
241
242\wxheading{See also}
243
244\helpref{wxSocketBase::CallbackData}{wxsocketbasecallbackdata},
245\helpref{wxSocketBase::SetNotify}{wxsocketbasesetnotify},
246\helpref{wxSocketBase::Notify}{wxsocketbasenotify}
247
248%
249% CallbackData
250%
251\membersection{wxSocketBase::CallbackData}\label{wxsocketbasecallbackdata}
252
253\func{char *}{CallbackData}{\param{char *}{cdata}}
254
255This function sets the the user data which will be passed to a
256callback function set via \helpref{Callback}{wxsocketbasecallback}.
257
258\wxheading{Return value}
259
260A pointer to the previous user data.
261
262\wxheading{Remark/Warning}
263
264Note that callbacks are now deprecated and unsupported, and they remain
265for backwards compatibility only. Use events instead.
266
267\wxheading{See also}
268
269\helpref{wxSocketBase::Callback}{wxsocketbasecallback},
270\helpref{wxSocketBase::SetNotify}{wxsocketbasesetnotify},
271\helpref{wxSocketBase::Notify}{wxsocketbasenotify}
272
273%
274% Close
275%
276\membersection{wxSocketBase::Close}\label{wxsocketbaseclose}
277
278\func{void}{Close}{\void}
279
280This function shuts down the socket, disabling further transmission and
281reception of data; it also disables events for the socket and frees the
282associated system resources. Upon socket destruction, Close is automatically
283called, so in most cases you won't need to do it yourself, unless you
284explicitly want to shut down the socket, typically to notify the peer
285that you are closing the connection.
286
287\wxheading{Remark/Warning}
288
289Although Close immediately disables events for the socket, it is possible
290that event messages may be waiting in the application's event queue. The
291application must therefore be prepared to handle socket event messages
292even after calling Close.
293
294%
295% Destroy
296%
297\membersection{wxSocketBase::Destroy}\label{wxsocketbasedestroy}
298
299\func{bool}{Destroy}{\void}
300
301Destroys the socket safely. Use this function instead of the delete operator,
302since otherwise socket events could reach the application even after the
303socket has been destroyed. To prevent this problem, this function appends
304the wxSocket to a list of object to be deleted on idle time, after all
305events have been processed. For the same reason, you should avoid creating
306socket objects in the stack.
307
308Destroy calls \helpref{Close}{wxsocketbaseclose} automatically.
309
310\wxheading{Return value}
311
312Always true.
313
314%
315% Discard
316%
317\membersection{wxSocketBase::Discard}\label{wxsocketbasediscard}
318
319\func{wxSocketBase\&}{Discard}{\void}
320
321This function simply deletes all bytes in the incoming queue. This function
322always returns immediately and its operation is not affected by IO flags.
323
324Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually discarded.
325
326If you use \helpref{Error}{wxsocketbaseerror}, it will always return false.
327
328%
329% Error
330%
331\membersection{wxSocketBase::Error}\label{wxsocketbaseerror}
332
333\constfunc{bool}{Error}{\void}
334
335Returns true if an error occurred in the last IO operation.
336
337Use this function to check for an error condition after one of the
338following calls: Discard, Peek, Read, ReadMsg, Unread, Write, WriteMsg.
339
340%
341% GetClientData
342%
343\membersection{wxSocketBase::GetClientData}\label{wxsocketbasegetclientdata}
344
345\constfunc{void *}{GetClientData}{\void}
346
347Returns a pointer of the client data for this socket, as set with
348\helpref{SetClientData}{wxsocketbasesetclientdata}
349
350%
351% GetLocal
352%
353\membersection{wxSocketBase::GetLocal}\label{wxsocketbasegetlocal}
354
355\constfunc{bool}{GetLocal}{\param{wxSockAddress\& }{addr}}
356
357This function returns the local address field of the socket. The local
358address field contains the complete local address of the socket (local
359address, local port, ...).
360
361\wxheading{Return value}
362
363true if no error happened, false otherwise.
364
365%
366% GetFlags
367%
368\membersection{wxSocketBase::GetFlags}\label{wxsocketbasegetflags}
369
370\constfunc{wxSocketFlags}{GetFlags}{\void}
371
372Returns current IO flags, as set with \helpref{SetFlags}{wxsocketbasesetflags}
373
374%
375% GetPeer
376%
377\membersection{wxSocketBase::GetPeer}\label{wxsocketbasegetpeer}
378
379\constfunc{bool}{GetPeer}{\param{wxSockAddress\& }{addr}}
380
381This function returns the peer address field of the socket. The peer
382address field contains the complete peer host address of the socket
383(address, port, ...).
384
385\wxheading{Return value}
386
387true if no error happened, false otherwise.
388
389%
390% InterruptWait
391%
392\membersection{wxSocketBase::InterruptWait}\label{wxsocketbaseinterruptwait}
393
394\func{void}{InterruptWait}{\void}
395
396Use this function to interrupt any wait operation currently in progress.
397Note that this is not intended as a regular way to interrupt a Wait call,
398but only as an escape mechanism for exceptional situations where it is
399absolutely necessary to use it, for example to abort an operation due to
400some exception or abnormal problem. InterruptWait is automatically called
401when you \helpref{Close}{wxsocketbaseclose} a socket (and thus also upon
402socket destruction), so you don't need to use it in these cases.
403
404\helpref{wxSocketBase::Wait}{wxsocketbasewait},
405\helpref{wxSocketServer::WaitForAccept}{wxsocketserverwaitforaccept},
406\helpref{wxSocketBase::WaitForLost}{wxsocketbasewaitforlost},
407\helpref{wxSocketBase::WaitForRead}{wxsocketbasewaitforread},
408\helpref{wxSocketBase::WaitForWrite}{wxsocketbasewaitforwrite},
409\helpref{wxSocketClient::WaitOnConnect}{wxsocketclientwaitonconnect}
410
411%
412% IsConnected
413%
414\membersection{wxSocketBase::IsConnected}\label{wxsocketbaseisconnected}
415
416\constfunc{bool}{IsConnected}{\void}
417
418Returns true if the socket is connected.
419
420%
421% IsData
422%
423\membersection{wxSocketBase::IsData}\label{wxsocketbaseisdata}
424
425\constfunc{bool}{IsData}{\void}
426
427This function waits until the socket is readable. This might mean that
428queued data is available for reading or, for streamed sockets, that
429the connection has been closed, so that a read operation will complete
430immediately without blocking (unless the {\bf wxSOCKET\_WAITALL} flag
431is set, in which case the operation might still block).
432
433\membersection{wxSocketBase::IsDisconnected}\label{wxsocketbaseisdisconnected}
434
435%
436% IsDisconnected
437%
438\constfunc{bool}{IsDisconnected}{\void}
439
440Returns true if the socket is not connected.
441
442\membersection{wxSocketBase::LastCount}\label{wxsocketbaselastcount}
443
444%
445% LastCount
446%
447\constfunc{wxUint32}{LastCount}{\void}
448
449Returns the number of bytes read or written by the last IO call.
450
451Use this function to get the number of bytes actually transferred
452after using one of the following IO calls: Discard, Peek, Read,
453ReadMsg, Unread, Write, WriteMsg.
454
455%
456% LastError
457%
458\membersection{wxSocketBase::LastError}\label{wxsocketbaselasterror}
459
460\constfunc{wxSocketError}{LastError}{\void}
461
462Returns the last wxSocket error. See \helpref{wxSocket errors}{wxsocketbase}.
463
464Please note that this function merely returns the last error code,
465but it should not be used to determine if an error has occurred (this
466is because successful operations do not change the LastError value).
467Use \helpref{Error}{wxsocketbaseerror} first, in order to determine
468if the last IO call failed. If this returns true, use LastError
469to discover the cause of the error.
470
471%
472% Notify
473%
474\membersection{wxSocketBase::Notify}\label{wxsocketbasenotify}
475
476\func{void}{Notify}{\param{bool}{ notify}}
477
478According to the {\it notify} value, this function enables
479or disables socket events. If {\it notify} is true, the events
480configured with \helpref{SetNotify}{wxsocketbasesetnotify} will
481be sent to the application. If {\it notify} is false; no events
482will be sent.
483
484%
485% Ok
486%
487\membersection{wxSocketBase::Ok}\label{wxsocketbaseok}
488
489\constfunc{bool}{Ok}{\void}
490
491Returns true if the socket is initialized and ready and false in other
492cases.
493
494\wxheading{Remark/Warning}
495
496For \helpref{wxSocketClient}{wxsocketclient}, Ok won't return true unless
497the client is connected to a server.
498
499For \helpref{wxSocketServer}{wxsocketserver}, Ok will return true if the
500server could bind to the specified address and is already listening for
501new connections.
502
503Ok does not check for IO errors;
504use \helpref{Error}{wxsocketbaseerror} instead for that purpose.
505
506%
507% RestoreState
508%
509\membersection{wxSocketBase::RestoreState}\label{wxsocketbaserestorestate}
510
511\func{void}{RestoreState}{\void}
512
513This function restores the previous state of the socket, as saved
514with \helpref{SaveState}{wxsocketbasesavestate}
515
516Calls to SaveState and RestoreState can be nested.
517
518\wxheading{See also}
519
520\helpref{wxSocketBase::SaveState}{wxsocketbasesavestate}
521
522%
523% SaveState
524%
525\membersection{wxSocketBase::SaveState}\label{wxsocketbasesavestate}
526
527\func{void}{SaveState}{\void}
528
529This function saves the current state of the socket in a stack. Socket
530state includes flags, as set with \helpref{SetFlags}{wxsocketbasesetflags},
531event mask, as set with \helpref{SetNotify}{wxsocketbasesetnotify} and
532\helpref{Notify}{wxsocketbasenotify}, user data, as set with
533\helpref{SetClientData}{wxsocketbasesetclientdata}, and asynchronous
534callback settings, as set with \helpref{Callback}{wxsocketbasecallback}
535and \helpref{CallbackData}{wxsocketbasecallbackdata}.
536
537Calls to SaveState and RestoreState can be nested.
538
539\wxheading{See also}
540
541\helpref{wxSocketBase::RestoreState}{wxsocketbaserestorestate}
542
543%
544% SetClientData
545%
546\membersection{wxSocketBase::SetClientData}\label{wxsocketbasesetclientdata}
547
548\func{void}{SetClientData}{\param{void *}{data}}
549
550Sets user-supplied client data for this socket. All socket events will
551contain a pointer to this data, which can be retrieved with
552the \helpref{wxSocketEvent::GetClientData}{wxsocketeventgetclientdata} function.
553
554%
555% SetEventHandler
556%
557\membersection{wxSocketBase::SetEventHandler}\label{wxsocketbaseseteventhandler}
558
559\func{void}{SetEventHandler}{\param{wxEvtHandler\&}{ handler}, \param{int}{ id = -1}}
560
561Sets an event handler to be called when a socket event occurs. The
562handler will be called for those events for which notification is
563enabled with \helpref{SetNotify}{wxsocketbasesetnotify} and
564\helpref{Notify}{wxsocketbasenotify}.
565
566\wxheading{Parameters}
567
568\docparam{handler}{Specifies the event handler you want to use.}
569
570\docparam{id}{The id of socket event.}
571
572\wxheading{See also}
573
574\helpref{wxSocketBase::SetNotify}{wxsocketbasesetnotify},
575\helpref{wxSocketBase::Notify}{wxsocketbasenotify},
576\helpref{wxSocketEvent}{wxsocketevent},
577\helpref{wxEvtHandler}{wxevthandler}
578
579%
580% SetFlags
581%
582\membersection{wxSocketBase::SetFlags}\label{wxsocketbasesetflags}
583
584\func{void}{SetFlags}{\param{wxSocketFlags}{ flags}}
585
586Use SetFlags to customize IO operation for this socket.
587The {\it flags} parameter may be a combination of flags ORed together.
588The following flags can be used:
589
590\twocolwidtha{7cm}
591\begin{twocollist}\itemsep=0pt
592\twocolitem{{\bf wxSOCKET\_NONE}}{Normal functionality.}
593\twocolitem{{\bf wxSOCKET\_NOWAIT}}{Read/write as much data as possible and return immediately.}
594\twocolitem{{\bf wxSOCKET\_WAITALL}}{Wait for all required data to be read/written unless an error occurs.}
595\twocolitem{{\bf wxSOCKET\_BLOCK}}{Block the GUI (do not yield) while reading/writing data.}
596\twocolitem{{\bf wxSOCKET\_REUSEADDR}}{Allows the use of an in-use port (wxServerSocket only)}
597\end{twocollist}
598
599A brief overview on how to use these flags follows.
600
601If no flag is specified (this is the same as {\bf wxSOCKET\_NONE}),
602IO calls will return after some data has been read or written, even
603when the transfer might not be complete. This is the same as issuing
604exactly one blocking low-level call to recv() or send(). Note
605that {\it blocking} here refers to when the function returns, not
606to whether the GUI blocks during this time.
607
608If {\bf wxSOCKET\_NOWAIT} is specified, IO calls will return immediately.
609Read operations will retrieve only available data. Write operations will
610write as much data as possible, depending on how much space is available
611in the output buffer. This is the same as issuing exactly one nonblocking
612low-level call to recv() or send(). Note that {\it nonblocking} here
613refers to when the function returns, not to whether the GUI blocks during
614this time.
615
616If {\bf wxSOCKET\_WAITALL} is specified, IO calls won't return until ALL
617the data has been read or written (or until an error occurs), blocking if
618necessary, and issuing several low level calls if necessary. This is the
619same as having a loop which makes as many blocking low-level calls to
620recv() or send() as needed so as to transfer all the data. Note
621that {\it blocking} here refers to when the function returns, not
622to whether the GUI blocks during this time.
623
624The {\bf wxSOCKET\_BLOCK} flag controls whether the GUI blocks during
625IO operations. If this flag is specified, the socket will not yield
626during IO calls, so the GUI will remain blocked until the operation
627completes. If it is not used, then the application must take extra
628care to avoid unwanted reentrance.
629
630The {\bf wxSOCKET\_REUSEADDR} flag controls the use of the SO\_REUSEADDR standard
631setsockopt() flag. This flag allows the socket to bind to a port that is already in use.
632This is mostly used on UNIX-based systems to allow rapid starting and stopping of a server -
633otherwise you may have to wait several minutes for the port to become available.
634This option can have suprising platform dependent behavior, check the documentation for
635your platforms implementation of setsockopt().
636
637So:
638
639{\bf wxSOCKET\_NONE} will try to read at least SOME data, no matter how much.
640
641{\bf wxSOCKET\_NOWAIT} will always return immediately, even if it cannot
642read or write ANY data.
643
644{\bf wxSOCKET\_WAITALL} will only return when it has read or written ALL
645the data.
646
647{\bf wxSOCKET\_BLOCK} has nothing to do with the previous flags and
648it controls whether the GUI blocks.
649
650{\bf wxSOCKET\_REUSEADDR} controls special platform-specific behavior for wxServerSocket.
651
652%
653% SetNotify
654%
655\membersection{wxSocketBase::SetNotify}\label{wxsocketbasesetnotify}
656
657\func{void}{SetNotify}{\param{wxSocketEventFlags}{ flags}}
658
659SetNotify specifies which socket events are to be sent to the event handler.
660The {\it flags} parameter may be combination of flags ORed together. The
661following flags can be used:
662
663\twocolwidtha{7cm}
664\begin{twocollist}\itemsep=0pt
665\twocolitem{{\bf wxSOCKET\_INPUT\_FLAG}}{to receive wxSOCKET\_INPUT}
666\twocolitem{{\bf wxSOCKET\_OUTPUT\_FLAG}}{to receive wxSOCKET\_OUTPUT}
667\twocolitem{{\bf wxSOCKET\_CONNECTION\_FLAG}}{to receive wxSOCKET\_CONNECTION}
668\twocolitem{{\bf wxSOCKET\_LOST\_FLAG}}{to receive wxSOCKET\_LOST}
669\end{twocollist}
670
671For example:
672
673\begin{verbatim}
674 sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
675 sock.Notify(true);
676\end{verbatim}
677
678In this example, the user will be notified about incoming socket data and
679whenever the connection is closed.
680
681For more information on socket events see \helpref{wxSocket events}{wxsocketbase}.
682
683%
684% SetTimeout
685%
686\membersection{wxSocketBase::SetTimeout}\label{wxsocketbasesettimeout}
687
688\func{void}{SetTimeout}{\param{int }{seconds}}
689
690This function sets the default socket timeout in seconds. This timeout
691applies to all IO calls, and also to the \helpref{Wait}{wxsocketbasewait} family
692of functions if you don't specify a wait interval. Initially, the default
693timeout is 10 minutes.
694
695%
696% Peek
697%
698\membersection{wxSocketBase::Peek}\label{wxsocketbasepeek}
699
700\func{wxSocketBase\&}{Peek}{\param{void *}{ buffer}, \param{wxUint32}{ nbytes}}
701
702This function peeks a buffer of {\it nbytes} bytes from the socket.
703Peeking a buffer doesn't delete it from the socket input queue.
704
705Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually peeked.
706
707Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.
708
709\wxheading{Parameters}
710
711\docparam{buffer}{Buffer where to put peeked data.}
712
713\docparam{nbytes}{Number of bytes.}
714
715\wxheading{Return value}
716
717Returns a reference to the current object.
718
719\wxheading{Remark/Warning}
720
721The exact behaviour of wxSocketBase::Peek depends on the combination
722of flags being used. For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}
723
724\wxheading{See also}
725
726\helpref{wxSocketBase::Error}{wxsocketbaseerror},
727\helpref{wxSocketBase::LastError}{wxsocketbaselasterror},
728\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount},
729\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}
730
731%
732% Read
733%
734\membersection{wxSocketBase::Read}\label{wxsocketbaseread}
735
736\func{wxSocketBase\&}{Read}{\param{void *}{ buffer}, \param{wxUint32}{ nbytes}}
737
738This function reads a buffer of {\it nbytes} bytes from the socket.
739
740Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually read.
741
742Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.
743
744\wxheading{Parameters}
745
746\docparam{buffer}{Buffer where to put read data.}
747
748\docparam{nbytes}{Number of bytes.}
749
750\wxheading{Return value}
751
752Returns a reference to the current object.
753
754\wxheading{Remark/Warning}
755
756The exact behaviour of wxSocketBase::Read depends on the combination
757of flags being used. For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}.
758
759\wxheading{See also}
760
761\helpref{wxSocketBase::Error}{wxsocketbaseerror},
762\helpref{wxSocketBase::LastError}{wxsocketbaselasterror},
763\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount},
764\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}
765
766%
767% ReadMsg
768%
769\membersection{wxSocketBase::ReadMsg}\label{wxsocketbasereadmsg}
770
771\func{wxSocketBase\&}{ReadMsg}{\param{void *}{ buffer}, \param{wxUint32}{ nbytes}}
772
773This function reads a buffer sent by \helpref{WriteMsg}{wxsocketbasewritemsg}
774on a socket. If the buffer passed to the function isn't big enough, the
775remaining bytes will be discarded. This function always waits for the
776buffer to be entirely filled, unless an error occurs.
777
778Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually read.
779
780Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.
781
782\wxheading{Parameters}
783
784\docparam{buffer}{Buffer where to put read data.}
785
786\docparam{nbytes}{Size of the buffer.}
787
788\wxheading{Return value}
789
790Returns a reference to the current object.
791
792\wxheading{Remark/Warning}
793
794wxSocketBase::ReadMsg will behave as if the {\bf wxSOCKET\_WAITALL} flag
795was always set and it will always ignore the {\bf wxSOCKET\_NOWAIT} flag.
796The exact behaviour of ReadMsg depends on the {\bf wxSOCKET\_BLOCK} flag.
797For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}.
798
799\wxheading{See also}
800
801\helpref{wxSocketBase::Error}{wxsocketbaseerror},
802\helpref{wxSocketBase::LastError}{wxsocketbaselasterror},
803\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount},
804\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags},
805\helpref{wxSocketBase::WriteMsg}{wxsocketbasewritemsg}
806
807%
808% Unread
809%
810\membersection{wxSocketBase::Unread}\label{wxsocketbaseunread}
811
812\func{wxSocketBase\&}{Unread}{\param{const void *}{ buffer}, \param{wxUint32}{ nbytes}}
813
814This function unreads a buffer. That is, the data in the buffer is put back
815in the incoming queue. This function is not affected by wxSocket flags.
816
817If you use \helpref{LastCount}{wxsocketbaselastcount}, it will always return {\it nbytes}.
818
819If you use \helpref{Error}{wxsocketbaseerror}, it will always return false.
820
821\wxheading{Parameters}
822
823\docparam{buffer}{Buffer to be unread.}
824
825\docparam{nbytes}{Number of bytes.}
826
827\wxheading{Return value}
828
829Returns a reference to the current object.
830
831\wxheading{See also}
832
833\helpref{wxSocketBase::Error}{wxsocketbaseerror},
834\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount},
835\helpref{wxSocketBase::LastError}{wxsocketbaselasterror}
836
837%
838% Wait
839%
840\membersection{wxSocketBase::Wait}\label{wxsocketbasewait}
841
842\func{bool}{Wait}{\param{long}{ seconds = -1}, \param{long}{ millisecond = 0}}
843
844This function waits until any of the following conditions is true:
845
846\begin{itemize}
847\item The socket becomes readable.
848\item The socket becomes writable.
849\item An ongoing connection request has completed (\helpref{wxSocketClient}{wxsocketclient} only)
850\item An incoming connection request has arrived (\helpref{wxSocketServer}{wxsocketserver} only)
851\item The connection has been closed.
852\end{itemize}
853
854Note that it is recommended to use the individual Wait functions
855to wait for the required condition, instead of this one.
856
857\wxheading{Parameters}
858
859\docparam{seconds}{Number of seconds to wait.
860If -1, it will wait for the default timeout,
861as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}
862
863\docparam{millisecond}{Number of milliseconds to wait.}
864
865\wxheading{Return value}
866
867Returns true when any of the above conditions is satisfied,
868false if the timeout was reached.
869
870\wxheading{See also}
871
872\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait},
873\helpref{wxSocketServer::WaitForAccept}{wxsocketserverwaitforaccept},
874\helpref{wxSocketBase::WaitForLost}{wxsocketbasewaitforlost},
875\helpref{wxSocketBase::WaitForRead}{wxsocketbasewaitforread},
876\helpref{wxSocketBase::WaitForWrite}{wxsocketbasewaitforwrite},
877\helpref{wxSocketClient::WaitOnConnect}{wxsocketclientwaitonconnect}
878
879%
880% WaitForLost
881%
882\membersection{wxSocketBase::WaitForLost}\label{wxsocketbasewaitforlost}
883
884\func{bool}{Wait}{\param{long}{ seconds = -1}, \param{long}{ millisecond = 0}}
885
886This function waits until the connection is lost. This may happen if
887the peer gracefully closes the connection or if the connection breaks.
888
889\wxheading{Parameters}
890
891\docparam{seconds}{Number of seconds to wait.
892If -1, it will wait for the default timeout,
893as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}
894
895\docparam{millisecond}{Number of milliseconds to wait.}
896
897\wxheading{Return value}
898
899Returns true if the connection was lost, false if the timeout was reached.
900
901\wxheading{See also}
902
903\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait},
904\helpref{wxSocketBase::Wait}{wxsocketbasewait}
905
906%
907% WaitForRead
908%
909\membersection{wxSocketBase::WaitForRead}\label{wxsocketbasewaitforread}
910
911\func{bool}{WaitForRead}{\param{long}{ seconds = -1}, \param{long}{ millisecond = 0}}
912
913This function waits until the socket is readable. This might mean that
914queued data is available for reading or, for streamed sockets, that
915the connection has been closed, so that a read operation will complete
916immediately without blocking (unless the {\bf wxSOCKET\_WAITALL} flag
917is set, in which case the operation might still block).
918
919\wxheading{Parameters}
920
921\docparam{seconds}{Number of seconds to wait.
922If -1, it will wait for the default timeout,
923as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}
924
925\docparam{millisecond}{Number of milliseconds to wait.}
926
927\wxheading{Return value}
928
929Returns true if the socket becomes readable, false on timeout.
930
931\wxheading{See also}
932
933\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait},
934\helpref{wxSocketBase::Wait}{wxsocketbasewait}
935
936%
937% WaitForWrite
938%
939\membersection{wxSocketBase::WaitForWrite}\label{wxsocketbasewaitforwrite}
940
941\func{bool}{WaitForWrite}{\param{long}{ seconds = -1}, \param{long}{ millisecond = 0}}
942
943This function waits until the socket becomes writable. This might mean that
944the socket is ready to send new data, or for streamed sockets, that the
945connection has been closed, so that a write operation is guaranteed to
946complete immediately (unless the {\bf wxSOCKET\_WAITALL} flag is set,
947in which case the operation might still block).
948
949\wxheading{Parameters}
950
951\docparam{seconds}{Number of seconds to wait.
952If -1, it will wait for the default timeout,
953as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}
954
955\docparam{millisecond}{Number of milliseconds to wait.}
956
957\wxheading{Return value}
958
959Returns true if the socket becomes writable, false on timeout.
960
961\wxheading{See also}
962
963\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait},
964\helpref{wxSocketBase::Wait}{wxsocketbasewait}
965
966%
967% Write
968%
969\membersection{wxSocketBase::Write}\label{wxsocketbasewrite}
970
971\func{wxSocketBase\&}{Write}{\param{const void *}{ buffer}, \param{wxUint32}{ nbytes}}
972
973This function writes a buffer of {\it nbytes} bytes to the socket.
974
975Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually written.
976
977Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.
978
979\wxheading{Parameters}
980
981\docparam{buffer}{Buffer with the data to be sent.}
982
983\docparam{nbytes}{Number of bytes.}
984
985\wxheading{Return value}
986
987Returns a reference to the current object.
988
989\wxheading{Remark/Warning}
990
991The exact behaviour of wxSocketBase::Write depends on the combination
992of flags being used. For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}.
993
994\wxheading{See also}
995
996\helpref{wxSocketBase::Error}{wxsocketbaseerror},
997\helpref{wxSocketBase::LastError}{wxsocketbaselasterror},
998\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount},
999\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}
1000
1001%
1002% WriteMsg
1003%
1004\membersection{wxSocketBase::WriteMsg}\label{wxsocketbasewritemsg}
1005
1006\func{wxSocketBase\&}{WriteMsg}{\param{const void *}{ buffer}, \param{wxUint32}{ nbytes}}
1007
1008This function writes a buffer of {\it nbytes} bytes from the socket, but it
1009writes a short header before so that \helpref{ReadMsg}{wxsocketbasereadmsg}
1010knows how much data should it actually read. So, a buffer sent with WriteMsg
1011{\bf must} be read with ReadMsg. This function always waits for the entire
1012buffer to be sent, unless an error occurs.
1013
1014Use \helpref{LastCount}{wxsocketbaselastcount} to verify the number of bytes actually written.
1015
1016Use \helpref{Error}{wxsocketbaseerror} to determine if the operation succeeded.
1017
1018\wxheading{Parameters}
1019
1020\docparam{buffer}{Buffer with the data to be sent.}
1021
1022\docparam{nbytes}{Number of bytes to send.}
1023
1024\wxheading{Return value}
1025
1026Returns a reference to the current object.
1027
1028\wxheading{Remark/Warning}
1029
1030wxSocketBase::WriteMsg will behave as if the {\bf wxSOCKET\_WAITALL} flag
1031was always set and it will always ignore the {\bf wxSOCKET\_NOWAIT} flag.
1032The exact behaviour of WriteMsg depends on the {\bf wxSOCKET\_BLOCK} flag.
1033For a detailed explanation, see \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags}.
1034
1035\wxheading{See also}
1036
1037\helpref{wxSocketBase::Error}{wxsocketbaseerror},
1038\helpref{wxSocketBase::LastError}{wxsocketbaselasterror},
1039\helpref{wxSocketBase::LastCount}{wxsocketbaselastcount},
1040\helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags},
1041\helpref{wxSocketBase::ReadMsg}{wxsocketbasereadmsg}
1042
1043
1044% ---------------------------------------------------------------------------
1045% CLASS wxSocketClient
1046% ---------------------------------------------------------------------------
1047
1048\section{\class{wxSocketClient}}\label{wxsocketclient}
1049
1050\wxheading{Derived from}
1051
1052\helpref{wxSocketBase}{wxsocketbase}
1053
1054\wxheading{Include files}
1055
1056<wx/socket.h>
1057
1058\latexignore{\rtfignore{\wxheading{Members}}}
1059
1060% ---------------------------------------------------------------------------
1061% Members
1062% ---------------------------------------------------------------------------
1063%
1064% wxSocketClient
1065%
1066\membersection{wxSocketClient::wxSocketClient}
1067
1068\func{}{wxSocketClient}{\param{wxSocketFlags}{ flags = wxSOCKET\_NONE}}
1069
1070Constructor.
1071
1072\wxheading{Parameters}
1073
1074\docparam{flags}{Socket flags (See \helpref{wxSocketBase::SetFlags}{wxsocketbasesetflags})}
1075
1076%
1077% ~wxSocketClient
1078%
1079\membersection{wxSocketClient::\destruct{wxSocketClient}}
1080
1081\func{}{\destruct{wxSocketClient}}{\void}
1082
1083Destructor. Please see \helpref{wxSocketBase::Destroy}{wxsocketbasedestroy}.
1084
1085%
1086% Connect
1087%
1088\membersection{wxSocketClient::Connect}\label{wxsocketclientconnect}
1089
1090\func{bool}{Connect}{\param{wxSockAddress\&}{ address}, \param{bool}{ wait = true}}
1091
1092Connects to a server using the specified address.
1093
1094If {\it wait} is true, Connect will wait until the connection
1095completes. {\bf Warning:} This will block the GUI.
1096
1097If {\it wait} is false, Connect will try to establish the connection and
1098return immediately, without blocking the GUI. When used this way, even if
1099Connect returns false, the connection request can be completed later.
1100To detect this, use \helpref{WaitOnConnect}{wxsocketclientwaitonconnect},
1101or catch {\bf wxSOCKET\_CONNECTION} events (for successful establishment)
1102and {\bf wxSOCKET\_LOST} events (for connection failure).
1103
1104\wxheading{Parameters}
1105
1106\docparam{address}{Address of the server.}
1107
1108\docparam{wait}{If true, waits for the connection to complete.}
1109
1110\wxheading{Return value}
1111
1112Returns true if the connection is established and no error occurs.
1113
1114If {\it wait} was true, and Connect returns false, an error occurred
1115and the connection failed.
1116
1117If {\it wait} was false, and Connect returns false, you should still
1118be prepared to handle the completion of this connection request, either
1119with \helpref{WaitOnConnect}{wxsocketclientwaitonconnect} or by
1120watching {\bf wxSOCKET\_CONNECTION} and {\bf wxSOCKET\_LOST} events.
1121
1122\wxheading{See also}
1123
1124\helpref{wxSocketClient::WaitOnConnect}{wxsocketclientwaitonconnect},
1125\helpref{wxSocketBase::SetNotify}{wxsocketbasesetnotify},
1126\helpref{wxSocketBase::Notify}{wxsocketbasenotify}
1127
1128%
1129% WaitOnConnect
1130%
1131\membersection{wxSocketClient::WaitOnConnect}\label{wxsocketclientwaitonconnect}
1132
1133\func{bool}{WaitOnConnect}{\param{long}{ seconds = -1}, \param{long}{ milliseconds = 0}}
1134
1135Wait until a connection request completes, or until the specified timeout
1136elapses. Use this function after issuing a call
1137to \helpref{Connect}{wxsocketclientconnect} with {\it wait} set to false.
1138
1139\wxheading{Parameters}
1140
1141\docparam{seconds}{Number of seconds to wait.
1142If -1, it will wait for the default timeout,
1143as set with \helpref{SetTimeout}{wxsocketbasesettimeout}.}
1144
1145\docparam{millisecond}{Number of milliseconds to wait.}
1146
1147\wxheading{Return value}
1148
1149WaitOnConnect returns true if the connection request completes. This
1150does not necessarily mean that the connection was successfully established;
1151it might also happen that the connection was refused by the peer. Use
1152\helpref{IsConnected}{wxsocketbaseisconnected} to distinguish between
1153these two situations.
1154
1155If the timeout elapses, WaitOnConnect returns false.
1156
1157These semantics allow code like this:
1158
1159\begin{verbatim}
1160// Issue the connection request
1161client->Connect(addr, false);
1162
1163// Wait until the request completes or until we decide to give up
1164bool waitmore = true;
1165while ( !client->WaitOnConnect(seconds, millis) && waitmore )
1166{
1167 // possibly give some feedback to the user,
1168 // and update waitmore as needed.
1169}
1170bool success = client->IsConnected();
1171\end{verbatim}
1172
1173\wxheading{See also}
1174
1175\helpref{wxSocketClient::Connect}{wxsocketclientconnect},
1176\helpref{wxSocketBase::InterruptWait}{wxsocketbaseinterruptwait},
1177\helpref{wxSocketBase::IsConnected}{wxsocketbaseisconnected}
1178
1179% ---------------------------------------------------------------------------
1180% CLASS: wxSocketEvent
1181% ---------------------------------------------------------------------------
1182\section{\class{wxSocketEvent}}\label{wxsocketevent}
1183
1184This event class contains information about socket events.
1185
1186\wxheading{Derived from}
1187
1188\helpref{wxEvent}{wxevent}
1189
1190\wxheading{Include files}
1191
1192<wx/socket.h>
1193
1194\wxheading{Event table macros}
1195
1196To process a socket event, use these event handler macros to direct input
1197to member functions that take a wxSocketEvent argument.
1198
1199\twocolwidtha{7cm}
1200\begin{twocollist}\itemsep=0pt
1201\twocolitem{{\bf EVT\_SOCKET(id, func)}}{Process a socket event, supplying the member function.}
1202\end{twocollist}
1203
1204\wxheading{See also}
1205
1206\helpref{wxSocketBase}{wxsocketbase},
1207\helpref{wxSocketClient}{wxsocketclient},
1208\helpref{wxSocketServer}{wxsocketserver}
1209
1210\latexignore{\rtfignore{\wxheading{Members}}}
1211
1212\membersection{wxSocketEvent::wxSocketEvent}
1213
1214\func{}{wxSocketEvent}{\param{int}{ id = 0}}
1215
1216Constructor.
1217
1218\membersection{wxSocketEvent::GetClientData}\label{wxsocketeventgetclientdata}
1219
1220\func{void *}{GetClientData}{\void}
1221
1222Gets the client data of the socket which generated this event, as
1223set with \helpref{wxSocketBase::SetClientData}{wxsocketbasesetclientdata}.
1224
1225\membersection{wxSocketEvent::GetSocket}\label{wxsocketeventgetsocket}
1226
1227\constfunc{wxSocketBase *}{GetSocket}{\void}
1228
1229Returns the socket object to which this event refers to. This makes
1230it possible to use the same event handler for different sockets.
1231
1232\membersection{wxSocketEvent::GetSocketEvent}\label{wxsocketeventgetsocketevent}
1233
1234\constfunc{wxSocketNotify}{GetSocketEvent}{\void}
1235
1236Returns the socket event type.
1237