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
}
110 \membersection{Socket state
}
112 Functions to retrieve current state and miscellaneous info.
114 \helpref{Error
}{wxsocketbaseerror
}\\
115 \helpref{GetLocal
}{wxsocketbasegetlocal
}\\
116 \helpref{GetPeer
}{wxsocketbasegetpeer
}
117 \helpref{IsConnected
}{wxsocketbaseisconnected
}\\
118 \helpref{IsData
}{wxsocketbaseisdata
}\\
119 \helpref{IsDisconnected
}{wxsocketbaseisdisconnected
}\\
120 \helpref{LastCount
}{wxsocketbaselastcount
}\\
121 \helpref{LastError
}{wxsocketbaselasterror
}\\
122 \helpref{Ok
}{wxsocketbaseok
}\\
123 \helpref{SaveState
}{wxsocketbasesavestate
}\\
124 \helpref{RestoreState
}{wxsocketbaserestorestate
}
126 \membersection{Basic IO
}
128 Functions that perform basic IO functionality.
130 \helpref{Close
}{wxsocketbaseclose
}\\
131 \helpref{Discard
}{wxsocketbasediscard
}\\
132 \helpref{Peek
}{wxsocketbasepeek
}\\
133 \helpref{Read
}{wxsocketbaseread
}\\
134 \helpref{ReadMsg
}{wxsocketbasereadmsg
}\\
135 \helpref{Unread
}{wxsocketbaseunread
}\\
136 \helpref{Write
}{wxsocketbasewrite
}\\
137 \helpref{WriteMsg
}{wxsocketbasewritemsg
}
139 Functions that perform a timed wait on a certain IO condition.
141 \helpref{Wait
}{wxsocketbasewait
}\\
142 \helpref{WaitForRead
}{wxsocketbasewaitforread
}\\
143 \helpref{WaitForWrite
}{wxsocketbasewaitforwrite
}\\
144 \helpref{WaitForLost
}{wxsocketbasewaitforlost
}
146 Functions that allow applications to customize socket IO as needed.
148 \helpref{SetFlags
}{wxsocketbasesetflags
}\\
149 \helpref{SetTimeout
}{wxsocketbasesettimeout
}
151 \membersection{Handling socket events
}
153 Functions that allow applications to receive socket events.
155 \helpref{Notify
}{wxsocketbasenotify
}\\
156 \helpref{SetNotify
}{wxsocketbasesetnotify
}\\
157 \helpref{SetEventHandler
}{wxsocketbaseseteventhandler
}
159 Callback functions are also available, but they are provided for backwards
160 compatibility only. Their use is discouraged in favour of events, and should
161 be considered deprecated.
163 \helpref{Callback
}{wxsocketbasecallback
}\\
164 \helpref{CallbackData
}{wxsocketbasecallbackdata
}
167 % ---------------------------------------------------------------------------
169 % ---------------------------------------------------------------------------
171 \helponly{\insertatlevel{2}{
177 \membersection{wxSocketBase::wxSocketBase
}\label{wxsocketbaseconstruct
}
179 \func{}{wxSocketBase
}{\void}
181 Default constructor. Don't use it directly; instead, use
182 \helpref{wxSocketClient
}{wxsocketclient
} to construct a socket client, or
183 \helpref{wxSocketServer
}{wxsocketserver
} to construct a socket server.
185 \membersection{wxSocketBase::
\destruct{wxSocketBase
}}\label{wxsocketbasedestruct
}
187 \func{}{\destruct{wxSocketBase
}}{\void}
194 \membersection{wxSocketBase::Callback
}\label{wxsocketbasecallback
}
196 \func{wxSocketBase::wxSockCbk
}{Callback
}{\param{wxSocketBase::wxSockCbk
}{ callback
}}
198 You can setup a callback function to be called when an event occurs.
199 The function will be called only for those events for which notification
200 has been enabled with
\helpref{Notify
}{wxsocketbasenotify
} and
201 \helpref{SetNotify
}{wxsocketbasesetnotify
}. The prototype of the
202 callback must be as follows:
205 void SocketCallback(wxSocketBase& sock, wxSocketNotify evt, char *cdata);
208 The first parameter is a reference to the socket object in which the
209 event occured. The second parameter tells you which event occured.
210 (See
\helpref{wxSocket events
}{wxsocketbase
}). The third parameter
211 is the user data you specified using
212 \helpref{CallbackData
}{wxsocketbasecallbackdata
}.
214 Note that events are preferred over callbacks where possible.
216 \wxheading{Return value
}
218 A pointer to the previous callback.
222 \helpref{wxSocketBase::CallbackData
}{wxsocketbasecallbackdata
},
223 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
224 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
}
229 \membersection{wxSocketBase::CallbackData
}\label{wxsocketbasecallbackdata
}
231 \func{char *
}{CallbackData
}{\param{char *
}{cdata
}}
233 This function sets the the user data which will be passed to a
234 callback function set via
\helpref{Callback
}{wxsocketbasecallback
}.
236 Note that events are preferred over callbacks where possible.
238 \wxheading{Return value
}
240 A pointer to the previous user data.
242 \helpref{wxSocketBase::Callback
}{wxsocketbasecallback
},
243 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
244 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
}
249 \membersection{wxSocketBase::Close
}\label{wxsocketbaseclose
}
251 \func{void
}{Close
}{\void}
253 This function shuts down the socket, disabling further transmission and
254 reception of data; it also disables events for the socket and frees the
255 associated system resources. If you destroy a socket, Close is automatically
258 \wxheading{Remark/Warning
}
260 Although Close immediately disables events for the socket, it is possible
261 that event messages may be waiting in the application's event queue. The
262 application must therefore be prepared to handle socket event messages
263 even after calling Close.
268 \membersection{wxSocketBase::Discard
}\label{wxsocketbasediscard
}
270 \func{wxSocketBase\&
}{Discard
}{\void}
272 This function simply deletes all bytes in the incoming queue. This function
273 always returns immediately and its operation is not affected by IO flags.
275 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually discarded.
277 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return FALSE.
282 \membersection{wxSocketBase::Error
}\label{wxsocketbaseerror
}
284 \constfunc{bool
}{Error
}{\void}
286 Returns TRUE if an error occured in the last IO operation.
288 Use this function to check for an error condition after one of the
289 following calls: Discard, Peek, Read, ReadMsg, Unread, Write, WriteMsg.
294 \membersection{wxSocketBase::GetLocal
}\label{wxsocketbasegetlocal
}
296 \constfunc{bool
}{GetLocal
}{\param{wxSockAddress\&
}{addr
\_man}}
298 This function returns the local address field of the socket. The local
299 address field contains the complete local address of the socket (local
300 address, local port, ...).
302 \wxheading{Return value
}
304 It returns TRUE if no errors happened, FALSE otherwise.
309 \membersection{wxSocketBase::GetPeer
}\label{wxsocketbasegetpeer
}
311 \constfunc{bool
}{GetPeer
}{\param{wxSockAddress\&
}{addr
\_man}}
313 This function returns the peer address field of the socket. The peer
314 address field contains the complete peer host address of the socket
315 (address, port, ...).
317 \wxheading{Return value
}
319 It returns TRUE if no errors happened, FALSE otherwise.
324 \membersection{wxSocketBase::IsConnected
}\label{wxsocketbaseisconnected
}
326 \constfunc{bool
}{IsConnected
}{\void}
328 Returns TRUE if the socket is connected.
333 \membersection{wxSocketBase::IsData
}\label{wxsocketbaseisdata
}
335 \constfunc{bool
}{IsData
}{\void}
337 This function waits until the socket is readable. This might mean that
338 queued data is available for reading or, for streamed sockets, that
339 the connection has been closed, so that a read operation will complete
340 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
341 is set, in which case the operation might still block).
343 \membersection{wxSocketBase::IsDisconnected
}\label{wxsocketbaseisdisconnected
}
348 \constfunc{bool
}{IsDisconnected
}{\void}
350 Returns TRUE if the socket is not connected.
352 \membersection{wxSocketBase::LastCount
}\label{wxsocketbaselastcount
}
357 \constfunc{wxUint32
}{LastCount
}{\void}
359 Returns the number of bytes read or written by the last IO call.
361 Use this function to get the number of bytes actually transferred
362 after using one of the following IO calls: Discard, Peek, Read,
363 ReadMsg, Unread, Write, WriteMsg.
368 \membersection{wxSocketBase::LastError
}\label{wxsocketbaselasterror
}
370 \constfunc{wxSocketError
}{LastError
}{\void}
372 Returns the last wxSocket error. See
\helpref{wxSocket errors
}{wxsocketbase
}.
374 Please note that this function merely returns the last error code,
375 but it should not be used to determine if an error has occured (this
376 is because successful operations do not change the LastError value).
377 Use
\helpref{Error
}{wxsocketbaseerror
} first, in order to determine
378 if the last IO call failed. If this returns TRUE, use LastError
379 to discover the cause of the error.
384 \membersection{wxSocketBase::Notify
}\label{wxsocketbasenotify
}
386 \func{void
}{Notify
}{\param{bool
}{ notify
}}
388 According to the
{\it notify
} value, this function enables
389 or disables socket events. If
{\it notify
} is TRUE, the events
390 configured with
\helpref{SetNotify
}{wxsocketbasesetnotify
} will
391 be sent to the application. If
{\it notify
} is FALSE; no events
397 \membersection{wxSocketBase::Ok
}\label{wxsocketbaseok
}
399 \constfunc{bool
}{Ok
}{\void}
401 Returns TRUE if the socket is initialized and ready and FALSE in other
407 \membersection{wxSocketBase::RestoreState
}\label{wxsocketbaserestorestate
}
409 \func{void
}{RestoreState
}{\void}
411 This function restores the previous state of the socket, as saved
412 with
\helpref{SaveState
}{wxsocketbasesavestate
}
414 Calls to SaveState and RestoreState can be nested.
418 \helpref{wxSocketBase::SaveState
}{wxsocketbasesavestate
}
423 \membersection{wxSocketBase::SaveState
}\label{wxsocketbasesavestate
}
425 \func{void
}{SaveState
}{\void}
427 This function saves the current state of the socket in a stack. Socket
428 state includes flags, as set with
\helpref{SetFlags
}{wxsocketbasesetflags
},
429 event mask, as set with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
430 \helpref{Notify
}{wxsocketbasenotify
}, and current settings for the
431 asynchronous callbacks, as set with
\helpref{Callback
}{wxsocketbasecallback
}
432 and
\helpref{CallbackData
}{wxsocketbasecallbackdata
}.
434 Calls to SaveState and RestoreState can be nested.
438 \helpref{wxSocketBase::RestoreState
}{wxsocketbaserestorestate
}
443 \membersection{wxSocketBase::SetEventHandler
}\label{wxsocketbaseseteventhandler
}
445 \func{void
}{SetEventHandler
}{\param{wxEvtHandler\&
}{ evt
\_hdlr},
\param{int
}{ id = -
1}}
447 Sets an event handler to be called when a socket event occurs. The
448 handler will be called for those events for which notification is
449 enabled with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
450 \helpref{Notify
}{wxsocketbasenotify
}.
452 You can also specify a callback function to be called when an event
453 occurs, although if possible, events should be used instead of callbacks.
454 See
\helpref{Callback
}{wxsocketbasecallback
} and
455 \helpref{CallbackData
}{wxsocketbasecallbackdata
}.
457 \wxheading{Parameters
}
459 \docparam{evt
\_hdlr}{Specifies the event handler you want to use.
}
461 \docparam{id
}{The id of socket event.
}
465 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
466 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
467 \helpref{wxSocketEvent
}{wxsocketevent
},
468 \helpref{wxEvtHandler
}{wxevthandler
},
469 \helpref{wxSocketBase::Callback
}{wxsocketbasecallback
},
470 \helpref{wxSocketBase::CallbackData
}{wxsocketbasecallbackdata
}
475 \membersection{wxSocketBase::SetFlags
}\label{wxsocketbasesetflags
}
477 \func{void
}{SetFlags
}{\param{wxSocketBase::wxSockFlags
}{ flags
}}
480 \begin{twocollist
}\itemsep=
0pt
481 \twocolitem{{\bf wxSOCKET
\_NONE}}{Normal functionality.
}
482 \twocolitem{{\bf wxSOCKET
\_NOWAIT}}{Read/write as much data as possible and return immediately.
}
483 \twocolitem{{\bf wxSOCKET
\_WAITALL}}{Wait for all required data to be read/written unless an error occurs.
}
484 \twocolitem{{\bf wxSOCKET
\_BLOCK}}{Block the GUI (do not yield) while reading/writing data.
}
487 A brief overview on how to use these flags follows.
489 If no flag is specified (this is the same as
{\bf wxSOCKET
\_NONE}),
490 IO calls will return after some data has been read or written, even
491 when the transfer might not be complete. This is the same as issuing
492 exactly one blocking low-level call to recv() or send(). Note that
493 {\it blocking
} here refers to when the function returns, not to whether
494 the GUI blocks during this time.
496 If
{\bf wxSOCKET
\_NOWAIT} is specified, IO calls will return immediately.
497 Read operations will retrieve only available data. Write operations will
498 write as much data as possible, depending on how much space is available
499 in the output buffer. This is the same as issuing exactly one nonblocking
500 low-level call to recv() or send(). Note that
{\it nonblocking
} here
501 refers to when the function returns, not to whether the GUI blocks during
504 If
{\bf wxSOCKET
\_WAITALL} is specified, IO calls won't return until ALL
505 the data has been read or written (or until an error occurs), blocking if
506 necessary, and issuing several low level calls if necessary. This is the
507 same as having a loop which makes as many blocking low-level calls to
508 recv() or send() as needed so as to transfer all the data. Note that
509 {\it blocking
} here refers to when the function returns, not to whether
510 the GUI blocks during this time.
512 The
{\bf wxSOCKET
\_BLOCK} flag controls whether the GUI blocks during
513 IO operations. If this flag is specified, the socket will not yield
514 during IO calls, so the GUI will remain blocked until the operation
515 completes. If it is not used, then the application must take extra
516 care to avoid unwanted reentrance.
520 {\bf wxSOCKET
\_NONE} will try to read at least SOME data, no matter how much.
522 {\bf wxSOCKET
\_NOWAIT} will always return immediately, even if it cannot
523 read or write ANY data.
525 {\bf wxSOCKET
\_WAITALL} will only return when it has read or written ALL
528 {\bf wxSOCKET
\_BLOCK} has nothing to do with the previous flags and
529 it controls whether the GUI blocks.
534 \membersection{wxSocketBase::SetNotify
}\label{wxsocketbasesetnotify
}
536 \func{void
}{SetNotify
}{\param{wxSocketEventFlags
}{ flags
}}
538 SetNotify specifies which socket events are to be sent to the event handler.
539 The
{\it flags
} parameter is a combination of flags ORed toghether. The
540 following flags can be used:
543 \begin{twocollist
}\itemsep=
0pt
544 \twocolitem{{\bf wxSOCKET
\_INPUT\_FLAG}}{to receive wxSOCKET
\_INPUT}
545 \twocolitem{{\bf wxSOCKET
\_OUTPUT\_FLAG}}{to receive wxSOCKET
\_OUTPUT}
546 \twocolitem{{\bf wxSOCKET
\_CONNECTION\_FLAG}}{to receive wxSOCKET
\_CONNECTION}
547 \twocolitem{{\bf wxSOCKET
\_LOST\_FLAG}}{to receive wxSOCKET
\_LOST}
553 sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
557 In this example, the user will be notified about incoming socket data and
558 whenever the connection is closed.
560 For more information on socket events see
\helpref{wxSocket events
}{wxsocketbase
}.
565 \membersection{wxSocketBase::SetTimeout
}\label{wxsocketbasesettimeout
}
567 \func{void
}{SetTimeout
}{\param{int
}{seconds
}}
569 This function sets the default socket timeout in seconds. This timeout
570 applies to all IO calls, and also to the
\helpref{Wait
}{wxsocketbasewait
}
571 family of functions if you don't specify a wait interval. Initially, the
572 default is set to
10 minutes.
577 \membersection{wxSocketBase::Peek
}\label{wxsocketbasepeek
}
579 \func{wxSocketBase\&
}{Peek
}{\param{char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
581 This function peeks a buffer of
{\it nbytes
} bytes from the socket.
582 Peeking a buffer doesn't delete it from the socket input queue.
584 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually peeked.
586 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
588 \wxheading{Parameters
}
590 \docparam{buffer
}{Buffer where to put peeked data.
}
592 \docparam{nbytes
}{Number of bytes.
}
594 \wxheading{Return value
}
596 Returns a reference to the current object.
598 \wxheading{Remark/Warning
}
600 The exact behaviour of wxSocketBase::Peek depends on the combination
601 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
605 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
606 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
607 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
608 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
613 \membersection{wxSocketBase::Read
}\label{wxsocketbaseread
}
615 \func{wxSocketBase\&
}{Read
}{\param{char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
617 This function reads a buffer of
{\it nbytes
} bytes from the socket.
619 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
621 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
623 \wxheading{Parameters
}
625 \docparam{buffer
}{Buffer where to put read 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::Read 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::ReadMsg
}\label{wxsocketbasereadmsg
}
650 \func{wxSocketBase\&
}{ReadMsg
}{\param{char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
652 This function reads a buffer sent by
\helpref{WriteMsg
}{wxsocketbasewritemsg
}
653 on a socket. If the buffer passed to the function isn't big enough, the
654 remaining bytes will be discarded. This function always waits for the
655 buffer to be entirely filled, unless an error occurs.
657 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
659 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
661 \wxheading{Parameters
}
663 \docparam{buffer
}{Buffer where to put read data.
}
665 \docparam{nbytes
}{Size of the buffer.
}
667 \wxheading{Return value
}
669 Returns a reference to the current object.
671 \wxheading{Remark/Warning
}
673 wxSocketBase::ReadMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
674 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
675 The exact behaviour of ReadMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
676 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
680 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
681 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
682 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
683 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
684 \helpref{wxSocketBase::WriteMsg
}{wxsocketbasewritemsg
}
689 \membersection{wxSocketBase::Unread
}\label{wxsocketbaseunread
}
691 \func{wxSocketBase\&
}{Unread
}{\param{const char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
693 This function unreads a buffer. That is, the data in the buffer is put back
694 in the incoming queue. This function is not affected by wxSocket flags.
696 If you use
\helpref{LastCount
}{wxsocketbaselastcount
}, it will always return
{\it nbytes
}.
698 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return FALSE.
700 \wxheading{Parameters
}
702 \docparam{buffer
}{Buffer to be unread.
}
704 \docparam{nbytes
}{Number of bytes.
}
706 \wxheading{Return value
}
708 Returns a reference to the current object.
712 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
713 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
714 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
}
719 \membersection{wxSocketBase::Wait
}\label{wxsocketbasewait
}
721 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
723 This function waits until one of the following conditions is TRUE. Note
724 that it is recommended to use the individual Wait functions to wait for
725 the required condition, instead of this one.
728 \item The socket becomes readable.
729 \item The socket becomes writable.
730 \item An ongoing connection request has completed (only for clients)
731 \item An incoming connection request has arrived (only for servers)
732 \item The connection has been closed.
735 \wxheading{Parameters
}
737 \docparam{seconds
}{Number of seconds to wait.
738 If -
1, it will wait for the default timeout,
739 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
741 \docparam{millisecond
}{Number of milliseconds to wait.
}
743 \wxheading{Return value
}
745 Returns TRUE when any of the above conditions is satisfied,
746 FALSE if the timeout was reached.
750 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
751 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
752 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
757 \membersection{wxSocketBase::WaitForRead
}\label{wxsocketbasewaitforread
}
759 \func{bool
}{WaitForRead
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
761 This function waits until the socket is readable. This might mean that
762 queued data is available for reading or, for streamed sockets, that
763 the connection has been closed, so that a read operation will complete
764 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
765 is set, in which case the operation might still block).
767 \wxheading{Parameters
}
769 \docparam{seconds
}{Number of seconds to wait.
770 If -
1, it will wait for the default timeout,
771 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
773 \docparam{millisecond
}{Number of milliseconds to wait.
}
775 \wxheading{Return value
}
777 Returns TRUE if the socket becomes readable, FALSE on timeout.
781 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
},
782 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
783 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
788 \membersection{wxSocketBase::WaitForWrite
}\label{wxsocketbasewaitforwrite
}
790 \func{bool
}{WaitForWrite
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
792 This function waits until the socket becomes writable. This might mean that
793 the socket is ready to send new data, or for streamed sockets, that the
794 connection has been closed, so that a write operation is guaranteed to
795 complete immediately (unless the
{\bf wxSOCKET
\_WAITALL} flag is set,
796 in which case the operation might still block).
798 \wxheading{Parameters
}
800 \docparam{seconds
}{Number of seconds to wait.
801 If -
1, it will wait for the default timeout,
802 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
804 \docparam{millisecond
}{Number of milliseconds to wait.
}
806 \wxheading{Return value
}
808 Returns TRUE if the socket becomes writable, FALSE on timeout.
812 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
},
813 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
814 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
819 \membersection{wxSocketBase::WaitForLost
}\label{wxsocketbasewaitforlost
}
821 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
823 This function waits until the connection is lost. This may happen if
824 the peer gracefully closes the connection or if the connection breaks.
826 \wxheading{Parameters
}
828 \docparam{seconds
}{Number of seconds to wait.
829 If -
1, it will wait for the default timeout,
830 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
832 \docparam{millisecond
}{Number of milliseconds to wait.
}
834 \wxheading{Return value
}
836 Returns TRUE if the connection was lost, FALSE if the timeout was reached.
840 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
841 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
842 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
}
847 \membersection{wxSocketBase::Write
}\label{wxsocketbasewrite
}
849 \func{wxSocketBase\&
}{Write
}{\param{const char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
851 This function writes a buffer of
{\it nbytes
} bytes to the socket.
853 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
855 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
857 \wxheading{Parameters
}
859 \docparam{buffer
}{Buffer with the data to be sent.
}
861 \docparam{nbytes
}{Number of bytes.
}
863 \wxheading{Return value
}
865 Returns a reference to the current object.
867 \wxheading{Remark/Warning
}
869 The exact behaviour of wxSocketBase::Write depends on the combination
870 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
874 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
875 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
876 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
877 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
882 \membersection{wxSocketBase::WriteMsg
}\label{wxsocketbasewritemsg
}
884 \func{wxSocketBase\&
}{WriteMsg
}{\param{const char *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
886 This function writes a buffer of
{\it nbytes
} bytes from the socket, but it
887 writes a short header before so that
\helpref{ReadMsg
}{wxsocketbasereadmsg
}
888 knows how much data should it actually read. So, a buffer sent with WriteMsg
889 {\bf must
} be read with ReadMsg. This function always waits for the entire
890 buffer to be sent, unless an error occurs.
892 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
894 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
896 \wxheading{Parameters
}
898 \docparam{buffer
}{Buffer with the data to be sent.
}
900 \docparam{nbytes
}{Number of bytes to send.
}
902 \wxheading{Return value
}
904 Returns a reference to the current object.
906 \wxheading{Remark/Warning
}
908 wxSocketBase::WriteMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
909 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
910 The exact behaviour of WriteMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
911 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
915 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
916 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
917 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
918 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
919 \helpref{wxSocketBase::ReadMsg
}{wxsocketbasereadmsg
}
922 % ---------------------------------------------------------------------------
923 % CLASS wxSocketClient
924 % ---------------------------------------------------------------------------
926 \section{\class{wxSocketClient
}}\label{wxsocketclient
}
928 \wxheading{Derived from
}
930 \helpref{wxSocketBase
}{wxsocketbase
}
932 \wxheading{Include files
}
936 % ---------------------------------------------------------------------------
938 % ---------------------------------------------------------------------------
942 \membersection{wxSocketClient::wxSocketClient
}
944 \func{}{wxSocketClient
}{\param{wxSockFlags
}{ flags = wxSocketBase::NONE
}}
948 \wxheading{Parameters
}
950 \docparam{flags
}{Socket flags (See
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
})
}
955 \membersection{wxSocketClient::
\destruct{wxSocketClient
}}
957 \func{}{\destruct{wxSocketClient
}}{\void}
964 \membersection{wxSocketClient::Connect
}\label{wxsocketclientconnect
}
966 \func{bool
}{Connect
}{\param{wxSockAddress\&
}{ address
},
\param{bool
}{ wait = TRUE
}}
968 Connects to a server using the specified address.
970 If
{\it wait
} is TRUE, Connect will wait until the connection completes.
971 {\bf Warning:
} This will block the GUI.
973 If
{\it wait
} is FALSE, Connect will try to establish the connection and
974 return immediately, without blocking the GUI. When used this way, even if
975 Connect returns FALSE, the connection request can be completed later.
976 To detect this, use
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
},
977 or catch
{\bf wxSOCKET
\_CONNECTION} events (for successful establishment)
978 and
{\bf wxSOCKET
\_LOST} events (for connection failure).
980 \wxheading{Parameters
}
982 \docparam{address
}{Address of the server.
}
984 \docparam{wait
}{If TRUE, waits for the connection to complete.
}
986 \wxheading{Return value
}
988 Returns TRUE if the connection is established and no error occurs.
990 If
{\it wait
} was TRUE, and Connect returns FALSE, an error occured
991 and the connection failed.
993 If
{\it wait
} was FALSE, and Connect returns FALSE, you should still
994 be prepared to handle the completion of this connection request, either
995 with
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
} or by watching
996 {\bf wxSOCKET
\_CONNECTION} and
{\bf wxSOCKET
\_LOST} events.
1000 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
},
1001 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1002 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
}
1007 \membersection{wxSocketClient::WaitOnConnect
}\label{wxsocketclientwaitonconnect
}
1009 \func{bool
}{WaitOnConnect
}{\param{long
}{ seconds = -
1},
\param{long
}{ milliseconds =
0}}
1011 Wait until a connection request completes, or until the specified timeout
1012 elapses. Use this function after issuing a call to
\helpref{Connect
}{wxsocketclientconnect
}
1013 with
{\it wait
} set to FALSE.
1015 \wxheading{Parameters
}
1017 \docparam{seconds
}{Number of seconds to wait.
1018 If -
1, it will wait for the default timeout,
1019 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
1021 \docparam{millisecond
}{Number of milliseconds to wait.
}
1023 \wxheading{Return value
}
1025 WaitOnConnect returns TRUE if the connection request completes. This
1026 does not necessarily mean that the connection was succesfully established;
1027 it might also happen that the connection was refused by the peer. Use
1028 \helpref{IsConnected
}{wxsocketbaseisconnected
} to distinguish between
1029 these two situations.
1031 If the timeout elapses, WaitOnConnect returns FALSE.
1033 These semantics allow code like this:
1036 // Issue the connection request
1037 client->Connect(addr, FALSE);
1039 // Wait until the request completes or until we decide to give up
1040 bool waitmore = TRUE;
1041 while ( !client->WaitOnConnect(seconds, millis) && waitmore )
1043 // possibly give some feedback to the user,
1044 // and update waitmore if needed.
1046 bool success = client->IsConnected();
1049 \wxheading{See also
}
1051 \helpref{wxSocketClient::Connect
}{wxsocketclientconnect
},
1052 \helpref{wxSocketBase::IsConnected
}{wxsocketbaseisconnected
}
1054 % ---------------------------------------------------------------------------
1055 % CLASS: wxSocketEvent
1056 % ---------------------------------------------------------------------------
1057 \section{\class{wxSocketEvent
}}\label{wxsocketevent
}
1059 This event class contains information about socket events.
1061 \wxheading{Derived from
}
1063 \helpref{wxEvent
}{wxevent
}
1065 \wxheading{Include files
}
1069 \wxheading{Event table macros
}
1071 To process a socket event, use these event handler macros to direct input to member
1072 functions that take a wxSocketEvent argument.
1075 \begin{twocollist
}\itemsep=
0pt
1076 \twocolitem{{\bf EVT
\_SOCKET(id, func)
}}{Process a socket event, supplying the member function.
}
1079 \wxheading{See also
}
1081 \helpref{wxSocketBase
}{wxsocketbase
},
1082 \helpref{wxSocketClient
}{wxsocketclient
},
1083 \helpref{wxSocketServer
}{wxsocketserver
}
1085 \latexignore{\rtfignore{\wxheading{Members
}}}
1087 \membersection{wxSocketEvent::wxSocketEvent
}
1089 \func{}{wxSocketEvent
}{\param{int
}{ id =
0}}
1093 \membersection{wxSocketEvent::Socket
}\label{wxsocketeventsocket
}
1095 \constfunc{wxSocketBase *
}{Socket
}{\void}
1097 Returns the socket object to which this event refers to. This makes
1098 it possible to use the same event handler for different sockets.
1100 \membersection{wxSocketEvent::SocketEvent
}\label{wxsocketeventsocketevent
}
1102 \constfunc{wxSocketNotify
}{SocketEvent
}{\void}
1104 Returns the socket event type.
1106 % ---------------------------------------------------------------------------
1107 % CLASS: wxSocketServer
1108 % ---------------------------------------------------------------------------
1109 \section{\class{wxSocketServer
}}\label{wxsocketserver
}
1111 \wxheading{Derived from
}
1113 \helpref{wxSocketBase
}{wxsocketbase
}
1115 \wxheading{Include files
}
1119 % ---------------------------------------------------------------------------
1121 % ---------------------------------------------------------------------------
1122 \latexignore{\rtfignore{\wxheading{Members
}}}
1127 \membersection{wxSocketServer::wxSocketServer
}\label{wxsocketserverconstr
}
1129 \func{}{wxSocketServer
}{\param{wxSockAddress\&
}{ address
},
\param{wxSockFlags
}{ flags = wxSocketBase::NONE
}}
1131 Constructs a new server and tries to bind to the specified
{\it address
}.
1132 Before trying to accept new connections, test whether it succeeded with
1133 \helpref{wxSocketBase::Ok
}{wxsocketbaseok
}.
1135 \wxheading{Parameters
}
1137 \docparam{address
}{Specifies the local address for the server (e.g. port number).
}
1139 \docparam{flags
}{Socket flags (See
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
})
}
1144 \membersection{wxSocketServer::
\destruct{wxSocketServer
}}
1146 \func{}{\destruct{wxSocketServer
}}{\void}
1148 Destructor (it doesn't close the accepted connections).
1153 \membersection{wxSocketServer::Accept
}\label{wxsocketserveraccept
}
1155 \func{wxSocketBase *
}{Accept
}{\param{bool
}{ wait = TRUE
}}
1157 Accepts an incoming connection request, and creates a new
1158 \helpref{wxSocketBase
}{wxsocketbase
} object which represents
1159 the server-side of the connection.
1161 If
{\it wait
} is TRUE and there are no pending connections to be
1162 accepted, it will wait for the next incoming connection to arrive.
1163 {\bf Warning:
} This will block the GUI.
1165 If
{\it wait
} is FALSE, it will try to accept a pending connection
1166 if there is one, but it will always return immediately without blocking
1167 the GUI. If you want to use Accept in this way, you can either check for
1168 incoming connections with
\helpref{WaitForAccept
}{wxsocketserverwaitforaccept
}
1169 or catch
{\bf wxSOCKET
\_CONNECTION} events, then call Accept once you know
1170 that there is an incoming connection waiting to be accepted.
1172 \wxheading{Return value
}
1174 Returns an opened socket connection, or NULL if an error occured or
1175 if the
{\it wait
} parameter was FALSE and there were no pending
1178 \wxheading{See also
}
1180 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
1181 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1182 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
1183 \helpref{wxSocketServer::AcceptWith
}{wxsocketserveracceptwith
}
1188 \membersection{wxSocketServer::AcceptWith
}\label{wxsocketserveracceptwith
}
1190 \func{bool
}{AcceptWith
}{\param{wxSocketBase\&
}{ socket
},
\param{bool
}{ wait = TRUE
}}
1192 Accept an incoming connection using the specified socket object.
1194 \wxheading{Parameters
}
1196 \docparam{socket
}{Socket to be initialized
}
1198 \wxheading{Return value
}
1200 Returns TRUE on success, or FALSE if an error occured or if the
1201 {\it wait
} parameter was FALSE and there were no pending
1204 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
1205 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1206 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
1207 \helpref{wxSocketServer::Accept
}{wxsocketserveraccept
} for a detailed explanation
1212 \membersection{wxSocketServer::WaitForAccept
}\label{wxsocketserverwaitforaccept
}
1214 \func{bool
}{WaitForAccept
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
1216 This function waits for an incoming connection. Use it if you want to call
1217 \helpref{Accept
}{wxsocketserveraccept
} or
\helpref{AcceptWith
}{wxsocketserveracceptwith
}
1218 with
{\it wait
} set to FALSE, to detect when an incoming connection is waiting
1221 \wxheading{Parameters
}
1223 \docparam{seconds
}{Number of seconds to wait.
1224 If -
1, it will wait for the default timeout,
1225 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
1227 \docparam{millisecond
}{Number of milliseconds to wait.
}
1229 \wxheading{Return value
}
1231 Returns TRUE if an incoming connection arrived, FALSE if the timeout elapsed.
1233 \wxheading{See also
}
1235 \helpref{wxSocketServer::Accept
}{wxsocketserveraccept
},
1236 \helpref{wxSocketServer::AcceptWith
}{wxsocketserveracceptwith
}