1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %% Purpose: wxSocket docs
4 %% Author: Guillermo Rodriguez Garcia <guille@iies.es>
8 %% Copyright: (c) wxWidgets team
9 %% License: wxWindows license
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 Note: (Workaround for implementation limitation for wxWidgets up to
2.5.x)
18 If you want to use sockets or derived classes such as wxFTP in a secondary thread,
19 call wxSocketBase::Initialize() (undocumented) from the main thread before creating
20 any sockets - in wxApp::OnInit for example.
21 See http://wiki.wxwidgets.org/wiki.pl?WxSocket or
22 http://www.litwindow.com/knowhow/knowhow.html for more details.
24 \wxheading{Derived from
}
26 \helpref{wxObject
}{wxobject
}
28 \wxheading{Include files
}
32 \wxheading{wxSocket errors
}
35 \begin{twocollist
}\itemsep=
0pt
36 \twocolitem{{\bf wxSOCKET
\_NOERROR}}{No error happened.
}
37 \twocolitem{{\bf wxSOCKET
\_INVOP}}{Invalid operation.
}
38 \twocolitem{{\bf wxSOCKET
\_IOERR}}{Input/Output error.
}
39 \twocolitem{{\bf wxSOCKET
\_INVADDR}}{Invalid address passed to wxSocket.
}
40 \twocolitem{{\bf wxSOCKET
\_INVSOCK}}{Invalid socket (uninitialized).
}
41 \twocolitem{{\bf wxSOCKET
\_NOHOST}}{No corresponding host.
}
42 \twocolitem{{\bf wxSOCKET
\_INVPORT}}{Invalid port.
}
43 \twocolitem{{\bf wxSOCKET
\_WOULDBLOCK}}{The socket is non-blocking and the operation would block.
}
44 \twocolitem{{\bf wxSOCKET
\_TIMEDOUT}}{The timeout for this operation expired.
}
45 \twocolitem{{\bf wxSOCKET
\_MEMERR}}{Memory exhausted.
}
48 \wxheading{wxSocket events
}
51 \begin{twocollist
}\itemsep=
0pt
52 \twocolitem{{\bf wxSOCKET
\_INPUT}}{There is data available for reading.
}
53 \twocolitem{{\bf wxSOCKET
\_OUTPUT}}{The socket is ready to be written to.
}
54 \twocolitem{{\bf wxSOCKET
\_CONNECTION}}{Incoming connection request (server), or successful connection establishment (client).
}
55 \twocolitem{{\bf wxSOCKET
\_LOST}}{The connection has been closed.
}
58 A brief note on how to use these events:
60 The
{\bf wxSOCKET
\_INPUT} event will be issued whenever there is data
61 available for reading. This will be the case if the input queue was
62 empty and new data arrives, or if the application has read some data
63 yet there is still more data available. This means that the application
64 does not need to read all available data in response to a
65 {\bf wxSOCKET
\_INPUT} event, as more events will be produced as
68 The
{\bf wxSOCKET
\_OUTPUT} event is issued when a socket is first
69 connected with
\helpref{Connect
}{wxsocketclientconnect
} or accepted
70 with
\helpref{Accept
}{wxsocketserveraccept
}. After that, new
71 events will be generated only after an output operation fails
72 with
{\bf wxSOCKET
\_WOULDBLOCK} and buffer space becomes available
73 again. This means that the application should assume that it
74 can write data to the socket until an
{\bf wxSOCKET
\_WOULDBLOCK}
75 error occurs; after this, whenever the socket becomes writable
76 again the application will be notified with another
77 {\bf wxSOCKET
\_OUTPUT} event.
79 The
{\bf wxSOCKET
\_CONNECTION} event is issued when a delayed connection
80 request completes successfully (client) or when a new connection arrives
81 at the incoming queue (server).
83 The
{\bf wxSOCKET
\_LOST} event is issued when a close indication is
84 received for the socket. This means that the connection broke down or
85 that it was closed by the peer. Also, this event will be issued if
86 a connection request fails.
88 \wxheading{Event handling
}
90 To process events coming from a socket object, use the following event
91 handler macro to direct events to member functions that take
92 a
\helpref{wxSocketEvent
}{wxsocketevent
} argument.
95 \begin{twocollist
}\itemsep=
0pt
96 \twocolitem{{\bf EVT
\_SOCKET(id, func)
}}{Process a wxEVT
\_SOCKET event.
}
101 \helpref{wxSocketEvent
}{wxsocketevent
},
102 \helpref{wxSocketClient
}{wxsocketclient
},
103 \helpref{wxSocketServer
}{wxsocketserver
},
104 \helpref{Sockets sample
}{samplesockets
}
106 % ---------------------------------------------------------------------------
108 % ---------------------------------------------------------------------------
110 \latexignore{\rtfignore{\wxheading{Function groups
}}}
112 \membersection{Construction and destruction
}\label{socketconstruction
}
114 \helpref{wxSocketBase
}{wxsocketbaseconstruct
}\\
115 \helpref{\destruct{wxSocketBase
}}{wxsocketbasedestruct
}\\
116 \helpref{Destroy
}{wxsocketbasedestroy
}
118 \membersection{Socket state
}\label{socketstate
}
120 Functions to retrieve current state and miscellaneous info.
122 \helpref{Error
}{wxsocketbaseerror
}\\
123 \helpref{GetLocal
}{wxsocketbasegetlocal
}\\
124 \helpref{GetPeer
}{wxsocketbasegetpeer
}
125 \helpref{IsConnected
}{wxsocketbaseisconnected
}\\
126 \helpref{IsData
}{wxsocketbaseisdata
}\\
127 \helpref{IsDisconnected
}{wxsocketbaseisdisconnected
}\\
128 \helpref{LastCount
}{wxsocketbaselastcount
}\\
129 \helpref{LastError
}{wxsocketbaselasterror
}\\
130 \helpref{Ok
}{wxsocketbaseok
}\\
131 \helpref{SaveState
}{wxsocketbasesavestate
}\\
132 \helpref{RestoreState
}{wxsocketbaserestorestate
}
134 \membersection{Basic IO
}\label{socketbasicio
}
136 Functions that perform basic IO functionality.
138 \helpref{Close
}{wxsocketbaseclose
}\\
139 \helpref{Discard
}{wxsocketbasediscard
}\\
140 \helpref{Peek
}{wxsocketbasepeek
}\\
141 \helpref{Read
}{wxsocketbaseread
}\\
142 \helpref{ReadMsg
}{wxsocketbasereadmsg
}\\
143 \helpref{Unread
}{wxsocketbaseunread
}\\
144 \helpref{Write
}{wxsocketbasewrite
}\\
145 \helpref{WriteMsg
}{wxsocketbasewritemsg
}
147 Functions that perform a timed wait on a certain IO condition.
149 \helpref{InterruptWait
}{wxsocketbaseinterruptwait
}\\
150 \helpref{Wait
}{wxsocketbasewait
}\\
151 \helpref{WaitForLost
}{wxsocketbasewaitforlost
}\\
152 \helpref{WaitForRead
}{wxsocketbasewaitforread
}\\
153 \helpref{WaitForWrite
}{wxsocketbasewaitforwrite
}\\
157 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
}\\
158 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
}
160 Functions that allow applications to customize socket IO as needed.
162 \helpref{GetFlags
}{wxsocketbasegetflags
}\\
163 \helpref{SetFlags
}{wxsocketbasesetflags
}\\
164 \helpref{SetTimeout
}{wxsocketbasesettimeout
}
166 \membersection{Handling socket events
}\label{socketevents
}
168 Functions that allow applications to receive socket events.
170 \helpref{Notify
}{wxsocketbasenotify
}\\
171 \helpref{SetNotify
}{wxsocketbasesetnotify
}\\
172 \helpref{GetClientData
}{wxsocketbasegetclientdata
}\\
173 \helpref{SetClientData
}{wxsocketbasesetclientdata
}\\
174 \helpref{SetEventHandler
}{wxsocketbaseseteventhandler
}
177 % ---------------------------------------------------------------------------
179 % ---------------------------------------------------------------------------
181 \helponly{\insertatlevel{2}{
187 \membersection{wxSocketBase::wxSocketBase
}\label{wxsocketbaseconstruct
}
189 \func{}{wxSocketBase
}{\void}
191 Default constructor. Don't use it directly; instead, use
192 \helpref{wxSocketClient
}{wxsocketclient
} to construct a socket client, or
193 \helpref{wxSocketServer
}{wxsocketserver
} to construct a socket server.
195 \membersection{wxSocketBase::
\destruct{wxSocketBase
}}\label{wxsocketbasedestruct
}
197 \func{}{\destruct{wxSocketBase
}}{\void}
199 Destructor. Do not destroy a socket using the delete operator directly;
200 use
\helpref{Destroy
}{wxsocketbasedestroy
} instead. Also, do not create
201 socket objects in the stack.
207 \membersection{wxSocketBase::Close
}\label{wxsocketbaseclose
}
209 \func{void
}{Close
}{\void}
211 This function shuts down the socket, disabling further transmission and
212 reception of data; it also disables events for the socket and frees the
213 associated system resources. Upon socket destruction, Close is automatically
214 called, so in most cases you won't need to do it yourself, unless you
215 explicitly want to shut down the socket, typically to notify the peer
216 that you are closing the connection.
218 \wxheading{Remark/Warning
}
220 Although Close immediately disables events for the socket, it is possible
221 that event messages may be waiting in the application's event queue. The
222 application must therefore be prepared to handle socket event messages
223 even after calling Close.
228 \membersection{wxSocketBase::Destroy
}\label{wxsocketbasedestroy
}
230 \func{bool
}{Destroy
}{\void}
232 Destroys the socket safely. Use this function instead of the delete operator,
233 since otherwise socket events could reach the application even after the
234 socket has been destroyed. To prevent this problem, this function appends
235 the wxSocket to a list of object to be deleted on idle time, after all
236 events have been processed. For the same reason, you should avoid creating
237 socket objects in the stack.
239 Destroy calls
\helpref{Close
}{wxsocketbaseclose
} automatically.
241 \wxheading{Return value
}
248 \membersection{wxSocketBase::Discard
}\label{wxsocketbasediscard
}
250 \func{wxSocketBase\&
}{Discard
}{\void}
252 This function simply deletes all bytes in the incoming queue. This function
253 always returns immediately and its operation is not affected by IO flags.
255 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually discarded.
257 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return false.
262 \membersection{wxSocketBase::Error
}\label{wxsocketbaseerror
}
264 \constfunc{bool
}{Error
}{\void}
266 Returns true if an error occurred in the last IO operation.
268 Use this function to check for an error condition after one of the
269 following calls: Discard, Peek, Read, ReadMsg, Unread, Write, WriteMsg.
274 \membersection{wxSocketBase::GetClientData
}\label{wxsocketbasegetclientdata
}
276 \constfunc{void *
}{GetClientData
}{\void}
278 Returns a pointer of the client data for this socket, as set with
279 \helpref{SetClientData
}{wxsocketbasesetclientdata
}
284 \membersection{wxSocketBase::GetLocal
}\label{wxsocketbasegetlocal
}
286 \constfunc{bool
}{GetLocal
}{\param{wxSockAddress\&
}{addr
}}
288 This function returns the local address field of the socket. The local
289 address field contains the complete local address of the socket (local
290 address, local port, ...).
292 \wxheading{Return value
}
294 true if no error happened, false otherwise.
299 \membersection{wxSocketBase::GetFlags
}\label{wxsocketbasegetflags
}
301 \constfunc{wxSocketFlags
}{GetFlags
}{\void}
303 Returns current IO flags, as set with
\helpref{SetFlags
}{wxsocketbasesetflags
}
308 \membersection{wxSocketBase::GetPeer
}\label{wxsocketbasegetpeer
}
310 \constfunc{bool
}{GetPeer
}{\param{wxSockAddress\&
}{addr
}}
312 This function returns the peer address field of the socket. The peer
313 address field contains the complete peer host address of the socket
314 (address, port, ...).
316 \wxheading{Return value
}
318 true if no error happened, false otherwise.
323 \membersection{wxSocketBase::InterruptWait
}\label{wxsocketbaseinterruptwait
}
325 \func{void
}{InterruptWait
}{\void}
327 Use this function to interrupt any wait operation currently in progress.
328 Note that this is not intended as a regular way to interrupt a Wait call,
329 but only as an escape mechanism for exceptional situations where it is
330 absolutely necessary to use it, for example to abort an operation due to
331 some exception or abnormal problem. InterruptWait is automatically called
332 when you
\helpref{Close
}{wxsocketbaseclose
} a socket (and thus also upon
333 socket destruction), so you don't need to use it in these cases.
335 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
},
336 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
337 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
},
338 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
339 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
340 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
}
345 \membersection{wxSocketBase::IsConnected
}\label{wxsocketbaseisconnected
}
347 \constfunc{bool
}{IsConnected
}{\void}
349 Returns true if the socket is connected.
354 \membersection{wxSocketBase::IsData
}\label{wxsocketbaseisdata
}
356 \constfunc{bool
}{IsData
}{\void}
358 This function waits until the socket is readable. This might mean that
359 queued data is available for reading or, for streamed sockets, that
360 the connection has been closed, so that a read operation will complete
361 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
362 is set, in which case the operation might still block).
364 \membersection{wxSocketBase::IsDisconnected
}\label{wxsocketbaseisdisconnected
}
369 \constfunc{bool
}{IsDisconnected
}{\void}
371 Returns true if the socket is not connected.
373 \membersection{wxSocketBase::LastCount
}\label{wxsocketbaselastcount
}
378 \constfunc{wxUint32
}{LastCount
}{\void}
380 Returns the number of bytes read or written by the last IO call.
382 Use this function to get the number of bytes actually transferred
383 after using one of the following IO calls: Discard, Peek, Read,
384 ReadMsg, Unread, Write, WriteMsg.
389 \membersection{wxSocketBase::LastError
}\label{wxsocketbaselasterror
}
391 \constfunc{wxSocketError
}{LastError
}{\void}
393 Returns the last wxSocket error. See
\helpref{wxSocket errors
}{wxsocketbase
}.
395 Please note that this function merely returns the last error code,
396 but it should not be used to determine if an error has occurred (this
397 is because successful operations do not change the LastError value).
398 Use
\helpref{Error
}{wxsocketbaseerror
} first, in order to determine
399 if the last IO call failed. If this returns true, use LastError
400 to discover the cause of the error.
405 \membersection{wxSocketBase::Notify
}\label{wxsocketbasenotify
}
407 \func{void
}{Notify
}{\param{bool
}{ notify
}}
409 According to the
{\it notify
} value, this function enables
410 or disables socket events. If
{\it notify
} is true, the events
411 configured with
\helpref{SetNotify
}{wxsocketbasesetnotify
} will
412 be sent to the application. If
{\it notify
} is false; no events
418 \membersection{wxSocketBase::Ok
}\label{wxsocketbaseok
}
420 \constfunc{bool
}{Ok
}{\void}
422 Returns true if the socket is initialized and ready and false in other
425 \wxheading{Remark/Warning
}
427 For
\helpref{wxSocketClient
}{wxsocketclient
}, Ok won't return true unless
428 the client is connected to a server.
430 For
\helpref{wxSocketServer
}{wxsocketserver
}, Ok will return true if the
431 server could bind to the specified address and is already listening for
434 Ok does not check for IO errors;
435 use
\helpref{Error
}{wxsocketbaseerror
} instead for that purpose.
440 \membersection{wxSocketBase::RestoreState
}\label{wxsocketbaserestorestate
}
442 \func{void
}{RestoreState
}{\void}
444 This function restores the previous state of the socket, as saved
445 with
\helpref{SaveState
}{wxsocketbasesavestate
}
447 Calls to SaveState and RestoreState can be nested.
451 \helpref{wxSocketBase::SaveState
}{wxsocketbasesavestate
}
456 \membersection{wxSocketBase::SaveState
}\label{wxsocketbasesavestate
}
458 \func{void
}{SaveState
}{\void}
460 This function saves the current state of the socket in a stack. Socket
461 state includes flags, as set with
\helpref{SetFlags
}{wxsocketbasesetflags
},
462 event mask, as set with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
463 \helpref{Notify
}{wxsocketbasenotify
}, user data, as set with
464 \helpref{SetClientData
}{wxsocketbasesetclientdata
}.
466 Calls to SaveState and RestoreState can be nested.
470 \helpref{wxSocketBase::RestoreState
}{wxsocketbaserestorestate
}
475 \membersection{wxSocketBase::SetClientData
}\label{wxsocketbasesetclientdata
}
477 \func{void
}{SetClientData
}{\param{void *
}{data
}}
479 Sets user-supplied client data for this socket. All socket events will
480 contain a pointer to this data, which can be retrieved with
481 the
\helpref{wxSocketEvent::GetClientData
}{wxsocketeventgetclientdata
} function.
486 \membersection{wxSocketBase::SetEventHandler
}\label{wxsocketbaseseteventhandler
}
488 \func{void
}{SetEventHandler
}{\param{wxEvtHandler\&
}{ handler
},
\param{int
}{ id = -
1}}
490 Sets an event handler to be called when a socket event occurs. The
491 handler will be called for those events for which notification is
492 enabled with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
493 \helpref{Notify
}{wxsocketbasenotify
}.
495 \wxheading{Parameters
}
497 \docparam{handler
}{Specifies the event handler you want to use.
}
499 \docparam{id
}{The id of socket event.
}
503 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
504 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
505 \helpref{wxSocketEvent
}{wxsocketevent
},
506 \helpref{wxEvtHandler
}{wxevthandler
}
511 \membersection{wxSocketBase::SetFlags
}\label{wxsocketbasesetflags
}
513 \func{void
}{SetFlags
}{\param{wxSocketFlags
}{ flags
}}
515 Use SetFlags to customize IO operation for this socket.
516 The
{\it flags
} parameter may be a combination of flags ORed together.
517 The following flags can be used:
520 \begin{twocollist
}\itemsep=
0pt
521 \twocolitem{{\bf wxSOCKET
\_NONE}}{Normal functionality.
}
522 \twocolitem{{\bf wxSOCKET
\_NOWAIT}}{Read/write as much data as possible and return immediately.
}
523 \twocolitem{{\bf wxSOCKET
\_WAITALL}}{Wait for all required data to be read/written unless an error occurs.
}
524 \twocolitem{{\bf wxSOCKET
\_BLOCK}}{Block the GUI (do not yield) while reading/writing data.
}
525 \twocolitem{{\bf wxSOCKET
\_REUSEADDR}}{Allows the use of an in-use port (wxServerSocket only)
}
528 A brief overview on how to use these flags follows.
530 If no flag is specified (this is the same as
{\bf wxSOCKET
\_NONE}),
531 IO calls will return after some data has been read or written, even
532 when the transfer might not be complete. This is the same as issuing
533 exactly one blocking low-level call to recv() or send(). Note
534 that
{\it blocking
} here refers to when the function returns, not
535 to whether the GUI blocks during this time.
537 If
{\bf wxSOCKET
\_NOWAIT} is specified, IO calls will return immediately.
538 Read operations will retrieve only available data. Write operations will
539 write as much data as possible, depending on how much space is available
540 in the output buffer. This is the same as issuing exactly one nonblocking
541 low-level call to recv() or send(). Note that
{\it nonblocking
} here
542 refers to when the function returns, not to whether the GUI blocks during
545 If
{\bf wxSOCKET
\_WAITALL} is specified, IO calls won't return until ALL
546 the data has been read or written (or until an error occurs), blocking if
547 necessary, and issuing several low level calls if necessary. This is the
548 same as having a loop which makes as many blocking low-level calls to
549 recv() or send() as needed so as to transfer all the data. Note
550 that
{\it blocking
} here refers to when the function returns, not
551 to whether the GUI blocks during this time.
553 The
{\bf wxSOCKET
\_BLOCK} flag controls whether the GUI blocks during
554 IO operations. If this flag is specified, the socket will not yield
555 during IO calls, so the GUI will remain blocked until the operation
556 completes. If it is not used, then the application must take extra
557 care to avoid unwanted reentrance.
559 The
{\bf wxSOCKET
\_REUSEADDR} flag controls the use of the SO
\_REUSEADDR standard
560 setsockopt() flag. This flag allows the socket to bind to a port that is already in use.
561 This is mostly used on UNIX-based systems to allow rapid starting and stopping of a server -
562 otherwise you may have to wait several minutes for the port to become available.
563 This option can have suprising platform dependent behavior, check the documentation for
564 your platforms implementation of setsockopt().
568 {\bf wxSOCKET
\_NONE} will try to read at least SOME data, no matter how much.
570 {\bf wxSOCKET
\_NOWAIT} will always return immediately, even if it cannot
571 read or write ANY data.
573 {\bf wxSOCKET
\_WAITALL} will only return when it has read or written ALL
576 {\bf wxSOCKET
\_BLOCK} has nothing to do with the previous flags and
577 it controls whether the GUI blocks.
579 {\bf wxSOCKET
\_REUSEADDR} controls special platform-specific behavior for wxServerSocket.
584 \membersection{wxSocketBase::SetNotify
}\label{wxsocketbasesetnotify
}
586 \func{void
}{SetNotify
}{\param{wxSocketEventFlags
}{ flags
}}
588 SetNotify specifies which socket events are to be sent to the event handler.
589 The
{\it flags
} parameter may be combination of flags ORed together. The
590 following flags can be used:
593 \begin{twocollist
}\itemsep=
0pt
594 \twocolitem{{\bf wxSOCKET
\_INPUT\_FLAG}}{to receive wxSOCKET
\_INPUT}
595 \twocolitem{{\bf wxSOCKET
\_OUTPUT\_FLAG}}{to receive wxSOCKET
\_OUTPUT}
596 \twocolitem{{\bf wxSOCKET
\_CONNECTION\_FLAG}}{to receive wxSOCKET
\_CONNECTION}
597 \twocolitem{{\bf wxSOCKET
\_LOST\_FLAG}}{to receive wxSOCKET
\_LOST}
603 sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
607 In this example, the user will be notified about incoming socket data and
608 whenever the connection is closed.
610 For more information on socket events see
\helpref{wxSocket events
}{wxsocketbase
}.
615 \membersection{wxSocketBase::SetTimeout
}\label{wxsocketbasesettimeout
}
617 \func{void
}{SetTimeout
}{\param{int
}{seconds
}}
619 This function sets the default socket timeout in seconds. This timeout
620 applies to all IO calls, and also to the
\helpref{Wait
}{wxsocketbasewait
} family
621 of functions if you don't specify a wait interval. Initially, the default
622 timeout is
10 minutes.
627 \membersection{wxSocketBase::Peek
}\label{wxsocketbasepeek
}
629 \func{wxSocketBase\&
}{Peek
}{\param{void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
631 This function peeks a buffer of
{\it nbytes
} bytes from the socket.
632 Peeking a buffer doesn't delete it from the socket input queue.
634 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually peeked.
636 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
638 \wxheading{Parameters
}
640 \docparam{buffer
}{Buffer where to put peeked data.
}
642 \docparam{nbytes
}{Number of bytes.
}
644 \wxheading{Return value
}
646 Returns a reference to the current object.
648 \wxheading{Remark/Warning
}
650 The exact behaviour of wxSocketBase::Peek depends on the combination
651 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
655 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
656 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
657 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
658 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
663 \membersection{wxSocketBase::Read
}\label{wxsocketbaseread
}
665 \func{wxSocketBase\&
}{Read
}{\param{void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
667 This function reads a buffer of
{\it nbytes
} bytes from the socket.
669 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
671 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
673 \wxheading{Parameters
}
675 \docparam{buffer
}{Buffer where to put read data.
}
677 \docparam{nbytes
}{Number of bytes.
}
679 \wxheading{Return value
}
681 Returns a reference to the current object.
683 \wxheading{Remark/Warning
}
685 The exact behaviour of wxSocketBase::Read depends on the combination
686 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
690 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
691 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
692 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
693 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
698 \membersection{wxSocketBase::ReadMsg
}\label{wxsocketbasereadmsg
}
700 \func{wxSocketBase\&
}{ReadMsg
}{\param{void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
702 This function reads a buffer sent by
\helpref{WriteMsg
}{wxsocketbasewritemsg
}
703 on a socket. If the buffer passed to the function isn't big enough, the
704 remaining bytes will be discarded. This function always waits for the
705 buffer to be entirely filled, unless an error occurs.
707 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
709 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
711 \wxheading{Parameters
}
713 \docparam{buffer
}{Buffer where to put read data.
}
715 \docparam{nbytes
}{Size of the buffer.
}
717 \wxheading{Return value
}
719 Returns a reference to the current object.
721 \wxheading{Remark/Warning
}
723 wxSocketBase::ReadMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
724 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
725 The exact behaviour of ReadMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
726 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
730 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
731 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
732 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
733 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
734 \helpref{wxSocketBase::WriteMsg
}{wxsocketbasewritemsg
}
739 \membersection{wxSocketBase::Unread
}\label{wxsocketbaseunread
}
741 \func{wxSocketBase\&
}{Unread
}{\param{const void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
743 This function unreads a buffer. That is, the data in the buffer is put back
744 in the incoming queue. This function is not affected by wxSocket flags.
746 If you use
\helpref{LastCount
}{wxsocketbaselastcount
}, it will always return
{\it nbytes
}.
748 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return false.
750 \wxheading{Parameters
}
752 \docparam{buffer
}{Buffer to be unread.
}
754 \docparam{nbytes
}{Number of bytes.
}
756 \wxheading{Return value
}
758 Returns a reference to the current object.
762 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
763 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
764 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
}
769 \membersection{wxSocketBase::Wait
}\label{wxsocketbasewait
}
771 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
773 This function waits until any of the following conditions is true:
776 \item The socket becomes readable.
777 \item The socket becomes writable.
778 \item An ongoing connection request has completed (
\helpref{wxSocketClient
}{wxsocketclient
} only)
779 \item An incoming connection request has arrived (
\helpref{wxSocketServer
}{wxsocketserver
} only)
780 \item The connection has been closed.
783 Note that it is recommended to use the individual Wait functions
784 to wait for the required condition, instead of this one.
786 \wxheading{Parameters
}
788 \docparam{seconds
}{Number of seconds to wait.
789 If -
1, it will wait for the default timeout,
790 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
792 \docparam{millisecond
}{Number of milliseconds to wait.
}
794 \wxheading{Return value
}
796 Returns true when any of the above conditions is satisfied,
797 false if the timeout was reached.
801 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
802 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
803 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
},
804 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
805 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
806 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
}
811 \membersection{wxSocketBase::WaitForLost
}\label{wxsocketbasewaitforlost
}
813 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
815 This function waits until the connection is lost. This may happen if
816 the peer gracefully closes the connection or if the connection breaks.
818 \wxheading{Parameters
}
820 \docparam{seconds
}{Number of seconds to wait.
821 If -
1, it will wait for the default timeout,
822 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
824 \docparam{millisecond
}{Number of milliseconds to wait.
}
826 \wxheading{Return value
}
828 Returns true if the connection was lost, false if the timeout was reached.
832 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
833 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
}
838 \membersection{wxSocketBase::WaitForRead
}\label{wxsocketbasewaitforread
}
840 \func{bool
}{WaitForRead
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
842 This function waits until the socket is readable. This might mean that
843 queued data is available for reading or, for streamed sockets, that
844 the connection has been closed, so that a read operation will complete
845 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
846 is set, in which case the operation might still block).
848 \wxheading{Parameters
}
850 \docparam{seconds
}{Number of seconds to wait.
851 If -
1, it will wait for the default timeout,
852 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
854 \docparam{millisecond
}{Number of milliseconds to wait.
}
856 \wxheading{Return value
}
858 Returns true if the socket becomes readable, false on timeout.
862 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
863 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
}
868 \membersection{wxSocketBase::WaitForWrite
}\label{wxsocketbasewaitforwrite
}
870 \func{bool
}{WaitForWrite
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
872 This function waits until the socket becomes writable. This might mean that
873 the socket is ready to send new data, or for streamed sockets, that the
874 connection has been closed, so that a write operation is guaranteed to
875 complete immediately (unless the
{\bf wxSOCKET
\_WAITALL} flag is set,
876 in which case the operation might still block).
878 \wxheading{Parameters
}
880 \docparam{seconds
}{Number of seconds to wait.
881 If -
1, it will wait for the default timeout,
882 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
884 \docparam{millisecond
}{Number of milliseconds to wait.
}
886 \wxheading{Return value
}
888 Returns true if the socket becomes writable, false on timeout.
892 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
893 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
}
898 \membersection{wxSocketBase::Write
}\label{wxsocketbasewrite
}
900 \func{wxSocketBase\&
}{Write
}{\param{const void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
902 This function writes a buffer of
{\it nbytes
} bytes to the socket.
904 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
906 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
908 \wxheading{Parameters
}
910 \docparam{buffer
}{Buffer with the data to be sent.
}
912 \docparam{nbytes
}{Number of bytes.
}
914 \wxheading{Return value
}
916 Returns a reference to the current object.
918 \wxheading{Remark/Warning
}
920 The exact behaviour of wxSocketBase::Write depends on the combination
921 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
925 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
926 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
927 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
928 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
933 \membersection{wxSocketBase::WriteMsg
}\label{wxsocketbasewritemsg
}
935 \func{wxSocketBase\&
}{WriteMsg
}{\param{const void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
937 This function writes a buffer of
{\it nbytes
} bytes from the socket, but it
938 writes a short header before so that
\helpref{ReadMsg
}{wxsocketbasereadmsg
}
939 knows how much data should it actually read. So, a buffer sent with WriteMsg
940 {\bf must
} be read with ReadMsg. This function always waits for the entire
941 buffer to be sent, unless an error occurs.
943 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
945 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
947 \wxheading{Parameters
}
949 \docparam{buffer
}{Buffer with the data to be sent.
}
951 \docparam{nbytes
}{Number of bytes to send.
}
953 \wxheading{Return value
}
955 Returns a reference to the current object.
957 \wxheading{Remark/Warning
}
959 wxSocketBase::WriteMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
960 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
961 The exact behaviour of WriteMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
962 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
966 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
967 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
968 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
969 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
970 \helpref{wxSocketBase::ReadMsg
}{wxsocketbasereadmsg
}
973 % ---------------------------------------------------------------------------
974 % CLASS wxSocketClient
975 % ---------------------------------------------------------------------------
977 \section{\class{wxSocketClient
}}\label{wxsocketclient
}
979 \wxheading{Derived from
}
981 \helpref{wxSocketBase
}{wxsocketbase
}
983 \wxheading{Include files
}
987 \latexignore{\rtfignore{\wxheading{Members
}}}
989 % ---------------------------------------------------------------------------
991 % ---------------------------------------------------------------------------
995 \membersection{wxSocketClient::wxSocketClient
}\label{wxsocketclientctor
}
997 \func{}{wxSocketClient
}{\param{wxSocketFlags
}{ flags = wxSOCKET
\_NONE}}
1001 \wxheading{Parameters
}
1003 \docparam{flags
}{Socket flags (See
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
})
}
1008 \membersection{wxSocketClient::
\destruct{wxSocketClient
}}\label{wxsocketclientdtor
}
1010 \func{}{\destruct{wxSocketClient
}}{\void}
1012 Destructor. Please see
\helpref{wxSocketBase::Destroy
}{wxsocketbasedestroy
}.
1017 \membersection{wxSocketClient::Connect
}\label{wxsocketclientconnect
}
1019 \func{bool
}{Connect
}{\param{wxSockAddress\&
}{ address
},
\param{bool
}{ wait = true
}}
1021 Connects to a server using the specified address.
1023 If
{\it wait
} is true, Connect will wait until the connection
1024 completes.
{\bf Warning:
} This will block the GUI.
1026 If
{\it wait
} is false, Connect will try to establish the connection and
1027 return immediately, without blocking the GUI. When used this way, even if
1028 Connect returns false, the connection request can be completed later.
1029 To detect this, use
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
},
1030 or catch
{\bf wxSOCKET
\_CONNECTION} events (for successful establishment)
1031 and
{\bf wxSOCKET
\_LOST} events (for connection failure).
1033 \wxheading{Parameters
}
1035 \docparam{address
}{Address of the server.
}
1037 \docparam{wait
}{If true, waits for the connection to complete.
}
1039 \wxheading{Return value
}
1041 Returns true if the connection is established and no error occurs.
1043 If
{\it wait
} was true, and Connect returns false, an error occurred
1044 and the connection failed.
1046 If
{\it wait
} was false, and Connect returns false, you should still
1047 be prepared to handle the completion of this connection request, either
1048 with
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
} or by
1049 watching
{\bf wxSOCKET
\_CONNECTION} and
{\bf wxSOCKET
\_LOST} events.
1051 \wxheading{See also
}
1053 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
},
1054 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1055 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
}
1060 \membersection{wxSocketClient::WaitOnConnect
}\label{wxsocketclientwaitonconnect
}
1062 \func{bool
}{WaitOnConnect
}{\param{long
}{ seconds = -
1},
\param{long
}{ milliseconds =
0}}
1064 Wait until a connection request completes, or until the specified timeout
1065 elapses. Use this function after issuing a call
1066 to
\helpref{Connect
}{wxsocketclientconnect
} with
{\it wait
} set to false.
1068 \wxheading{Parameters
}
1070 \docparam{seconds
}{Number of seconds to wait.
1071 If -
1, it will wait for the default timeout,
1072 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
1074 \docparam{millisecond
}{Number of milliseconds to wait.
}
1076 \wxheading{Return value
}
1078 WaitOnConnect returns true if the connection request completes. This
1079 does not necessarily mean that the connection was successfully established;
1080 it might also happen that the connection was refused by the peer. Use
1081 \helpref{IsConnected
}{wxsocketbaseisconnected
} to distinguish between
1082 these two situations.
1084 If the timeout elapses, WaitOnConnect returns false.
1086 These semantics allow code like this:
1089 // Issue the connection request
1090 client->Connect(addr, false);
1092 // Wait until the request completes or until we decide to give up
1093 bool waitmore = true;
1094 while ( !client->WaitOnConnect(seconds, millis) && waitmore )
1096 // possibly give some feedback to the user,
1097 // and update waitmore as needed.
1099 bool success = client->IsConnected();
1102 \wxheading{See also
}
1104 \helpref{wxSocketClient::Connect
}{wxsocketclientconnect
},
1105 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
1106 \helpref{wxSocketBase::IsConnected
}{wxsocketbaseisconnected
}
1108 % ---------------------------------------------------------------------------
1109 % CLASS: wxSocketEvent
1110 % ---------------------------------------------------------------------------
1111 \section{\class{wxSocketEvent
}}\label{wxsocketevent
}
1113 This event class contains information about socket events.
1115 \wxheading{Derived from
}
1117 \helpref{wxEvent
}{wxevent
}
1119 \wxheading{Include files
}
1123 \wxheading{Event table macros
}
1125 To process a socket event, use these event handler macros to direct input
1126 to member functions that take a wxSocketEvent argument.
1129 \begin{twocollist
}\itemsep=
0pt
1130 \twocolitem{{\bf EVT
\_SOCKET(id, func)
}}{Process a socket event, supplying the member function.
}
1133 \wxheading{See also
}
1135 \helpref{wxSocketBase
}{wxsocketbase
},
1136 \helpref{wxSocketClient
}{wxsocketclient
},
1137 \helpref{wxSocketServer
}{wxsocketserver
}
1139 \latexignore{\rtfignore{\wxheading{Members
}}}
1141 \membersection{wxSocketEvent::wxSocketEvent
}\label{wxsocketeventctor
}
1143 \func{}{wxSocketEvent
}{\param{int
}{ id =
0}}
1147 \membersection{wxSocketEvent::GetClientData
}\label{wxsocketeventgetclientdata
}
1149 \func{void *
}{GetClientData
}{\void}
1151 Gets the client data of the socket which generated this event, as
1152 set with
\helpref{wxSocketBase::SetClientData
}{wxsocketbasesetclientdata
}.
1154 \membersection{wxSocketEvent::GetSocket
}\label{wxsocketeventgetsocket
}
1156 \constfunc{wxSocketBase *
}{GetSocket
}{\void}
1158 Returns the socket object to which this event refers to. This makes
1159 it possible to use the same event handler for different sockets.
1161 \membersection{wxSocketEvent::GetSocketEvent
}\label{wxsocketeventgetsocketevent
}
1163 \constfunc{wxSocketNotify
}{GetSocketEvent
}{\void}
1165 Returns the socket event type.