1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %% Purpose: wxSocket docs
4 %% Author: Guillermo Rodriguez Garcia <guille@iies.es>
8 %% Copyright: (c) wxWindows team
9 %% Licence: wxWindows licence
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 \section{\class{wxSocketBase
}}\label{wxsocketbase
}
14 wxSocketBase is the base class for all socket-related objects, and it
15 defines all basic IO functionality.
17 \wxheading{Derived from
}
19 \helpref{wxEvtHandler
}{wxevthandler
}
21 \wxheading{Include files
}
25 \wxheading{wxSocket errors
}
28 \begin{twocollist
}\itemsep=
0pt
29 \twocolitem{{\bf wxSOCKET
\_NOERROR}}{No error happened.
}
30 \twocolitem{{\bf wxSOCKET
\_INVOP}}{Invalid operation.
}
31 \twocolitem{{\bf wxSOCKET
\_IOERR}}{Input/Output error.
}
32 \twocolitem{{\bf wxSOCKET
\_INVADDR}}{Invalid address passed to wxSocket.
}
33 \twocolitem{{\bf wxSOCKET
\_INVSOCK}}{Invalid socket (uninitialized).
}
34 \twocolitem{{\bf wxSOCKET
\_NOHOST}}{No corresponding host.
}
35 \twocolitem{{\bf wxSOCKET
\_INVPORT}}{Invalid port.
}
36 \twocolitem{{\bf wxSOCKET
\_WOULDBLOCK}}{The socket is non-blocking and the operation would block.
}
37 \twocolitem{{\bf wxSOCKET
\_TIMEDOUT}}{The timeout for this operation expired.
}
38 \twocolitem{{\bf wxSOCKET
\_MEMERR}}{Memory exhausted.
}
41 \wxheading{wxSocket events
}
44 \begin{twocollist
}\itemsep=
0pt
45 \twocolitem{{\bf wxSOCKET
\_INPUT}}{There is data available for reading.
}
46 \twocolitem{{\bf wxSOCKET
\_OUTPUT}}{The socket is ready to be written to.
}
47 \twocolitem{{\bf wxSOCKET
\_CONNECTION}}{Incoming connection (server), or connection establishment (client).
}
48 \twocolitem{{\bf wxSOCKET
\_LOST}}{The connection has been closed.
}
51 A brief note on how to use these events:
53 The
{\bf wxSOCKET
\_INPUT} event will be issued whenever there is data
54 available for reading. This will be the case if the input queue was
55 empty and new data arrives, or if the application has read some data
56 yet there is still more data available. This means that the application
57 does not need to read all available data in response to a
58 {\bf wxSOCKET
\_INPUT} event, as more events will be produced as
61 The
{\bf wxSOCKET
\_OUTPUT} event is issued when a socket is first
62 connected with
\helpref{Connect
}{wxsocketclientconnect
} or accepted
63 with
\helpref{Accept
}{wxsocketserveraccept
}. After that, new
64 events will be generated only after an output operation fails
65 with
{\bf wxSOCKET
\_WOULDBLOCK} and buffer space becomes available
66 again. This means that the application should assume that it
67 can write data to the socket until an
{\bf wxSOCKET
\_WOULDBLOCK}
68 error occurs; after this, whenever the socket becomes writable
69 again the application will be notified with another
70 {\bf wxSOCKET
\_OUTPUT} event.
72 The
{\bf wxSOCKET
\_CONNECTION} event is issued when a delayed connection
73 request completes succesfully (client) or when a new connection arrives
74 at the incoming queue (server).
76 The
{\bf wxSOCKET
\_LOST} event is issued when a close indication is
77 received for the socket. This means that the connection broke down or
78 that it was closed by the peer. Also, this event will be issued if
79 a delayed connection request fails.
81 \wxheading{Event handling
}
83 To process events coming from a socket object, use the following event
84 handler macro to direct events to member functions that take a
85 \helpref{wxSocketEvent
}{wxsocketevent
} argument.
88 \begin{twocollist
}\itemsep=
0pt
89 \twocolitem{{\bf EVT
\_SOCKET(id, func)
}}{Process a wxEVT
\_SOCKET event.
}
94 \helpref{wxSocketEvent
}{wxsocketevent
},
95 \helpref{wxSocketClient
}{wxsocketclient
},
96 \helpref{wxSocketServer
}{wxsocketserver
},
97 \helpref{Sockets sample
}{samplesockets
}
99 % ---------------------------------------------------------------------------
101 % ---------------------------------------------------------------------------
103 \latexignore{\rtfignore{\wxheading{Function groups
}}}
105 \membersection{Construction and destruction
}
107 \helpref{wxSocketBase
}{wxsocketbaseconstruct
}\\
108 \helpref{\destruct{wxSocketBase
}}{wxsocketbasedestruct
}\\
109 \helpref{wxDestroy
}{wxsocketbasedestroy
}
111 \membersection{Socket state
}
113 Functions to retrieve current state and miscellaneous info.
115 \helpref{Error
}{wxsocketbaseerror
}\\
116 \helpref{GetLocal
}{wxsocketbasegetlocal
}\\
117 \helpref{GetPeer
}{wxsocketbasegetpeer
}
118 \helpref{IsConnected
}{wxsocketbaseisconnected
}\\
119 \helpref{IsData
}{wxsocketbaseisdata
}\\
120 \helpref{IsDisconnected
}{wxsocketbaseisdisconnected
}\\
121 \helpref{LastCount
}{wxsocketbaselastcount
}\\
122 \helpref{LastError
}{wxsocketbaselasterror
}\\
123 \helpref{Ok
}{wxsocketbaseok
}\\
124 \helpref{SaveState
}{wxsocketbasesavestate
}\\
125 \helpref{RestoreState
}{wxsocketbaserestorestate
}
127 \membersection{Basic IO
}
129 Functions that perform basic IO functionality.
131 \helpref{Close
}{wxsocketbaseclose
}\\
132 \helpref{Discard
}{wxsocketbasediscard
}\\
133 \helpref{Peek
}{wxsocketbasepeek
}\\
134 \helpref{Read
}{wxsocketbaseread
}\\
135 \helpref{ReadMsg
}{wxsocketbasereadmsg
}\\
136 \helpref{Unread
}{wxsocketbaseunread
}\\
137 \helpref{Write
}{wxsocketbasewrite
}\\
138 \helpref{WriteMsg
}{wxsocketbasewritemsg
}
140 Functions that perform a timed wait on a certain IO condition.
142 \helpref{Wait
}{wxsocketbasewait
}\\
143 \helpref{WaitForRead
}{wxsocketbasewaitforread
}\\
144 \helpref{WaitForWrite
}{wxsocketbasewaitforwrite
}\\
145 \helpref{WaitForLost
}{wxsocketbasewaitforlost
}
147 Functions that allow applications to customize socket IO as needed.
149 \helpref{SetFlags
}{wxsocketbasesetflags
}\\
150 \helpref{SetTimeout
}{wxsocketbasesettimeout
}
152 \membersection{Handling socket events
}
154 Functions that allow applications to receive socket events.
156 \helpref{Notify
}{wxsocketbasenotify
}\\
157 \helpref{SetNotify
}{wxsocketbasesetnotify
}\\
158 \helpref{SetEventHandler
}{wxsocketbaseseteventhandler
}
160 Callback functions are also available, but they are provided for backwards
161 compatibility only. Their use is discouraged in favour of events, and should
162 be considered deprecated.
164 \helpref{Callback
}{wxsocketbasecallback
}\\
165 \helpref{CallbackData
}{wxsocketbasecallbackdata
}
168 % ---------------------------------------------------------------------------
170 % ---------------------------------------------------------------------------
172 \helponly{\insertatlevel{2}{
178 \membersection{wxSocketBase::wxSocketBase
}\label{wxsocketbaseconstruct
}
180 \func{}{wxSocketBase
}{\void}
182 Default constructor. Don't use it directly; instead, use
183 \helpref{wxSocketClient
}{wxsocketclient
} to construct a socket client, or
184 \helpref{wxSocketServer
}{wxsocketserver
} to construct a socket server.
186 \membersection{wxSocketBase::
\destruct{wxSocketBase
}}\label{wxsocketbasedestruct
}
188 \func{}{\destruct{wxSocketBase
}}{\void}
190 Destructor. Do not destroy a socket using the delete operator directly;
191 use
\helpref{wxsocketbasedestroy
} instead. Also, do not create socket
192 objects in the stack.
197 \membersection{wxSocketBase::Callback
}\label{wxsocketbasecallback
}
199 \func{wxSocketBase::wxSockCbk
}{Callback
}{\param{wxSocketBase::wxSockCbk
}{ callback
}}
201 You can setup a callback function to be called when an event occurs.
202 The function will be called only for those events for which notification
203 has been enabled with
\helpref{Notify
}{wxsocketbasenotify
} and
204 \helpref{SetNotify
}{wxsocketbasesetnotify
}. The prototype of the
205 callback must be as follows:
208 void SocketCallback(wxSocketBase& sock, wxSocketNotify evt, char *cdata);
211 The first parameter is a reference to the socket object in which the
212 event occured. The second parameter tells you which event occured.
213 (See
\helpref{wxSocket events
}{wxsocketbase
}). The third parameter
214 is the user data you specified using
215 \helpref{CallbackData
}{wxsocketbasecallbackdata
}.
217 Note that events are preferred over callbacks where possible.
219 \wxheading{Return value
}
221 A pointer to the previous callback.
225 \helpref{wxSocketBase::CallbackData
}{wxsocketbasecallbackdata
},
226 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
227 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
}
232 \membersection{wxSocketBase::CallbackData
}\label{wxsocketbasecallbackdata
}
234 \func{char *
}{CallbackData
}{\param{char *
}{cdata
}}
236 This function sets the the user data which will be passed to a
237 callback function set via
\helpref{Callback
}{wxsocketbasecallback
}.
239 Note that events are preferred over callbacks where possible.
241 \wxheading{Return value
}
243 A pointer to the previous user data.
245 \helpref{wxSocketBase::Callback
}{wxsocketbasecallback
},
246 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
247 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
}
252 \membersection{wxSocketBase::Close
}\label{wxsocketbaseclose
}
254 \func{void
}{Close
}{\void}
256 This function shuts down the socket, disabling further transmission and
257 reception of data; it also disables events for the socket and frees the
258 associated system resources. If you
\helpref{Destroy
}{wxsocketbasedestroy
}
259 a socket, Close is automatically called.
261 \wxheading{Remark/Warning
}
263 Although Close immediately disables events for the socket, it is possible
264 that event messages may be waiting in the application's event queue. The
265 application must therefore be prepared to handle socket event messages
266 even after calling Close.
271 \membersection{wxSocketBase::Destroy
}\label{wxsocketbasedestroy
}
273 \func{bool
}{Destroy
}{\void}
275 Destroys the socket safely. Use this function instead of the delete operator,
276 since otherwise socket events could reach the application even after the
277 socket has been destroyed. To prevent this problem, this function appends
278 the wxSocket to a list of object to be deleted on idle time, after all
279 events have been processed. For the same reason, you should avoid creating
280 socket objects in the stack.
282 Destroy calls
\helpref{Close
}{wxsocketbaseclose
} automatically.
284 \wxheading{Return value
}
291 \membersection{wxSocketBase::Discard
}\label{wxsocketbasediscard
}
293 \func{wxSocketBase\&
}{Discard
}{\void}
295 This function simply deletes all bytes in the incoming queue. This function
296 always returns immediately and its operation is not affected by IO flags.
298 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually discarded.
300 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return FALSE.
305 \membersection{wxSocketBase::Error
}\label{wxsocketbaseerror
}
307 \constfunc{bool
}{Error
}{\void}
309 Returns TRUE if an error occured in the last IO operation.
311 Use this function to check for an error condition after one of the
312 following calls: Discard, Peek, Read, ReadMsg, Unread, Write, WriteMsg.
317 \membersection{wxSocketBase::GetLocal
}\label{wxsocketbasegetlocal
}
319 \constfunc{bool
}{GetLocal
}{\param{wxSockAddress\&
}{addr
\_man}}
321 This function returns the local address field of the socket. The local
322 address field contains the complete local address of the socket (local
323 address, local port, ...).
325 \wxheading{Return value
}
327 It returns TRUE if no errors happened, FALSE otherwise.
332 \membersection{wxSocketBase::GetPeer
}\label{wxsocketbasegetpeer
}
334 \constfunc{bool
}{GetPeer
}{\param{wxSockAddress\&
}{addr
\_man}}
336 This function returns the peer address field of the socket. The peer
337 address field contains the complete peer host address of the socket
338 (address, port, ...).
340 \wxheading{Return value
}
342 It returns TRUE if no errors happened, FALSE otherwise.
347 \membersection{wxSocketBase::IsConnected
}\label{wxsocketbaseisconnected
}
349 \constfunc{bool
}{IsConnected
}{\void}
351 Returns TRUE if the socket is connected.
356 \membersection{wxSocketBase::IsData
}\label{wxsocketbaseisdata
}
358 \constfunc{bool
}{IsData
}{\void}
360 This function waits until the socket is readable. This might mean that
361 queued data is available for reading or, for streamed sockets, that
362 the connection has been closed, so that a read operation will complete
363 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
364 is set, in which case the operation might still block).
366 \membersection{wxSocketBase::IsDisconnected
}\label{wxsocketbaseisdisconnected
}
371 \constfunc{bool
}{IsDisconnected
}{\void}
373 Returns TRUE if the socket is not connected.
375 \membersection{wxSocketBase::LastCount
}\label{wxsocketbaselastcount
}
380 \constfunc{wxUint32
}{LastCount
}{\void}
382 Returns the number of bytes read or written by the last IO call.
384 Use this function to get the number of bytes actually transferred
385 after using one of the following IO calls: Discard, Peek, Read,
386 ReadMsg, Unread, Write, WriteMsg.
391 \membersection{wxSocketBase::LastError
}\label{wxsocketbaselasterror
}
393 \constfunc{wxSocketError
}{LastError
}{\void}
395 Returns the last wxSocket error. See
\helpref{wxSocket errors
}{wxsocketbase
}.
397 Please note that this function merely returns the last error code,
398 but it should not be used to determine if an error has occured (this
399 is because successful operations do not change the LastError value).
400 Use
\helpref{Error
}{wxsocketbaseerror
} first, in order to determine
401 if the last IO call failed. If this returns TRUE, use LastError
402 to discover the cause of the error.
407 \membersection{wxSocketBase::Notify
}\label{wxsocketbasenotify
}
409 \func{void
}{Notify
}{\param{bool
}{ notify
}}
411 According to the
{\it notify
} value, this function enables
412 or disables socket events. If
{\it notify
} is TRUE, the events
413 configured with
\helpref{SetNotify
}{wxsocketbasesetnotify
} will
414 be sent to the application. If
{\it notify
} is FALSE; no events
420 \membersection{wxSocketBase::Ok
}\label{wxsocketbaseok
}
422 \constfunc{bool
}{Ok
}{\void}
424 Returns TRUE if the socket is initialized and ready and FALSE in other
427 \wxheading{Remark/Warning
}
429 For
\helpref{wxSocketClient
}{wxsocketclient
}, Ok won't return TRUE unless
430 the client is connected to a server.
432 For
\helpref{wxSocketServer
}{wxsocketserver
}, Ok will return TRUE if the
433 server could bind to the specified address and is already listening for
436 Ok does not check for IO errors; use
\helpref{Error
}{wxsocketbaseerror
}
437 instead for that purpose.
442 \membersection{wxSocketBase::RestoreState
}\label{wxsocketbaserestorestate
}
444 \func{void
}{RestoreState
}{\void}
446 This function restores the previous state of the socket, as saved
447 with
\helpref{SaveState
}{wxsocketbasesavestate
}
449 Calls to SaveState and RestoreState can be nested.
453 \helpref{wxSocketBase::SaveState
}{wxsocketbasesavestate
}
458 \membersection{wxSocketBase::SaveState
}\label{wxsocketbasesavestate
}
460 \func{void
}{SaveState
}{\void}
462 This function saves the current state of the socket in a stack. Socket
463 state includes flags, as set with
\helpref{SetFlags
}{wxsocketbasesetflags
},
464 event mask, as set with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
465 \helpref{Notify
}{wxsocketbasenotify
}, and current settings for the
466 asynchronous callbacks, as set with
\helpref{Callback
}{wxsocketbasecallback
}
467 and
\helpref{CallbackData
}{wxsocketbasecallbackdata
}.
469 Calls to SaveState and RestoreState can be nested.
473 \helpref{wxSocketBase::RestoreState
}{wxsocketbaserestorestate
}
478 \membersection{wxSocketBase::SetEventHandler
}\label{wxsocketbaseseteventhandler
}
480 \func{void
}{SetEventHandler
}{\param{wxEvtHandler\&
}{ evt
\_hdlr},
\param{int
}{ id = -
1}}
482 Sets an event handler to be called when a socket event occurs. The
483 handler will be called for those events for which notification is
484 enabled with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
485 \helpref{Notify
}{wxsocketbasenotify
}.
487 You can also specify a callback function to be called when an event
488 occurs, although if possible, events should be used instead of callbacks.
489 See
\helpref{Callback
}{wxsocketbasecallback
} and
490 \helpref{CallbackData
}{wxsocketbasecallbackdata
}.
492 \wxheading{Parameters
}
494 \docparam{evt
\_hdlr}{Specifies the event handler you want to use.
}
496 \docparam{id
}{The id of socket event.
}
500 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
501 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
502 \helpref{wxSocketEvent
}{wxsocketevent
},
503 \helpref{wxEvtHandler
}{wxevthandler
},
504 \helpref{wxSocketBase::Callback
}{wxsocketbasecallback
},
505 \helpref{wxSocketBase::CallbackData
}{wxsocketbasecallbackdata
}
510 \membersection{wxSocketBase::SetFlags
}\label{wxsocketbasesetflags
}
512 \func{void
}{SetFlags
}{\param{wxSocketBase::wxSockFlags
}{ flags
}}
515 \begin{twocollist
}\itemsep=
0pt
516 \twocolitem{{\bf wxSOCKET
\_NONE}}{Normal functionality.
}
517 \twocolitem{{\bf wxSOCKET
\_NOWAIT}}{Read/write as much data as possible and return immediately.
}
518 \twocolitem{{\bf wxSOCKET
\_WAITALL}}{Wait for all required data to be read/written unless an error occurs.
}
519 \twocolitem{{\bf wxSOCKET
\_BLOCK}}{Block the GUI (do not yield) while reading/writing data.
}
522 A brief overview on how to use these flags follows.
524 If no flag is specified (this is the same as
{\bf wxSOCKET
\_NONE}),
525 IO calls will return after some data has been read or written, even
526 when the transfer might not be complete. This is the same as issuing
527 exactly one blocking low-level call to recv() or send(). Note that
528 {\it blocking
} here refers to when the function returns, not to whether
529 the GUI blocks during this time.
531 If
{\bf wxSOCKET
\_NOWAIT} is specified, IO calls will return immediately.
532 Read operations will retrieve only available data. Write operations will
533 write as much data as possible, depending on how much space is available
534 in the output buffer. This is the same as issuing exactly one nonblocking
535 low-level call to recv() or send(). Note that
{\it nonblocking
} here
536 refers to when the function returns, not to whether the GUI blocks during
539 If
{\bf wxSOCKET
\_WAITALL} is specified, IO calls won't return until ALL
540 the data has been read or written (or until an error occurs), blocking if
541 necessary, and issuing several low level calls if necessary. This is the
542 same as having a loop which makes as many blocking low-level calls to
543 recv() or send() as needed so as to transfer all the data. Note that
544 {\it blocking
} here refers to when the function returns, not to whether
545 the GUI blocks during this time.
547 The
{\bf wxSOCKET
\_BLOCK} flag controls whether the GUI blocks during
548 IO operations. If this flag is specified, the socket will not yield
549 during IO calls, so the GUI will remain blocked until the operation
550 completes. If it is not used, then the application must take extra
551 care to avoid unwanted reentrance.
555 {\bf wxSOCKET
\_NONE} will try to read at least SOME data, no matter how much.
557 {\bf wxSOCKET
\_NOWAIT} will always return immediately, even if it cannot
558 read or write ANY data.
560 {\bf wxSOCKET
\_WAITALL} will only return when it has read or written ALL
563 {\bf wxSOCKET
\_BLOCK} has nothing to do with the previous flags and
564 it controls whether the GUI blocks.
569 \membersection{wxSocketBase::SetNotify
}\label{wxsocketbasesetnotify
}
571 \func{void
}{SetNotify
}{\param{wxSocketEventFlags
}{ flags
}}
573 SetNotify specifies which socket events are to be sent to the event handler.
574 The
{\it flags
} parameter is a combination of flags ORed toghether. The
575 following flags can be used:
578 \begin{twocollist
}\itemsep=
0pt
579 \twocolitem{{\bf wxSOCKET
\_INPUT\_FLAG}}{to receive wxSOCKET
\_INPUT}
580 \twocolitem{{\bf wxSOCKET
\_OUTPUT\_FLAG}}{to receive wxSOCKET
\_OUTPUT}
581 \twocolitem{{\bf wxSOCKET
\_CONNECTION\_FLAG}}{to receive wxSOCKET
\_CONNECTION}
582 \twocolitem{{\bf wxSOCKET
\_LOST\_FLAG}}{to receive wxSOCKET
\_LOST}
588 sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
592 In this example, the user will be notified about incoming socket data and
593 whenever the connection is closed.
595 For more information on socket events see
\helpref{wxSocket events
}{wxsocketbase
}.
600 \membersection{wxSocketBase::SetTimeout
}\label{wxsocketbasesettimeout
}
602 \func{void
}{SetTimeout
}{\param{int
}{seconds
}}
604 This function sets the default socket timeout in seconds. This timeout
605 applies to all IO calls, and also to the
\helpref{Wait
}{wxsocketbasewait
}
606 family of functions if you don't specify a wait interval. Initially, the
607 default is set to
10 minutes.
612 \membersection{wxSocketBase::Peek
}\label{wxsocketbasepeek
}
614 \func{wxSocketBase\&
}{Peek
}{\param{char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
616 This function peeks a buffer of
{\it nbytes
} bytes from the socket.
617 Peeking a buffer doesn't delete it from the socket input queue.
619 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually peeked.
621 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
623 \wxheading{Parameters
}
625 \docparam{buffer
}{Buffer where to put peeked data.
}
627 \docparam{nbytes
}{Number of bytes.
}
629 \wxheading{Return value
}
631 Returns a reference to the current object.
633 \wxheading{Remark/Warning
}
635 The exact behaviour of wxSocketBase::Peek depends on the combination
636 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
640 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
641 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
642 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
643 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
648 \membersection{wxSocketBase::Read
}\label{wxsocketbaseread
}
650 \func{wxSocketBase\&
}{Read
}{\param{char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
652 This function reads a buffer of
{\it nbytes
} bytes from the socket.
654 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
656 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
658 \wxheading{Parameters
}
660 \docparam{buffer
}{Buffer where to put read data.
}
662 \docparam{nbytes
}{Number of bytes.
}
664 \wxheading{Return value
}
666 Returns a reference to the current object.
668 \wxheading{Remark/Warning
}
670 The exact behaviour of wxSocketBase::Read depends on the combination
671 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
675 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
676 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
677 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
678 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
683 \membersection{wxSocketBase::ReadMsg
}\label{wxsocketbasereadmsg
}
685 \func{wxSocketBase\&
}{ReadMsg
}{\param{char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
687 This function reads a buffer sent by
\helpref{WriteMsg
}{wxsocketbasewritemsg
}
688 on a socket. If the buffer passed to the function isn't big enough, the
689 remaining bytes will be discarded. This function always waits for the
690 buffer to be entirely filled, unless an error occurs.
692 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
694 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
696 \wxheading{Parameters
}
698 \docparam{buffer
}{Buffer where to put read data.
}
700 \docparam{nbytes
}{Size of the buffer.
}
702 \wxheading{Return value
}
704 Returns a reference to the current object.
706 \wxheading{Remark/Warning
}
708 wxSocketBase::ReadMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
709 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
710 The exact behaviour of ReadMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
711 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
715 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
716 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
717 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
718 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
719 \helpref{wxSocketBase::WriteMsg
}{wxsocketbasewritemsg
}
724 \membersection{wxSocketBase::Unread
}\label{wxsocketbaseunread
}
726 \func{wxSocketBase\&
}{Unread
}{\param{const char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
728 This function unreads a buffer. That is, the data in the buffer is put back
729 in the incoming queue. This function is not affected by wxSocket flags.
731 If you use
\helpref{LastCount
}{wxsocketbaselastcount
}, it will always return
{\it nbytes
}.
733 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return FALSE.
735 \wxheading{Parameters
}
737 \docparam{buffer
}{Buffer to be unread.
}
739 \docparam{nbytes
}{Number of bytes.
}
741 \wxheading{Return value
}
743 Returns a reference to the current object.
747 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
748 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
749 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
}
754 \membersection{wxSocketBase::Wait
}\label{wxsocketbasewait
}
756 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
758 This function waits until one of the following conditions is TRUE. Note
759 that it is recommended to use the individual Wait functions to wait for
760 the required condition, instead of this one.
763 \item The socket becomes readable.
764 \item The socket becomes writable.
765 \item An ongoing connection request has completed (only for clients)
766 \item An incoming connection request has arrived (only for servers)
767 \item The connection has been closed.
770 \wxheading{Parameters
}
772 \docparam{seconds
}{Number of seconds to wait.
773 If -
1, it will wait for the default timeout,
774 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
776 \docparam{millisecond
}{Number of milliseconds to wait.
}
778 \wxheading{Return value
}
780 Returns TRUE when any of the above conditions is satisfied,
781 FALSE if the timeout was reached.
785 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
786 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
787 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
792 \membersection{wxSocketBase::WaitForRead
}\label{wxsocketbasewaitforread
}
794 \func{bool
}{WaitForRead
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
796 This function waits until the socket is readable. This might mean that
797 queued data is available for reading or, for streamed sockets, that
798 the connection has been closed, so that a read operation will complete
799 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
800 is set, in which case the operation might still block).
802 \wxheading{Parameters
}
804 \docparam{seconds
}{Number of seconds to wait.
805 If -
1, it will wait for the default timeout,
806 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
808 \docparam{millisecond
}{Number of milliseconds to wait.
}
810 \wxheading{Return value
}
812 Returns TRUE if the socket becomes readable, FALSE on timeout.
816 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
},
817 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
818 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
823 \membersection{wxSocketBase::WaitForWrite
}\label{wxsocketbasewaitforwrite
}
825 \func{bool
}{WaitForWrite
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
827 This function waits until the socket becomes writable. This might mean that
828 the socket is ready to send new data, or for streamed sockets, that the
829 connection has been closed, so that a write operation is guaranteed to
830 complete immediately (unless the
{\bf wxSOCKET
\_WAITALL} flag is set,
831 in which case the operation might still block).
833 \wxheading{Parameters
}
835 \docparam{seconds
}{Number of seconds to wait.
836 If -
1, it will wait for the default timeout,
837 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
839 \docparam{millisecond
}{Number of milliseconds to wait.
}
841 \wxheading{Return value
}
843 Returns TRUE if the socket becomes writable, FALSE on timeout.
847 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
},
848 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
849 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
854 \membersection{wxSocketBase::WaitForLost
}\label{wxsocketbasewaitforlost
}
856 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
858 This function waits until the connection is lost. This may happen if
859 the peer gracefully closes the connection or if the connection breaks.
861 \wxheading{Parameters
}
863 \docparam{seconds
}{Number of seconds to wait.
864 If -
1, it will wait for the default timeout,
865 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
867 \docparam{millisecond
}{Number of milliseconds to wait.
}
869 \wxheading{Return value
}
871 Returns TRUE if the connection was lost, FALSE if the timeout was reached.
875 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
876 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
877 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
882 \membersection{wxSocketBase::Write
}\label{wxsocketbasewrite
}
884 \func{wxSocketBase\&
}{Write
}{\param{const char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
886 This function writes a buffer of
{\it nbytes
} bytes to the socket.
888 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
890 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
892 \wxheading{Parameters
}
894 \docparam{buffer
}{Buffer with the data to be sent.
}
896 \docparam{nbytes
}{Number of bytes.
}
898 \wxheading{Return value
}
900 Returns a reference to the current object.
902 \wxheading{Remark/Warning
}
904 The exact behaviour of wxSocketBase::Write depends on the combination
905 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
909 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
910 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
911 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
912 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
917 \membersection{wxSocketBase::WriteMsg
}\label{wxsocketbasewritemsg
}
919 \func{wxSocketBase\&
}{WriteMsg
}{\param{const char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
921 This function writes a buffer of
{\it nbytes
} bytes from the socket, but it
922 writes a short header before so that
\helpref{ReadMsg
}{wxsocketbasereadmsg
}
923 knows how much data should it actually read. So, a buffer sent with WriteMsg
924 {\bf must
} be read with ReadMsg. This function always waits for the entire
925 buffer to be sent, unless an error occurs.
927 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
929 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
931 \wxheading{Parameters
}
933 \docparam{buffer
}{Buffer with the data to be sent.
}
935 \docparam{nbytes
}{Number of bytes to send.
}
937 \wxheading{Return value
}
939 Returns a reference to the current object.
941 \wxheading{Remark/Warning
}
943 wxSocketBase::WriteMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
944 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
945 The exact behaviour of WriteMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
946 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
950 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
951 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
952 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
953 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
954 \helpref{wxSocketBase::ReadMsg
}{wxsocketbasereadmsg
}
957 % ---------------------------------------------------------------------------
958 % CLASS wxSocketClient
959 % ---------------------------------------------------------------------------
961 \section{\class{wxSocketClient
}}\label{wxsocketclient
}
963 \wxheading{Derived from
}
965 \helpref{wxSocketBase
}{wxsocketbase
}
967 \wxheading{Include files
}
971 % ---------------------------------------------------------------------------
973 % ---------------------------------------------------------------------------
977 \membersection{wxSocketClient::wxSocketClient
}
979 \func{}{wxSocketClient
}{\param{wxSockFlags
}{ flags = wxSocketBase::NONE
}}
983 \wxheading{Parameters
}
985 \docparam{flags
}{Socket flags (See
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
})
}
990 \membersection{wxSocketClient::
\destruct{wxSocketClient
}}
992 \func{}{\destruct{wxSocketClient
}}{\void}
999 \membersection{wxSocketClient::Connect
}\label{wxsocketclientconnect
}
1001 \func{bool
}{Connect
}{\param{wxSockAddress\&
}{ address
},
\param{bool
}{ wait = TRUE
}}
1003 Connects to a server using the specified address.
1005 If
{\it wait
} is TRUE, Connect will wait until the connection completes.
1006 {\bf Warning:
} This will block the GUI.
1008 If
{\it wait
} is FALSE, Connect will try to establish the connection and
1009 return immediately, without blocking the GUI. When used this way, even if
1010 Connect returns FALSE, the connection request can be completed later.
1011 To detect this, use
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
},
1012 or catch
{\bf wxSOCKET
\_CONNECTION} events (for successful establishment)
1013 and
{\bf wxSOCKET
\_LOST} events (for connection failure).
1015 \wxheading{Parameters
}
1017 \docparam{address
}{Address of the server.
}
1019 \docparam{wait
}{If TRUE, waits for the connection to complete.
}
1021 \wxheading{Return value
}
1023 Returns TRUE if the connection is established and no error occurs.
1025 If
{\it wait
} was TRUE, and Connect returns FALSE, an error occured
1026 and the connection failed.
1028 If
{\it wait
} was FALSE, and Connect returns FALSE, you should still
1029 be prepared to handle the completion of this connection request, either
1030 with
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
} or by watching
1031 {\bf wxSOCKET
\_CONNECTION} and
{\bf wxSOCKET
\_LOST} events.
1033 \wxheading{See also
}
1035 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
},
1036 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1037 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
}
1042 \membersection{wxSocketClient::WaitOnConnect
}\label{wxsocketclientwaitonconnect
}
1044 \func{bool
}{WaitOnConnect
}{\param{long
}{ seconds = -
1},
\param{long
}{ milliseconds =
0}}
1046 Wait until a connection request completes, or until the specified timeout
1047 elapses. Use this function after issuing a call to
\helpref{Connect
}{wxsocketclientconnect
}
1048 with
{\it wait
} set to FALSE.
1050 \wxheading{Parameters
}
1052 \docparam{seconds
}{Number of seconds to wait.
1053 If -
1, it will wait for the default timeout,
1054 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
1056 \docparam{millisecond
}{Number of milliseconds to wait.
}
1058 \wxheading{Return value
}
1060 WaitOnConnect returns TRUE if the connection request completes. This
1061 does not necessarily mean that the connection was succesfully established;
1062 it might also happen that the connection was refused by the peer. Use
1063 \helpref{IsConnected
}{wxsocketbaseisconnected
} to distinguish between
1064 these two situations.
1066 If the timeout elapses, WaitOnConnect returns FALSE.
1068 These semantics allow code like this:
1071 // Issue the connection request
1072 client->Connect(addr, FALSE);
1074 // Wait until the request completes or until we decide to give up
1075 bool waitmore = TRUE;
1076 while ( !client->WaitOnConnect(seconds, millis) && waitmore )
1078 // possibly give some feedback to the user,
1079 // and update waitmore if needed.
1081 bool success = client->IsConnected();
1084 \wxheading{See also
}
1086 \helpref{wxSocketClient::Connect
}{wxsocketclientconnect
},
1087 \helpref{wxSocketBase::IsConnected
}{wxsocketbaseisconnected
}
1089 % ---------------------------------------------------------------------------
1090 % CLASS: wxSocketEvent
1091 % ---------------------------------------------------------------------------
1092 \section{\class{wxSocketEvent
}}\label{wxsocketevent
}
1094 This event class contains information about socket events.
1096 \wxheading{Derived from
}
1098 \helpref{wxEvent
}{wxevent
}
1100 \wxheading{Include files
}
1104 \wxheading{Event table macros
}
1106 To process a socket event, use these event handler macros to direct input to member
1107 functions that take a wxSocketEvent argument.
1110 \begin{twocollist
}\itemsep=
0pt
1111 \twocolitem{{\bf EVT
\_SOCKET(id, func)
}}{Process a socket event, supplying the member function.
}
1114 \wxheading{See also
}
1116 \helpref{wxSocketBase
}{wxsocketbase
},
1117 \helpref{wxSocketClient
}{wxsocketclient
},
1118 \helpref{wxSocketServer
}{wxsocketserver
}
1120 \latexignore{\rtfignore{\wxheading{Members
}}}
1122 \membersection{wxSocketEvent::wxSocketEvent
}
1124 \func{}{wxSocketEvent
}{\param{int
}{ id =
0}}
1128 \membersection{wxSocketEvent::Socket
}\label{wxsocketeventsocket
}
1130 \constfunc{wxSocketBase *
}{Socket
}{\void}
1132 Returns the socket object to which this event refers to. This makes
1133 it possible to use the same event handler for different sockets.
1135 \membersection{wxSocketEvent::SocketEvent
}\label{wxsocketeventsocketevent
}
1137 \constfunc{wxSocketNotify
}{SocketEvent
}{\void}
1139 Returns the socket event type.
1141 % ---------------------------------------------------------------------------
1142 % CLASS: wxSocketServer
1143 % ---------------------------------------------------------------------------
1144 \section{\class{wxSocketServer
}}\label{wxsocketserver
}
1146 \wxheading{Derived from
}
1148 \helpref{wxSocketBase
}{wxsocketbase
}
1150 \wxheading{Include files
}
1154 % ---------------------------------------------------------------------------
1156 % ---------------------------------------------------------------------------
1157 \latexignore{\rtfignore{\wxheading{Members
}}}
1162 \membersection{wxSocketServer::wxSocketServer
}\label{wxsocketserverconstr
}
1164 \func{}{wxSocketServer
}{\param{wxSockAddress\&
}{ address
},
\param{wxSockFlags
}{ flags = wxSocketBase::NONE
}}
1166 Constructs a new server and tries to bind to the specified
{\it address
}.
1167 Before trying to accept new connections, test whether it succeeded with
1168 \helpref{wxSocketBase::Ok
}{wxsocketbaseok
}.
1170 \wxheading{Parameters
}
1172 \docparam{address
}{Specifies the local address for the server (e.g. port number).
}
1174 \docparam{flags
}{Socket flags (See
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
})
}
1179 \membersection{wxSocketServer::
\destruct{wxSocketServer
}}
1181 \func{}{\destruct{wxSocketServer
}}{\void}
1183 Destructor (it doesn't close the accepted connections).
1188 \membersection{wxSocketServer::Accept
}\label{wxsocketserveraccept
}
1190 \func{wxSocketBase *
}{Accept
}{\param{bool
}{ wait = TRUE
}}
1192 Accepts an incoming connection request, and creates a new
1193 \helpref{wxSocketBase
}{wxsocketbase
} object which represents
1194 the server-side of the connection.
1196 If
{\it wait
} is TRUE and there are no pending connections to be
1197 accepted, it will wait for the next incoming connection to arrive.
1198 {\bf Warning:
} This will block the GUI.
1200 If
{\it wait
} is FALSE, it will try to accept a pending connection
1201 if there is one, but it will always return immediately without blocking
1202 the GUI. If you want to use Accept in this way, you can either check for
1203 incoming connections with
\helpref{WaitForAccept
}{wxsocketserverwaitforaccept
}
1204 or catch
{\bf wxSOCKET
\_CONNECTION} events, then call Accept once you know
1205 that there is an incoming connection waiting to be accepted.
1207 \wxheading{Return value
}
1209 Returns an opened socket connection, or NULL if an error occured or
1210 if the
{\it wait
} parameter was FALSE and there were no pending
1213 \wxheading{See also
}
1215 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
1216 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1217 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
1218 \helpref{wxSocketServer::AcceptWith
}{wxsocketserveracceptwith
}
1223 \membersection{wxSocketServer::AcceptWith
}\label{wxsocketserveracceptwith
}
1225 \func{bool
}{AcceptWith
}{\param{wxSocketBase\&
}{ socket
},
\param{bool
}{ wait = TRUE
}}
1227 Accept an incoming connection using the specified socket object.
1229 \wxheading{Parameters
}
1231 \docparam{socket
}{Socket to be initialized
}
1233 \wxheading{Return value
}
1235 Returns TRUE on success, or FALSE if an error occured or if the
1236 {\it wait
} parameter was FALSE and there were no pending
1239 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
1240 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1241 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
1242 \helpref{wxSocketServer::Accept
}{wxsocketserveraccept
} for a detailed explanation
1247 \membersection{wxSocketServer::WaitForAccept
}\label{wxsocketserverwaitforaccept
}
1249 \func{bool
}{WaitForAccept
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
1251 This function waits for an incoming connection. Use it if you want to call
1252 \helpref{Accept
}{wxsocketserveraccept
} or
\helpref{AcceptWith
}{wxsocketserveracceptwith
}
1253 with
{\it wait
} set to FALSE, to detect when an incoming connection is waiting
1256 \wxheading{Parameters
}
1258 \docparam{seconds
}{Number of seconds to wait.
1259 If -
1, it will wait for the default timeout,
1260 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
1262 \docparam{millisecond
}{Number of milliseconds to wait.
}
1264 \wxheading{Return value
}
1266 Returns TRUE if an incoming connection arrived, FALSE if the timeout elapsed.
1268 \wxheading{See also
}
1270 \helpref{wxSocketServer::Accept
}{wxsocketserveraccept
},
1271 \helpref{wxSocketServer::AcceptWith
}{wxsocketserveracceptwith
}