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{Destroy
}{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. Upon socket destruction, Close is automatically
259 called. This means that you don't need to do it yourself, unless you
260 explicitly want to disable further operation.
262 \wxheading{Remark/Warning
}
264 Although Close immediately disables events for the socket, it is possible
265 that event messages may be waiting in the application's event queue. The
266 application must therefore be prepared to handle socket event messages
267 even after calling Close.
272 \membersection{wxSocketBase::Destroy
}\label{wxsocketbasedestroy
}
274 \func{bool
}{Destroy
}{\void}
276 Destroys the socket safely. Use this function instead of the delete operator,
277 since otherwise socket events could reach the application even after the
278 socket has been destroyed. To prevent this problem, this function appends
279 the wxSocket to a list of object to be deleted on idle time, after all
280 events have been processed. For the same reason, you should avoid creating
281 socket objects in the stack.
283 Destroy calls
\helpref{Close
}{wxsocketbaseclose
} automatically.
285 \wxheading{Return value
}
292 \membersection{wxSocketBase::Discard
}\label{wxsocketbasediscard
}
294 \func{wxSocketBase\&
}{Discard
}{\void}
296 This function simply deletes all bytes in the incoming queue. This function
297 always returns immediately and its operation is not affected by IO flags.
299 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually discarded.
301 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return FALSE.
306 \membersection{wxSocketBase::Error
}\label{wxsocketbaseerror
}
308 \constfunc{bool
}{Error
}{\void}
310 Returns TRUE if an error occured in the last IO operation.
312 Use this function to check for an error condition after one of the
313 following calls: Discard, Peek, Read, ReadMsg, Unread, Write, WriteMsg.
318 \membersection{wxSocketBase::GetLocal
}\label{wxsocketbasegetlocal
}
320 \constfunc{bool
}{GetLocal
}{\param{wxSockAddress\&
}{addr
\_man}}
322 This function returns the local address field of the socket. The local
323 address field contains the complete local address of the socket (local
324 address, local port, ...).
326 \wxheading{Return value
}
328 It returns TRUE if no errors happened, FALSE otherwise.
333 \membersection{wxSocketBase::GetPeer
}\label{wxsocketbasegetpeer
}
335 \constfunc{bool
}{GetPeer
}{\param{wxSockAddress\&
}{addr
\_man}}
337 This function returns the peer address field of the socket. The peer
338 address field contains the complete peer host address of the socket
339 (address, port, ...).
341 \wxheading{Return value
}
343 It returns TRUE if no errors happened, FALSE otherwise.
348 \membersection{wxSocketBase::IsConnected
}\label{wxsocketbaseisconnected
}
350 \constfunc{bool
}{IsConnected
}{\void}
352 Returns TRUE if the socket is connected.
357 \membersection{wxSocketBase::IsData
}\label{wxsocketbaseisdata
}
359 \constfunc{bool
}{IsData
}{\void}
361 This function waits until the socket is readable. This might mean that
362 queued data is available for reading or, for streamed sockets, that
363 the connection has been closed, so that a read operation will complete
364 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
365 is set, in which case the operation might still block).
367 \membersection{wxSocketBase::IsDisconnected
}\label{wxsocketbaseisdisconnected
}
372 \constfunc{bool
}{IsDisconnected
}{\void}
374 Returns TRUE if the socket is not connected.
376 \membersection{wxSocketBase::LastCount
}\label{wxsocketbaselastcount
}
381 \constfunc{wxUint32
}{LastCount
}{\void}
383 Returns the number of bytes read or written by the last IO call.
385 Use this function to get the number of bytes actually transferred
386 after using one of the following IO calls: Discard, Peek, Read,
387 ReadMsg, Unread, Write, WriteMsg.
392 \membersection{wxSocketBase::LastError
}\label{wxsocketbaselasterror
}
394 \constfunc{wxSocketError
}{LastError
}{\void}
396 Returns the last wxSocket error. See
\helpref{wxSocket errors
}{wxsocketbase
}.
398 Please note that this function merely returns the last error code,
399 but it should not be used to determine if an error has occured (this
400 is because successful operations do not change the LastError value).
401 Use
\helpref{Error
}{wxsocketbaseerror
} first, in order to determine
402 if the last IO call failed. If this returns TRUE, use LastError
403 to discover the cause of the error.
408 \membersection{wxSocketBase::Notify
}\label{wxsocketbasenotify
}
410 \func{void
}{Notify
}{\param{bool
}{ notify
}}
412 According to the
{\it notify
} value, this function enables
413 or disables socket events. If
{\it notify
} is TRUE, the events
414 configured with
\helpref{SetNotify
}{wxsocketbasesetnotify
} will
415 be sent to the application. If
{\it notify
} is FALSE; no events
421 \membersection{wxSocketBase::Ok
}\label{wxsocketbaseok
}
423 \constfunc{bool
}{Ok
}{\void}
425 Returns TRUE if the socket is initialized and ready and FALSE in other
428 \wxheading{Remark/Warning
}
430 For
\helpref{wxSocketClient
}{wxsocketclient
}, Ok won't return TRUE unless
431 the client is connected to a server.
433 For
\helpref{wxSocketServer
}{wxsocketserver
}, Ok will return TRUE if the
434 server could bind to the specified address and is already listening for
437 Ok does not check for IO errors; use
\helpref{Error
}{wxsocketbaseerror
}
438 instead for that purpose.
443 \membersection{wxSocketBase::RestoreState
}\label{wxsocketbaserestorestate
}
445 \func{void
}{RestoreState
}{\void}
447 This function restores the previous state of the socket, as saved
448 with
\helpref{SaveState
}{wxsocketbasesavestate
}
450 Calls to SaveState and RestoreState can be nested.
454 \helpref{wxSocketBase::SaveState
}{wxsocketbasesavestate
}
459 \membersection{wxSocketBase::SaveState
}\label{wxsocketbasesavestate
}
461 \func{void
}{SaveState
}{\void}
463 This function saves the current state of the socket in a stack. Socket
464 state includes flags, as set with
\helpref{SetFlags
}{wxsocketbasesetflags
},
465 event mask, as set with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
466 \helpref{Notify
}{wxsocketbasenotify
}, and current settings for the
467 asynchronous callbacks, as set with
\helpref{Callback
}{wxsocketbasecallback
}
468 and
\helpref{CallbackData
}{wxsocketbasecallbackdata
}.
470 Calls to SaveState and RestoreState can be nested.
474 \helpref{wxSocketBase::RestoreState
}{wxsocketbaserestorestate
}
479 \membersection{wxSocketBase::SetEventHandler
}\label{wxsocketbaseseteventhandler
}
481 \func{void
}{SetEventHandler
}{\param{wxEvtHandler\&
}{ evt
\_hdlr},
\param{int
}{ id = -
1}}
483 Sets an event handler to be called when a socket event occurs. The
484 handler will be called for those events for which notification is
485 enabled with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
486 \helpref{Notify
}{wxsocketbasenotify
}.
488 You can also specify a callback function to be called when an event
489 occurs, although if possible, events should be used instead of callbacks.
490 See
\helpref{Callback
}{wxsocketbasecallback
} and
491 \helpref{CallbackData
}{wxsocketbasecallbackdata
}.
493 \wxheading{Parameters
}
495 \docparam{evt
\_hdlr}{Specifies the event handler you want to use.
}
497 \docparam{id
}{The id of socket event.
}
501 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
502 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
503 \helpref{wxSocketEvent
}{wxsocketevent
},
504 \helpref{wxEvtHandler
}{wxevthandler
},
505 \helpref{wxSocketBase::Callback
}{wxsocketbasecallback
},
506 \helpref{wxSocketBase::CallbackData
}{wxsocketbasecallbackdata
}
511 \membersection{wxSocketBase::SetFlags
}\label{wxsocketbasesetflags
}
513 \func{void
}{SetFlags
}{\param{wxSocketBase::wxSocketFlags
}{ flags
}}
516 \begin{twocollist
}\itemsep=
0pt
517 \twocolitem{{\bf wxSOCKET
\_NONE}}{Normal functionality.
}
518 \twocolitem{{\bf wxSOCKET
\_NOWAIT}}{Read/write as much data as possible and return immediately.
}
519 \twocolitem{{\bf wxSOCKET
\_WAITALL}}{Wait for all required data to be read/written unless an error occurs.
}
520 \twocolitem{{\bf wxSOCKET
\_BLOCK}}{Block the GUI (do not yield) while reading/writing data.
}
523 A brief overview on how to use these flags follows.
525 If no flag is specified (this is the same as
{\bf wxSOCKET
\_NONE}),
526 IO calls will return after some data has been read or written, even
527 when the transfer might not be complete. This is the same as issuing
528 exactly one blocking low-level call to recv() or send(). Note that
529 {\it blocking
} here refers to when the function returns, not to whether
530 the GUI blocks during this time.
532 If
{\bf wxSOCKET
\_NOWAIT} is specified, IO calls will return immediately.
533 Read operations will retrieve only available data. Write operations will
534 write as much data as possible, depending on how much space is available
535 in the output buffer. This is the same as issuing exactly one nonblocking
536 low-level call to recv() or send(). Note that
{\it nonblocking
} here
537 refers to when the function returns, not to whether the GUI blocks during
540 If
{\bf wxSOCKET
\_WAITALL} is specified, IO calls won't return until ALL
541 the data has been read or written (or until an error occurs), blocking if
542 necessary, and issuing several low level calls if necessary. This is the
543 same as having a loop which makes as many blocking low-level calls to
544 recv() or send() as needed so as to transfer all the data. Note that
545 {\it blocking
} here refers to when the function returns, not to whether
546 the GUI blocks during this time.
548 The
{\bf wxSOCKET
\_BLOCK} flag controls whether the GUI blocks during
549 IO operations. If this flag is specified, the socket will not yield
550 during IO calls, so the GUI will remain blocked until the operation
551 completes. If it is not used, then the application must take extra
552 care to avoid unwanted reentrance.
556 {\bf wxSOCKET
\_NONE} will try to read at least SOME data, no matter how much.
558 {\bf wxSOCKET
\_NOWAIT} will always return immediately, even if it cannot
559 read or write ANY data.
561 {\bf wxSOCKET
\_WAITALL} will only return when it has read or written ALL
564 {\bf wxSOCKET
\_BLOCK} has nothing to do with the previous flags and
565 it controls whether the GUI blocks.
570 \membersection{wxSocketBase::SetNotify
}\label{wxsocketbasesetnotify
}
572 \func{void
}{SetNotify
}{\param{wxSocketEventFlags
}{ flags
}}
574 SetNotify specifies which socket events are to be sent to the event handler.
575 The
{\it flags
} parameter is a combination of flags ORed toghether. The
576 following flags can be used:
579 \begin{twocollist
}\itemsep=
0pt
580 \twocolitem{{\bf wxSOCKET
\_INPUT\_FLAG}}{to receive wxSOCKET
\_INPUT}
581 \twocolitem{{\bf wxSOCKET
\_OUTPUT\_FLAG}}{to receive wxSOCKET
\_OUTPUT}
582 \twocolitem{{\bf wxSOCKET
\_CONNECTION\_FLAG}}{to receive wxSOCKET
\_CONNECTION}
583 \twocolitem{{\bf wxSOCKET
\_LOST\_FLAG}}{to receive wxSOCKET
\_LOST}
589 sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
593 In this example, the user will be notified about incoming socket data and
594 whenever the connection is closed.
596 For more information on socket events see
\helpref{wxSocket events
}{wxsocketbase
}.
601 \membersection{wxSocketBase::SetTimeout
}\label{wxsocketbasesettimeout
}
603 \func{void
}{SetTimeout
}{\param{int
}{seconds
}}
605 This function sets the default socket timeout in seconds. This timeout
606 applies to all IO calls, and also to the
\helpref{Wait
}{wxsocketbasewait
}
607 family of functions if you don't specify a wait interval. Initially, the
608 default is set to
10 minutes.
613 \membersection{wxSocketBase::Peek
}\label{wxsocketbasepeek
}
615 \func{wxSocketBase\&
}{Peek
}{\param{void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
617 This function peeks a buffer of
{\it nbytes
} bytes from the socket.
618 Peeking a buffer doesn't delete it from the socket input queue.
620 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually peeked.
622 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
624 \wxheading{Parameters
}
626 \docparam{buffer
}{Buffer where to put peeked data.
}
628 \docparam{nbytes
}{Number of bytes.
}
630 \wxheading{Return value
}
632 Returns a reference to the current object.
634 \wxheading{Remark/Warning
}
636 The exact behaviour of wxSocketBase::Peek depends on the combination
637 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
641 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
642 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
643 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
644 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
649 \membersection{wxSocketBase::Read
}\label{wxsocketbaseread
}
651 \func{wxSocketBase\&
}{Read
}{\param{void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
653 This function reads a buffer of
{\it nbytes
} bytes from the socket.
655 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
657 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
659 \wxheading{Parameters
}
661 \docparam{buffer
}{Buffer where to put read data.
}
663 \docparam{nbytes
}{Number of bytes.
}
665 \wxheading{Return value
}
667 Returns a reference to the current object.
669 \wxheading{Remark/Warning
}
671 The exact behaviour of wxSocketBase::Read depends on the combination
672 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
676 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
677 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
678 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
679 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
684 \membersection{wxSocketBase::ReadMsg
}\label{wxsocketbasereadmsg
}
686 \func{wxSocketBase\&
}{ReadMsg
}{\param{void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
688 This function reads a buffer sent by
\helpref{WriteMsg
}{wxsocketbasewritemsg
}
689 on a socket. If the buffer passed to the function isn't big enough, the
690 remaining bytes will be discarded. This function always waits for the
691 buffer to be entirely filled, unless an error occurs.
693 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
695 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
697 \wxheading{Parameters
}
699 \docparam{buffer
}{Buffer where to put read data.
}
701 \docparam{nbytes
}{Size of the buffer.
}
703 \wxheading{Return value
}
705 Returns a reference to the current object.
707 \wxheading{Remark/Warning
}
709 wxSocketBase::ReadMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
710 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
711 The exact behaviour of ReadMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
712 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
716 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
717 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
718 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
719 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
720 \helpref{wxSocketBase::WriteMsg
}{wxsocketbasewritemsg
}
725 \membersection{wxSocketBase::Unread
}\label{wxsocketbaseunread
}
727 \func{wxSocketBase\&
}{Unread
}{\param{const void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
729 This function unreads a buffer. That is, the data in the buffer is put back
730 in the incoming queue. This function is not affected by wxSocket flags.
732 If you use
\helpref{LastCount
}{wxsocketbaselastcount
}, it will always return
{\it nbytes
}.
734 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return FALSE.
736 \wxheading{Parameters
}
738 \docparam{buffer
}{Buffer to be unread.
}
740 \docparam{nbytes
}{Number of bytes.
}
742 \wxheading{Return value
}
744 Returns a reference to the current object.
748 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
749 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
750 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
}
755 \membersection{wxSocketBase::Wait
}\label{wxsocketbasewait
}
757 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
759 This function waits until one of the following conditions is TRUE. Note
760 that it is recommended to use the individual Wait functions to wait for
761 the required condition, instead of this one.
764 \item The socket becomes readable.
765 \item The socket becomes writable.
766 \item An ongoing connection request has completed (only for clients)
767 \item An incoming connection request has arrived (only for servers)
768 \item The connection has been closed.
771 \wxheading{Parameters
}
773 \docparam{seconds
}{Number of seconds to wait.
774 If -
1, it will wait for the default timeout,
775 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
777 \docparam{millisecond
}{Number of milliseconds to wait.
}
779 \wxheading{Return value
}
781 Returns TRUE when any of the above conditions is satisfied,
782 FALSE if the timeout was reached.
786 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
787 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
788 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
793 \membersection{wxSocketBase::WaitForRead
}\label{wxsocketbasewaitforread
}
795 \func{bool
}{WaitForRead
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
797 This function waits until the socket is readable. This might mean that
798 queued data is available for reading or, for streamed sockets, that
799 the connection has been closed, so that a read operation will complete
800 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
801 is set, in which case the operation might still block).
803 \wxheading{Parameters
}
805 \docparam{seconds
}{Number of seconds to wait.
806 If -
1, it will wait for the default timeout,
807 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
809 \docparam{millisecond
}{Number of milliseconds to wait.
}
811 \wxheading{Return value
}
813 Returns TRUE if the socket becomes readable, FALSE on timeout.
817 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
},
818 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
819 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
824 \membersection{wxSocketBase::WaitForWrite
}\label{wxsocketbasewaitforwrite
}
826 \func{bool
}{WaitForWrite
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
828 This function waits until the socket becomes writable. This might mean that
829 the socket is ready to send new data, or for streamed sockets, that the
830 connection has been closed, so that a write operation is guaranteed to
831 complete immediately (unless the
{\bf wxSOCKET
\_WAITALL} flag is set,
832 in which case the operation might still block).
834 \wxheading{Parameters
}
836 \docparam{seconds
}{Number of seconds to wait.
837 If -
1, it will wait for the default timeout,
838 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
840 \docparam{millisecond
}{Number of milliseconds to wait.
}
842 \wxheading{Return value
}
844 Returns TRUE if the socket becomes writable, FALSE on timeout.
848 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
},
849 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
850 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
855 \membersection{wxSocketBase::WaitForLost
}\label{wxsocketbasewaitforlost
}
857 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
859 This function waits until the connection is lost. This may happen if
860 the peer gracefully closes the connection or if the connection breaks.
862 \wxheading{Parameters
}
864 \docparam{seconds
}{Number of seconds to wait.
865 If -
1, it will wait for the default timeout,
866 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
868 \docparam{millisecond
}{Number of milliseconds to wait.
}
870 \wxheading{Return value
}
872 Returns TRUE if the connection was lost, FALSE if the timeout was reached.
876 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
877 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
878 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
883 \membersection{wxSocketBase::Write
}\label{wxsocketbasewrite
}
885 \func{wxSocketBase\&
}{Write
}{\param{const void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
887 This function writes a buffer of
{\it nbytes
} bytes to the socket.
889 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
891 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
893 \wxheading{Parameters
}
895 \docparam{buffer
}{Buffer with the data to be sent.
}
897 \docparam{nbytes
}{Number of bytes.
}
899 \wxheading{Return value
}
901 Returns a reference to the current object.
903 \wxheading{Remark/Warning
}
905 The exact behaviour of wxSocketBase::Write depends on the combination
906 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
910 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
911 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
912 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
913 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
918 \membersection{wxSocketBase::WriteMsg
}\label{wxsocketbasewritemsg
}
920 \func{wxSocketBase\&
}{WriteMsg
}{\param{const void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
922 This function writes a buffer of
{\it nbytes
} bytes from the socket, but it
923 writes a short header before so that
\helpref{ReadMsg
}{wxsocketbasereadmsg
}
924 knows how much data should it actually read. So, a buffer sent with WriteMsg
925 {\bf must
} be read with ReadMsg. This function always waits for the entire
926 buffer to be sent, unless an error occurs.
928 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
930 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
932 \wxheading{Parameters
}
934 \docparam{buffer
}{Buffer with the data to be sent.
}
936 \docparam{nbytes
}{Number of bytes to send.
}
938 \wxheading{Return value
}
940 Returns a reference to the current object.
942 \wxheading{Remark/Warning
}
944 wxSocketBase::WriteMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
945 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
946 The exact behaviour of WriteMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
947 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
951 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
952 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
953 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
954 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
955 \helpref{wxSocketBase::ReadMsg
}{wxsocketbasereadmsg
}
958 % ---------------------------------------------------------------------------
959 % CLASS wxSocketClient
960 % ---------------------------------------------------------------------------
962 \section{\class{wxSocketClient
}}\label{wxsocketclient
}
964 \wxheading{Derived from
}
966 \helpref{wxSocketBase
}{wxsocketbase
}
968 \wxheading{Include files
}
972 % ---------------------------------------------------------------------------
974 % ---------------------------------------------------------------------------
978 \membersection{wxSocketClient::wxSocketClient
}
980 \func{}{wxSocketClient
}{\param{wxSocketFlags
}{ flags = wxSOCKET_NONE
}}
984 \wxheading{Parameters
}
986 \docparam{flags
}{Socket flags (See
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
})
}
991 \membersection{wxSocketClient::
\destruct{wxSocketClient
}}
993 \func{}{\destruct{wxSocketClient
}}{\void}
1000 \membersection{wxSocketClient::Connect
}\label{wxsocketclientconnect
}
1002 \func{bool
}{Connect
}{\param{wxSockAddress\&
}{ address
},
\param{bool
}{ wait = TRUE
}}
1004 Connects to a server using the specified address.
1006 If
{\it wait
} is TRUE, Connect will wait until the connection completes.
1007 {\bf Warning:
} This will block the GUI.
1009 If
{\it wait
} is FALSE, Connect will try to establish the connection and
1010 return immediately, without blocking the GUI. When used this way, even if
1011 Connect returns FALSE, the connection request can be completed later.
1012 To detect this, use
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
},
1013 or catch
{\bf wxSOCKET
\_CONNECTION} events (for successful establishment)
1014 and
{\bf wxSOCKET
\_LOST} events (for connection failure).
1016 \wxheading{Parameters
}
1018 \docparam{address
}{Address of the server.
}
1020 \docparam{wait
}{If TRUE, waits for the connection to complete.
}
1022 \wxheading{Return value
}
1024 Returns TRUE if the connection is established and no error occurs.
1026 If
{\it wait
} was TRUE, and Connect returns FALSE, an error occured
1027 and the connection failed.
1029 If
{\it wait
} was FALSE, and Connect returns FALSE, you should still
1030 be prepared to handle the completion of this connection request, either
1031 with
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
} or by watching
1032 {\bf wxSOCKET
\_CONNECTION} and
{\bf wxSOCKET
\_LOST} events.
1034 \wxheading{See also
}
1036 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
},
1037 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1038 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
}
1043 \membersection{wxSocketClient::WaitOnConnect
}\label{wxsocketclientwaitonconnect
}
1045 \func{bool
}{WaitOnConnect
}{\param{long
}{ seconds = -
1},
\param{long
}{ milliseconds =
0}}
1047 Wait until a connection request completes, or until the specified timeout
1048 elapses. Use this function after issuing a call to
\helpref{Connect
}{wxsocketclientconnect
}
1049 with
{\it wait
} set to FALSE.
1051 \wxheading{Parameters
}
1053 \docparam{seconds
}{Number of seconds to wait.
1054 If -
1, it will wait for the default timeout,
1055 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
1057 \docparam{millisecond
}{Number of milliseconds to wait.
}
1059 \wxheading{Return value
}
1061 WaitOnConnect returns TRUE if the connection request completes. This
1062 does not necessarily mean that the connection was succesfully established;
1063 it might also happen that the connection was refused by the peer. Use
1064 \helpref{IsConnected
}{wxsocketbaseisconnected
} to distinguish between
1065 these two situations.
1067 If the timeout elapses, WaitOnConnect returns FALSE.
1069 These semantics allow code like this:
1072 // Issue the connection request
1073 client->Connect(addr, FALSE);
1075 // Wait until the request completes or until we decide to give up
1076 bool waitmore = TRUE;
1077 while ( !client->WaitOnConnect(seconds, millis) && waitmore )
1079 // possibly give some feedback to the user,
1080 // and update waitmore if needed.
1082 bool success = client->IsConnected();
1085 \wxheading{See also
}
1087 \helpref{wxSocketClient::Connect
}{wxsocketclientconnect
},
1088 \helpref{wxSocketBase::IsConnected
}{wxsocketbaseisconnected
}
1090 % ---------------------------------------------------------------------------
1091 % CLASS: wxSocketEvent
1092 % ---------------------------------------------------------------------------
1093 \section{\class{wxSocketEvent
}}\label{wxsocketevent
}
1095 This event class contains information about socket events.
1097 \wxheading{Derived from
}
1099 \helpref{wxEvent
}{wxevent
}
1101 \wxheading{Include files
}
1105 \wxheading{Event table macros
}
1107 To process a socket event, use these event handler macros to direct input to member
1108 functions that take a wxSocketEvent argument.
1111 \begin{twocollist
}\itemsep=
0pt
1112 \twocolitem{{\bf EVT
\_SOCKET(id, func)
}}{Process a socket event, supplying the member function.
}
1115 \wxheading{See also
}
1117 \helpref{wxSocketBase
}{wxsocketbase
},
1118 \helpref{wxSocketClient
}{wxsocketclient
},
1119 \helpref{wxSocketServer
}{wxsocketserver
}
1121 \latexignore{\rtfignore{\wxheading{Members
}}}
1123 \membersection{wxSocketEvent::wxSocketEvent
}
1125 \func{}{wxSocketEvent
}{\param{int
}{ id =
0}}
1129 \membersection{wxSocketEvent::GetSocket
}\label{wxsocketeventgetsocket
}
1131 \constfunc{wxSocketBase *
}{GetSocket
}{\void}
1133 Returns the socket object to which this event refers to. This makes
1134 it possible to use the same event handler for different sockets.
1136 \membersection{wxSocketEvent::GetSocketEvent
}\label{wxsocketeventgetsocketevent
}
1138 \constfunc{wxSocketNotify
}{GetSocketEvent
}{\void}
1140 Returns the socket event type.
1142 % ---------------------------------------------------------------------------
1143 % CLASS: wxSocketServer
1144 % ---------------------------------------------------------------------------
1145 \section{\class{wxSocketServer
}}\label{wxsocketserver
}
1147 \wxheading{Derived from
}
1149 \helpref{wxSocketBase
}{wxsocketbase
}
1151 \wxheading{Include files
}
1155 % ---------------------------------------------------------------------------
1157 % ---------------------------------------------------------------------------
1158 \latexignore{\rtfignore{\wxheading{Members
}}}
1163 \membersection{wxSocketServer::wxSocketServer
}\label{wxsocketserverconstr
}
1165 \func{}{wxSocketServer
}{\param{wxSockAddress\&
}{ address
},
\param{wxSocketFlags
}{ flags = wxSOCKET_NONE
}}
1167 Constructs a new server and tries to bind to the specified
{\it address
}.
1168 Before trying to accept new connections, test whether it succeeded with
1169 \helpref{wxSocketBase::Ok
}{wxsocketbaseok
}.
1171 \wxheading{Parameters
}
1173 \docparam{address
}{Specifies the local address for the server (e.g. port number).
}
1175 \docparam{flags
}{Socket flags (See
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
})
}
1180 \membersection{wxSocketServer::
\destruct{wxSocketServer
}}
1182 \func{}{\destruct{wxSocketServer
}}{\void}
1184 Destructor (it doesn't close the accepted connections).
1189 \membersection{wxSocketServer::Accept
}\label{wxsocketserveraccept
}
1191 \func{wxSocketBase *
}{Accept
}{\param{bool
}{ wait = TRUE
}}
1193 Accepts an incoming connection request, and creates a new
1194 \helpref{wxSocketBase
}{wxsocketbase
} object which represents
1195 the server-side of the connection.
1197 If
{\it wait
} is TRUE and there are no pending connections to be
1198 accepted, it will wait for the next incoming connection to arrive.
1199 {\bf Warning:
} This will block the GUI.
1201 If
{\it wait
} is FALSE, it will try to accept a pending connection
1202 if there is one, but it will always return immediately without blocking
1203 the GUI. If you want to use Accept in this way, you can either check for
1204 incoming connections with
\helpref{WaitForAccept
}{wxsocketserverwaitforaccept
}
1205 or catch
{\bf wxSOCKET
\_CONNECTION} events, then call Accept once you know
1206 that there is an incoming connection waiting to be accepted.
1208 \wxheading{Return value
}
1210 Returns an opened socket connection, or NULL if an error occured or
1211 if the
{\it wait
} parameter was FALSE and there were no pending
1214 \wxheading{See also
}
1216 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
1217 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1218 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
1219 \helpref{wxSocketServer::AcceptWith
}{wxsocketserveracceptwith
}
1224 \membersection{wxSocketServer::AcceptWith
}\label{wxsocketserveracceptwith
}
1226 \func{bool
}{AcceptWith
}{\param{wxSocketBase\&
}{ socket
},
\param{bool
}{ wait = TRUE
}}
1228 Accept an incoming connection using the specified socket object.
1230 \wxheading{Parameters
}
1232 \docparam{socket
}{Socket to be initialized
}
1234 \wxheading{Return value
}
1236 Returns TRUE on success, or FALSE if an error occured or if the
1237 {\it wait
} parameter was FALSE and there were no pending
1240 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
1241 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1242 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
1243 \helpref{wxSocketServer::Accept
}{wxsocketserveraccept
} for a detailed explanation
1248 \membersection{wxSocketServer::WaitForAccept
}\label{wxsocketserverwaitforaccept
}
1250 \func{bool
}{WaitForAccept
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
1252 This function waits for an incoming connection. Use it if you want to call
1253 \helpref{Accept
}{wxsocketserveraccept
} or
\helpref{AcceptWith
}{wxsocketserveracceptwith
}
1254 with
{\it wait
} set to FALSE, to detect when an incoming connection is waiting
1257 \wxheading{Parameters
}
1259 \docparam{seconds
}{Number of seconds to wait.
1260 If -
1, it will wait for the default timeout,
1261 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
1263 \docparam{millisecond
}{Number of milliseconds to wait.
}
1265 \wxheading{Return value
}
1267 Returns TRUE if an incoming connection arrived, FALSE if the timeout elapsed.
1269 \wxheading{See also
}
1271 \helpref{wxSocketServer::Accept
}{wxsocketserveraccept
},
1272 \helpref{wxSocketServer::AcceptWith
}{wxsocketserveracceptwith
}