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