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
}
34 \helpref{wxNet
}{librarieslist
}
36 \wxheading{wxSocket errors
}
39 \begin{twocollist
}\itemsep=
0pt
40 \twocolitem{{\bf wxSOCKET
\_NOERROR}}{No error happened.
}
41 \twocolitem{{\bf wxSOCKET
\_INVOP}}{Invalid operation.
}
42 \twocolitem{{\bf wxSOCKET
\_IOERR}}{Input/Output error.
}
43 \twocolitem{{\bf wxSOCKET
\_INVADDR}}{Invalid address passed to wxSocket.
}
44 \twocolitem{{\bf wxSOCKET
\_INVSOCK}}{Invalid socket (uninitialized).
}
45 \twocolitem{{\bf wxSOCKET
\_NOHOST}}{No corresponding host.
}
46 \twocolitem{{\bf wxSOCKET
\_INVPORT}}{Invalid port.
}
47 \twocolitem{{\bf wxSOCKET
\_WOULDBLOCK}}{The socket is non-blocking and the operation would block.
}
48 \twocolitem{{\bf wxSOCKET
\_TIMEDOUT}}{The timeout for this operation expired.
}
49 \twocolitem{{\bf wxSOCKET
\_MEMERR}}{Memory exhausted.
}
52 \wxheading{wxSocket events
}
55 \begin{twocollist
}\itemsep=
0pt
56 \twocolitem{{\bf wxSOCKET
\_INPUT}}{There is data available for reading.
}
57 \twocolitem{{\bf wxSOCKET
\_OUTPUT}}{The socket is ready to be written to.
}
58 \twocolitem{{\bf wxSOCKET
\_CONNECTION}}{Incoming connection request (server), or successful connection establishment (client).
}
59 \twocolitem{{\bf wxSOCKET
\_LOST}}{The connection has been closed.
}
62 A brief note on how to use these events:
64 The
{\bf wxSOCKET
\_INPUT} event will be issued whenever there is data
65 available for reading. This will be the case if the input queue was
66 empty and new data arrives, or if the application has read some data
67 yet there is still more data available. This means that the application
68 does not need to read all available data in response to a
69 {\bf wxSOCKET
\_INPUT} event, as more events will be produced as
72 The
{\bf wxSOCKET
\_OUTPUT} event is issued when a socket is first
73 connected with
\helpref{Connect
}{wxsocketclientconnect
} or accepted
74 with
\helpref{Accept
}{wxsocketserveraccept
}. After that, new
75 events will be generated only after an output operation fails
76 with
{\bf wxSOCKET
\_WOULDBLOCK} and buffer space becomes available
77 again. This means that the application should assume that it
78 can write data to the socket until an
{\bf wxSOCKET
\_WOULDBLOCK}
79 error occurs; after this, whenever the socket becomes writable
80 again the application will be notified with another
81 {\bf wxSOCKET
\_OUTPUT} event.
83 The
{\bf wxSOCKET
\_CONNECTION} event is issued when a delayed connection
84 request completes successfully (client) or when a new connection arrives
85 at the incoming queue (server).
87 The
{\bf wxSOCKET
\_LOST} event is issued when a close indication is
88 received for the socket. This means that the connection broke down or
89 that it was closed by the peer. Also, this event will be issued if
90 a connection request fails.
92 \wxheading{Event handling
}
94 To process events coming from a socket object, use the following event
95 handler macro to direct events to member functions that take
96 a
\helpref{wxSocketEvent
}{wxsocketevent
} argument.
99 \begin{twocollist
}\itemsep=
0pt
100 \twocolitem{{\bf EVT
\_SOCKET(id, func)
}}{Process a wxEVT
\_SOCKET event.
}
105 \helpref{wxSocketEvent
}{wxsocketevent
},
106 \helpref{wxSocketClient
}{wxsocketclient
},
107 \helpref{wxSocketServer
}{wxsocketserver
},
108 \helpref{Sockets sample
}{samplesockets
}
110 % ---------------------------------------------------------------------------
112 % ---------------------------------------------------------------------------
114 \latexignore{\rtfignore{\wxheading{Function groups
}}}
116 \membersection{Construction and destruction
}\label{socketconstruction
}
118 \helpref{wxSocketBase
}{wxsocketbaseconstruct
}\\
119 \helpref{\destruct{wxSocketBase
}}{wxsocketbasedestruct
}\\
120 \helpref{Destroy
}{wxsocketbasedestroy
}
122 \membersection{Socket state
}\label{socketstate
}
124 Functions to retrieve current state and miscellaneous info.
126 \helpref{Error
}{wxsocketbaseerror
}\\
127 \helpref{GetLocal
}{wxsocketbasegetlocal
}\\
128 \helpref{GetPeer
}{wxsocketbasegetpeer
}
129 \helpref{IsConnected
}{wxsocketbaseisconnected
}\\
130 \helpref{IsData
}{wxsocketbaseisdata
}\\
131 \helpref{IsDisconnected
}{wxsocketbaseisdisconnected
}\\
132 \helpref{LastCount
}{wxsocketbaselastcount
}\\
133 \helpref{LastError
}{wxsocketbaselasterror
}\\
134 \helpref{IsOk
}{wxsocketbaseisok
}\\
135 \helpref{SaveState
}{wxsocketbasesavestate
}\\
136 \helpref{RestoreState
}{wxsocketbaserestorestate
}
138 \membersection{Basic IO
}\label{socketbasicio
}
140 Functions that perform basic IO functionality.
142 \helpref{Close
}{wxsocketbaseclose
}\\
143 \helpref{Discard
}{wxsocketbasediscard
}\\
144 \helpref{Peek
}{wxsocketbasepeek
}\\
145 \helpref{Read
}{wxsocketbaseread
}\\
146 \helpref{ReadMsg
}{wxsocketbasereadmsg
}\\
147 \helpref{Unread
}{wxsocketbaseunread
}\\
148 \helpref{Write
}{wxsocketbasewrite
}\\
149 \helpref{WriteMsg
}{wxsocketbasewritemsg
}
151 Functions that perform a timed wait on a certain IO condition.
153 \helpref{InterruptWait
}{wxsocketbaseinterruptwait
}\\
154 \helpref{Wait
}{wxsocketbasewait
}\\
155 \helpref{WaitForLost
}{wxsocketbasewaitforlost
}\\
156 \helpref{WaitForRead
}{wxsocketbasewaitforread
}\\
157 \helpref{WaitForWrite
}{wxsocketbasewaitforwrite
}\\
161 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
}\\
162 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
}
164 Functions that allow applications to customize socket IO as needed.
166 \helpref{GetFlags
}{wxsocketbasegetflags
}\\
167 \helpref{SetFlags
}{wxsocketbasesetflags
}\\
168 \helpref{SetTimeout
}{wxsocketbasesettimeout
}\\
169 \helpref{SetLocal
}{wxsocketbasesetlocal
}\\
171 \membersection{Handling socket events
}\label{socketevents
}
173 Functions that allow applications to receive socket events.
175 \helpref{Notify
}{wxsocketbasenotify
}\\
176 \helpref{SetNotify
}{wxsocketbasesetnotify
}\\
177 \helpref{GetClientData
}{wxsocketbasegetclientdata
}\\
178 \helpref{SetClientData
}{wxsocketbasesetclientdata
}\\
179 \helpref{SetEventHandler
}{wxsocketbaseseteventhandler
}
182 % ---------------------------------------------------------------------------
184 % ---------------------------------------------------------------------------
186 \helponly{\insertatlevel{2}{
192 \membersection{wxSocketBase::wxSocketBase
}\label{wxsocketbaseconstruct
}
194 \func{}{wxSocketBase
}{\void}
196 Default constructor. Don't use it directly; instead, use
197 \helpref{wxSocketClient
}{wxsocketclient
} to construct a socket client, or
198 \helpref{wxSocketServer
}{wxsocketserver
} to construct a socket server.
200 \membersection{wxSocketBase::
\destruct{wxSocketBase
}}\label{wxsocketbasedestruct
}
202 \func{}{\destruct{wxSocketBase
}}{\void}
204 Destructor. Do not destroy a socket using the delete operator directly;
205 use
\helpref{Destroy
}{wxsocketbasedestroy
} instead. Also, do not create
206 socket objects in the stack.
212 \membersection{wxSocketBase::Close
}\label{wxsocketbaseclose
}
214 \func{void
}{Close
}{\void}
216 This function shuts down the socket, disabling further transmission and
217 reception of data; it also disables events for the socket and frees the
218 associated system resources. Upon socket destruction, Close is automatically
219 called, so in most cases you won't need to do it yourself, unless you
220 explicitly want to shut down the socket, typically to notify the peer
221 that you are closing the connection.
223 \wxheading{Remark/Warning
}
225 Although Close immediately disables events for the socket, it is possible
226 that event messages may be waiting in the application's event queue. The
227 application must therefore be prepared to handle socket event messages
228 even after calling Close.
233 \membersection{wxSocketBase::Destroy
}\label{wxsocketbasedestroy
}
235 \func{bool
}{Destroy
}{\void}
237 Destroys the socket safely. Use this function instead of the delete operator,
238 since otherwise socket events could reach the application even after the
239 socket has been destroyed. To prevent this problem, this function appends
240 the wxSocket to a list of object to be deleted on idle time, after all
241 events have been processed. For the same reason, you should avoid creating
242 socket objects in the stack.
244 Destroy calls
\helpref{Close
}{wxsocketbaseclose
} automatically.
246 \wxheading{Return value
}
253 \membersection{wxSocketBase::Discard
}\label{wxsocketbasediscard
}
255 \func{wxSocketBase\&
}{Discard
}{\void}
257 This function simply deletes all bytes in the incoming queue. This function
258 always returns immediately and its operation is not affected by IO flags.
260 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually discarded.
262 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return false.
267 \membersection{wxSocketBase::Error
}\label{wxsocketbaseerror
}
269 \constfunc{bool
}{Error
}{\void}
271 Returns true if an error occurred in the last IO operation.
273 Use this function to check for an error condition after one of the
274 following calls: Discard, Peek, Read, ReadMsg, Unread, Write, WriteMsg.
279 \membersection{wxSocketBase::GetClientData
}\label{wxsocketbasegetclientdata
}
281 \constfunc{void *
}{GetClientData
}{\void}
283 Returns a pointer of the client data for this socket, as set with
284 \helpref{SetClientData
}{wxsocketbasesetclientdata
}
289 \membersection{wxSocketBase::GetLocal
}\label{wxsocketbasegetlocal
}
291 \constfunc{bool
}{GetLocal
}{\param{wxSockAddress\&
}{addr
}}
293 This function returns the local address field of the socket. The local
294 address field contains the complete local address of the socket (local
295 address, local port, ...).
297 \wxheading{Return value
}
299 true if no error happened, false otherwise.
304 \membersection{wxSocketBase::GetFlags
}\label{wxsocketbasegetflags
}
306 \constfunc{wxSocketFlags
}{GetFlags
}{\void}
308 Returns current IO flags, as set with
\helpref{SetFlags
}{wxsocketbasesetflags
}
313 \membersection{wxSocketBase::GetPeer
}\label{wxsocketbasegetpeer
}
315 \constfunc{bool
}{GetPeer
}{\param{wxSockAddress\&
}{addr
}}
317 This function returns the peer address field of the socket. The peer
318 address field contains the complete peer host address of the socket
319 (address, port, ...).
321 \wxheading{Return value
}
323 true if no error happened, false otherwise.
328 \membersection{wxSocketBase::InterruptWait
}\label{wxsocketbaseinterruptwait
}
330 \func{void
}{InterruptWait
}{\void}
332 Use this function to interrupt any wait operation currently in progress.
333 Note that this is not intended as a regular way to interrupt a Wait call,
334 but only as an escape mechanism for exceptional situations where it is
335 absolutely necessary to use it, for example to abort an operation due to
336 some exception or abnormal problem. InterruptWait is automatically called
337 when you
\helpref{Close
}{wxsocketbaseclose
} a socket (and thus also upon
338 socket destruction), so you don't need to use it in these cases.
340 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
},
341 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
342 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
},
343 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
344 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
345 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
}
350 \membersection{wxSocketBase::IsConnected
}\label{wxsocketbaseisconnected
}
352 \constfunc{bool
}{IsConnected
}{\void}
354 Returns true if the socket is connected.
359 \membersection{wxSocketBase::IsData
}\label{wxsocketbaseisdata
}
361 \constfunc{bool
}{IsData
}{\void}
363 This function waits until the socket is readable. This might mean that
364 queued data is available for reading or, for streamed sockets, that
365 the connection has been closed, so that a read operation will complete
366 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
367 is set, in which case the operation might still block).
369 \membersection{wxSocketBase::IsDisconnected
}\label{wxsocketbaseisdisconnected
}
374 \constfunc{bool
}{IsDisconnected
}{\void}
376 Returns true if the socket is not connected.
378 \membersection{wxSocketBase::LastCount
}\label{wxsocketbaselastcount
}
383 \constfunc{wxUint32
}{LastCount
}{\void}
385 Returns the number of bytes read or written by the last IO call.
387 Use this function to get the number of bytes actually transferred
388 after using one of the following IO calls: Discard, Peek, Read,
389 ReadMsg, Unread, Write, WriteMsg.
394 \membersection{wxSocketBase::LastError
}\label{wxsocketbaselasterror
}
396 \constfunc{wxSocketError
}{LastError
}{\void}
398 Returns the last wxSocket error. See
\helpref{wxSocket errors
}{wxsocketbase
}.
400 Please note that this function merely returns the last error code,
401 but it should not be used to determine if an error has occurred (this
402 is because successful operations do not change the LastError value).
403 Use
\helpref{Error
}{wxsocketbaseerror
} first, in order to determine
404 if the last IO call failed. If this returns true, use LastError
405 to discover the cause of the error.
410 \membersection{wxSocketBase::Notify
}\label{wxsocketbasenotify
}
412 \func{void
}{Notify
}{\param{bool
}{ notify
}}
414 According to the
{\it notify
} value, this function enables
415 or disables socket events. If
{\it notify
} is true, the events
416 configured with
\helpref{SetNotify
}{wxsocketbasesetnotify
} will
417 be sent to the application. If
{\it notify
} is false; no events
423 \membersection{wxSocketBase::IsOk
}\label{wxsocketbaseisok
}
425 \constfunc{bool
}{IsOk
}{\void}
427 Returns true if the socket is initialized and ready and false in other
430 \wxheading{Remark/Warning
}
432 For
\helpref{wxSocketClient
}{wxsocketclient
}, Ok won't return true unless
433 the client is connected to a server.
435 For
\helpref{wxSocketServer
}{wxsocketserver
}, Ok will return true if the
436 server could bind to the specified address and is already listening for
439 Ok does not check for IO errors;
440 use
\helpref{Error
}{wxsocketbaseerror
} instead for that purpose.
445 \membersection{wxSocketBase::RestoreState
}\label{wxsocketbaserestorestate
}
447 \func{void
}{RestoreState
}{\void}
449 This function restores the previous state of the socket, as saved
450 with
\helpref{SaveState
}{wxsocketbasesavestate
}
452 Calls to SaveState and RestoreState can be nested.
456 \helpref{wxSocketBase::SaveState
}{wxsocketbasesavestate
}
461 \membersection{wxSocketBase::SaveState
}\label{wxsocketbasesavestate
}
463 \func{void
}{SaveState
}{\void}
465 This function saves the current state of the socket in a stack. Socket
466 state includes flags, as set with
\helpref{SetFlags
}{wxsocketbasesetflags
},
467 event mask, as set with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
468 \helpref{Notify
}{wxsocketbasenotify
}, user data, as set with
469 \helpref{SetClientData
}{wxsocketbasesetclientdata
}.
471 Calls to SaveState and RestoreState can be nested.
475 \helpref{wxSocketBase::RestoreState
}{wxsocketbaserestorestate
}
480 \membersection{wxSocketBase::SetClientData
}\label{wxsocketbasesetclientdata
}
482 \func{void
}{SetClientData
}{\param{void *
}{data
}}
484 Sets user-supplied client data for this socket. All socket events will
485 contain a pointer to this data, which can be retrieved with
486 the
\helpref{wxSocketEvent::GetClientData
}{wxsocketeventgetclientdata
} function.
491 \membersection{wxSocketBase::SetEventHandler
}\label{wxsocketbaseseteventhandler
}
493 \func{void
}{SetEventHandler
}{\param{wxEvtHandler\&
}{ handler
},
\param{int
}{ id = -
1}}
495 Sets an event handler to be called when a socket event occurs. The
496 handler will be called for those events for which notification is
497 enabled with
\helpref{SetNotify
}{wxsocketbasesetnotify
} and
498 \helpref{Notify
}{wxsocketbasenotify
}.
500 \wxheading{Parameters
}
502 \docparam{handler
}{Specifies the event handler you want to use.
}
504 \docparam{id
}{The id of socket event.
}
508 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
509 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
},
510 \helpref{wxSocketEvent
}{wxsocketevent
},
511 \helpref{wxEvtHandler
}{wxevthandler
}
516 \membersection{wxSocketBase::SetFlags
}\label{wxsocketbasesetflags
}
518 \func{void
}{SetFlags
}{\param{wxSocketFlags
}{ flags
}}
520 Use SetFlags to customize IO operation for this socket.
521 The
{\it flags
} parameter may be a combination of flags ORed together.
522 The following flags can be used:
525 \begin{twocollist
}\itemsep=
0pt
526 \twocolitem{{\bf wxSOCKET
\_NONE}}{Normal functionality.
}
527 \twocolitem{{\bf wxSOCKET
\_NOWAIT}}{Read/write as much data as possible and return immediately.
}
528 \twocolitem{{\bf wxSOCKET
\_WAITALL}}{Wait for all required data to be read/written unless an error occurs.
}
529 \twocolitem{{\bf wxSOCKET
\_BLOCK}}{Block the GUI (do not yield) while reading/writing data.
}
530 \twocolitem{{\bf wxSOCKET
\_REUSEADDR}}{Allows the use of an in-use port (wxServerSocket only)
}
531 \twocolitem{{\bf wxSOCKET
\_BROADCAST}}{Switches the socket to broadcast mode
}
532 \twocolitem{{\bf wxSOCKET
\_NOBIND}}{Stops the socket from being bound to a specific adapter (normally used in conjunction with
{\bf wxSOCKET
\_BROADCAST})
}
535 A brief overview on how to use these flags follows.
537 If no flag is specified (this is the same as
{\bf wxSOCKET
\_NONE}),
538 IO calls will return after some data has been read or written, even
539 when the transfer might not be complete. This is the same as issuing
540 exactly one blocking low-level call to recv() or send(). Note
541 that
{\it blocking
} here refers to when the function returns, not
542 to whether the GUI blocks during this time.
544 If
{\bf wxSOCKET
\_NOWAIT} is specified, IO calls will return immediately.
545 Read operations will retrieve only available data. Write operations will
546 write as much data as possible, depending on how much space is available
547 in the output buffer. This is the same as issuing exactly one nonblocking
548 low-level call to recv() or send(). Note that
{\it nonblocking
} here
549 refers to when the function returns, not to whether the GUI blocks during
552 If
{\bf wxSOCKET
\_WAITALL} is specified, IO calls won't return until ALL
553 the data has been read or written (or until an error occurs), blocking if
554 necessary, and issuing several low level calls if necessary. This is the
555 same as having a loop which makes as many blocking low-level calls to
556 recv() or send() as needed so as to transfer all the data. Note
557 that
{\it blocking
} here refers to when the function returns, not
558 to whether the GUI blocks during this time.
560 The
{\bf wxSOCKET
\_BLOCK} flag controls whether the GUI blocks during
561 IO operations. If this flag is specified, the socket will not yield
562 during IO calls, so the GUI will remain blocked until the operation
563 completes. If it is not used, then the application must take extra
564 care to avoid unwanted reentrance.
566 The
{\bf wxSOCKET
\_REUSEADDR} flag controls the use of the SO
\_REUSEADDR standard
567 setsockopt() flag. This flag allows the socket to bind to a port that is already in use.
568 This is mostly used on UNIX-based systems to allow rapid starting and stopping of a server -
569 otherwise you may have to wait several minutes for the port to become available.
570 wxSOCKET
\_REUSEADDR can also be used with socket clients to (re)bind to a particular local port
571 for an outgoing connection.
572 This option can have surprising platform dependent behavior, so check the documentation for
573 your platform's implementation of setsockopt(). Note that on BSD-based systems (e.g. Mac OS X),
574 use of wxSOCKET
\_REUSEADDR implies SO
\_REUSEPORT in addition to SO
\_REUSEADDR to be consistent
577 The
{\bf wxSOCKET
\_BROADCAST} flag controls the use of the SO
\_BROADCAST standard
578 setsockopt() flag. This flag allows the socket to use the broadcast address, and is generally
579 used in conjunction with
{\bf wxSOCKET
\_NOBIND} and
\helpref{wxIPaddress::BroadcastAddress
}{wxipaddressbroadcastaddress
}.
583 {\bf wxSOCKET
\_NONE} will try to read at least SOME data, no matter how much.
585 {\bf wxSOCKET
\_NOWAIT} will always return immediately, even if it cannot
586 read or write ANY data.
588 {\bf wxSOCKET
\_WAITALL} will only return when it has read or written ALL
591 {\bf wxSOCKET
\_BLOCK} has nothing to do with the previous flags and
592 it controls whether the GUI blocks.
594 {\bf wxSOCKET
\_REUSEADDR} controls special platform-specific behavior for
595 reusing local addresses/ports.
600 \membersection{wxSocketBase::SetLocal
}\label{wxsocketbasesetlocal
}
602 \func{bool
}{SetLocal
}{\param{wxIPV4address\&
}{ local
}}
604 This function allows you to set the local address and port,
605 useful when an application needs to reuse a particular port. When
606 a local port is set for a
\helpref{wxSocketClient
}{wxsocketclient
},
607 {\bf bind
} will be called before
{\bf connect
}.
612 \membersection{wxSocketBase::SetNotify
}\label{wxsocketbasesetnotify
}
614 \func{void
}{SetNotify
}{\param{wxSocketEventFlags
}{ flags
}}
616 SetNotify specifies which socket events are to be sent to the event handler.
617 The
{\it flags
} parameter may be combination of flags ORed together. The
618 following flags can be used:
621 \begin{twocollist
}\itemsep=
0pt
622 \twocolitem{{\bf wxSOCKET
\_INPUT\_FLAG}}{to receive wxSOCKET
\_INPUT}
623 \twocolitem{{\bf wxSOCKET
\_OUTPUT\_FLAG}}{to receive wxSOCKET
\_OUTPUT}
624 \twocolitem{{\bf wxSOCKET
\_CONNECTION\_FLAG}}{to receive wxSOCKET
\_CONNECTION}
625 \twocolitem{{\bf wxSOCKET
\_LOST\_FLAG}}{to receive wxSOCKET
\_LOST}
631 sock.SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
635 In this example, the user will be notified about incoming socket data and
636 whenever the connection is closed.
638 For more information on socket events see
\helpref{wxSocket events
}{wxsocketbase
}.
643 \membersection{wxSocketBase::SetTimeout
}\label{wxsocketbasesettimeout
}
645 \func{void
}{SetTimeout
}{\param{int
}{seconds
}}
647 This function sets the default socket timeout in seconds. This timeout
648 applies to all IO calls, and also to the
\helpref{Wait
}{wxsocketbasewait
} family
649 of functions if you don't specify a wait interval. Initially, the default
650 timeout is
10 minutes.
655 \membersection{wxSocketBase::Peek
}\label{wxsocketbasepeek
}
657 \func{wxSocketBase\&
}{Peek
}{\param{void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
659 This function peeks a buffer of
{\it nbytes
} bytes from the socket.
660 Peeking a buffer doesn't delete it from the socket input queue.
662 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually peeked.
664 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
666 \wxheading{Parameters
}
668 \docparam{buffer
}{Buffer where to put peeked data.
}
670 \docparam{nbytes
}{Number of bytes.
}
672 \wxheading{Return value
}
674 Returns a reference to the current object.
676 \wxheading{Remark/Warning
}
678 The exact behaviour of wxSocketBase::Peek depends on the combination
679 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
683 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
684 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
685 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
686 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
691 \membersection{wxSocketBase::Read
}\label{wxsocketbaseread
}
693 \func{wxSocketBase\&
}{Read
}{\param{void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
695 This function reads a buffer of
{\it nbytes
} bytes from the socket.
697 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
699 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
701 \wxheading{Parameters
}
703 \docparam{buffer
}{Buffer where to put read data.
}
705 \docparam{nbytes
}{Number of bytes.
}
707 \wxheading{Return value
}
709 Returns a reference to the current object.
711 \wxheading{Remark/Warning
}
713 The exact behaviour of wxSocketBase::Read depends on the combination
714 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
718 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
719 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
720 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
721 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
726 \membersection{wxSocketBase::ReadMsg
}\label{wxsocketbasereadmsg
}
728 \func{wxSocketBase\&
}{ReadMsg
}{\param{void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
730 This function reads a buffer sent by
\helpref{WriteMsg
}{wxsocketbasewritemsg
}
731 on a socket. If the buffer passed to the function isn't big enough, the
732 remaining bytes will be discarded. This function always waits for the
733 buffer to be entirely filled, unless an error occurs.
735 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually read.
737 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
739 \wxheading{Parameters
}
741 \docparam{buffer
}{Buffer where to put read data.
}
743 \docparam{nbytes
}{Size of the buffer.
}
745 \wxheading{Return value
}
747 Returns a reference to the current object.
749 \wxheading{Remark/Warning
}
751 wxSocketBase::ReadMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
752 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
753 The exact behaviour of ReadMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
754 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
758 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
759 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
760 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
761 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
762 \helpref{wxSocketBase::WriteMsg
}{wxsocketbasewritemsg
}
767 \membersection{wxSocketBase::Unread
}\label{wxsocketbaseunread
}
769 \func{wxSocketBase\&
}{Unread
}{\param{const void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
771 This function unreads a buffer. That is, the data in the buffer is put back
772 in the incoming queue. This function is not affected by wxSocket flags.
774 If you use
\helpref{LastCount
}{wxsocketbaselastcount
}, it will always return
{\it nbytes
}.
776 If you use
\helpref{Error
}{wxsocketbaseerror
}, it will always return false.
778 \wxheading{Parameters
}
780 \docparam{buffer
}{Buffer to be unread.
}
782 \docparam{nbytes
}{Number of bytes.
}
784 \wxheading{Return value
}
786 Returns a reference to the current object.
790 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
791 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
792 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
}
797 \membersection{wxSocketBase::Wait
}\label{wxsocketbasewait
}
799 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
801 This function waits until any of the following conditions is true:
804 \item The socket becomes readable.
805 \item The socket becomes writable.
806 \item An ongoing connection request has completed (
\helpref{wxSocketClient
}{wxsocketclient
} only)
807 \item An incoming connection request has arrived (
\helpref{wxSocketServer
}{wxsocketserver
} only)
808 \item The connection has been closed.
811 Note that it is recommended to use the individual Wait functions
812 to wait for the required condition, instead of this one.
814 \wxheading{Parameters
}
816 \docparam{seconds
}{Number of seconds to wait.
817 If -
1, it will wait for the default timeout,
818 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
820 \docparam{millisecond
}{Number of milliseconds to wait.
}
822 \wxheading{Return value
}
824 Returns true when any of the above conditions is satisfied,
825 false if the timeout was reached.
829 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
830 \helpref{wxSocketServer::WaitForAccept
}{wxsocketserverwaitforaccept
},
831 \helpref{wxSocketBase::WaitForLost
}{wxsocketbasewaitforlost
},
832 \helpref{wxSocketBase::WaitForRead
}{wxsocketbasewaitforread
},
833 \helpref{wxSocketBase::WaitForWrite
}{wxsocketbasewaitforwrite
},
834 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
}
839 \membersection{wxSocketBase::WaitForLost
}\label{wxsocketbasewaitforlost
}
841 \func{bool
}{Wait
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
843 This function waits until the connection is lost. This may happen if
844 the peer gracefully closes the connection or if the connection breaks.
846 \wxheading{Parameters
}
848 \docparam{seconds
}{Number of seconds to wait.
849 If -
1, it will wait for the default timeout,
850 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
852 \docparam{millisecond
}{Number of milliseconds to wait.
}
854 \wxheading{Return value
}
856 Returns true if the connection was lost, false if the timeout was reached.
860 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
861 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
}
866 \membersection{wxSocketBase::WaitForRead
}\label{wxsocketbasewaitforread
}
868 \func{bool
}{WaitForRead
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
870 This function waits until the socket is readable. This might mean that
871 queued data is available for reading or, for streamed sockets, that
872 the connection has been closed, so that a read operation will complete
873 immediately without blocking (unless the
{\bf wxSOCKET
\_WAITALL} flag
874 is set, in which case the operation might still block).
876 \wxheading{Parameters
}
878 \docparam{seconds
}{Number of seconds to wait.
879 If -
1, it will wait for the default timeout,
880 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
882 \docparam{millisecond
}{Number of milliseconds to wait.
}
884 \wxheading{Return value
}
886 Returns true if the socket becomes readable, false on timeout.
890 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
891 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
}
896 \membersection{wxSocketBase::WaitForWrite
}\label{wxsocketbasewaitforwrite
}
898 \func{bool
}{WaitForWrite
}{\param{long
}{ seconds = -
1},
\param{long
}{ millisecond =
0}}
900 This function waits until the socket becomes writable. This might mean that
901 the socket is ready to send new data, or for streamed sockets, that the
902 connection has been closed, so that a write operation is guaranteed to
903 complete immediately (unless the
{\bf wxSOCKET
\_WAITALL} flag is set,
904 in which case the operation might still block).
906 \wxheading{Parameters
}
908 \docparam{seconds
}{Number of seconds to wait.
909 If -
1, it will wait for the default timeout,
910 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
912 \docparam{millisecond
}{Number of milliseconds to wait.
}
914 \wxheading{Return value
}
916 Returns true if the socket becomes writable, false on timeout.
920 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
921 \helpref{wxSocketBase::Wait
}{wxsocketbasewait
}
926 \membersection{wxSocketBase::Write
}\label{wxsocketbasewrite
}
928 \func{wxSocketBase\&
}{Write
}{\param{const void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
930 This function writes a buffer of
{\it nbytes
} bytes to the socket.
932 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
934 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
936 \wxheading{Parameters
}
938 \docparam{buffer
}{Buffer with the data to be sent.
}
940 \docparam{nbytes
}{Number of bytes.
}
942 \wxheading{Return value
}
944 Returns a reference to the current object.
946 \wxheading{Remark/Warning
}
948 The exact behaviour of wxSocketBase::Write depends on the combination
949 of flags being used. For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
953 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
954 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
955 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
956 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}
961 \membersection{wxSocketBase::WriteMsg
}\label{wxsocketbasewritemsg
}
963 \func{wxSocketBase\&
}{WriteMsg
}{\param{const void *
}{ buffer
},
\param{wxUint32
}{ nbytes
}}
965 This function writes a buffer of
{\it nbytes
} bytes from the socket, but it
966 writes a short header before so that
\helpref{ReadMsg
}{wxsocketbasereadmsg
}
967 knows how much data should it actually read. So, a buffer sent with WriteMsg
968 {\bf must
} be read with ReadMsg. This function always waits for the entire
969 buffer to be sent, unless an error occurs.
971 Use
\helpref{LastCount
}{wxsocketbaselastcount
} to verify the number of bytes actually written.
973 Use
\helpref{Error
}{wxsocketbaseerror
} to determine if the operation succeeded.
975 \wxheading{Parameters
}
977 \docparam{buffer
}{Buffer with the data to be sent.
}
979 \docparam{nbytes
}{Number of bytes to send.
}
981 \wxheading{Return value
}
983 Returns a reference to the current object.
985 \wxheading{Remark/Warning
}
987 wxSocketBase::WriteMsg will behave as if the
{\bf wxSOCKET
\_WAITALL} flag
988 was always set and it will always ignore the
{\bf wxSOCKET
\_NOWAIT} flag.
989 The exact behaviour of WriteMsg depends on the
{\bf wxSOCKET
\_BLOCK} flag.
990 For a detailed explanation, see
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
}.
994 \helpref{wxSocketBase::Error
}{wxsocketbaseerror
},
995 \helpref{wxSocketBase::LastError
}{wxsocketbaselasterror
},
996 \helpref{wxSocketBase::LastCount
}{wxsocketbaselastcount
},
997 \helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
},
998 \helpref{wxSocketBase::ReadMsg
}{wxsocketbasereadmsg
}
1001 % ---------------------------------------------------------------------------
1002 % CLASS wxSocketClient
1003 % ---------------------------------------------------------------------------
1005 \section{\class{wxSocketClient
}}\label{wxsocketclient
}
1007 \wxheading{Derived from
}
1009 \helpref{wxSocketBase
}{wxsocketbase
}
1011 \wxheading{Include files
}
1017 \helpref{wxNet
}{librarieslist
}
1019 \latexignore{\rtfignore{\wxheading{Members
}}}
1021 % ---------------------------------------------------------------------------
1023 % ---------------------------------------------------------------------------
1027 \membersection{wxSocketClient::wxSocketClient
}\label{wxsocketclientctor
}
1029 \func{}{wxSocketClient
}{\param{wxSocketFlags
}{ flags = wxSOCKET
\_NONE}}
1033 \wxheading{Parameters
}
1035 \docparam{flags
}{Socket flags (See
\helpref{wxSocketBase::SetFlags
}{wxsocketbasesetflags
})
}
1040 \membersection{wxSocketClient::
\destruct{wxSocketClient
}}\label{wxsocketclientdtor
}
1042 \func{}{\destruct{wxSocketClient
}}{\void}
1044 Destructor. Please see
\helpref{wxSocketBase::Destroy
}{wxsocketbasedestroy
}.
1049 \membersection{wxSocketClient::Connect
}\label{wxsocketclientconnect
}
1051 \func{bool
}{Connect
}{\param{wxSockAddress\&
}{ address
},
\param{bool
}{ wait = true
}}
1053 \func{bool
}{Connect
}{\param{wxSockAddress\&
}{ address
},
\param{wxSockAddress\&
}{ local
},
1054 \param{bool
}{ wait = true
}}
1056 Connects to a server using the specified address.
1058 If
{\it wait
} is true, Connect will wait until the connection
1059 completes.
{\bf Warning:
} This will block the GUI.
1061 If
{\it wait
} is false, Connect will try to establish the connection and
1062 return immediately, without blocking the GUI. When used this way, even if
1063 Connect returns false, the connection request can be completed later.
1064 To detect this, use
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
},
1065 or catch
{\bf wxSOCKET
\_CONNECTION} events (for successful establishment)
1066 and
{\bf wxSOCKET
\_LOST} events (for connection failure).
1068 \wxheading{Parameters
}
1070 \docparam{address
}{Address of the server.
}
1072 \docparam{local
}{Bind to the specified local address and port before connecting.
1073 The local address and port can also be set using
\helpref{SetLocal
}{wxsocketbasesetlocal
},
1074 and then using the
2-parameter Connect method.
}
1076 \docparam{wait
}{If true, waits for the connection to complete.
}
1078 \wxheading{Return value
}
1080 Returns true if the connection is established and no error occurs.
1082 If
{\it wait
} was true, and Connect returns false, an error occurred
1083 and the connection failed.
1085 If
{\it wait
} was false, and Connect returns false, you should still
1086 be prepared to handle the completion of this connection request, either
1087 with
\helpref{WaitOnConnect
}{wxsocketclientwaitonconnect
} or by
1088 watching
{\bf wxSOCKET
\_CONNECTION} and
{\bf wxSOCKET
\_LOST} events.
1090 \wxheading{See also
}
1092 \helpref{wxSocketClient::WaitOnConnect
}{wxsocketclientwaitonconnect
},
1093 \helpref{wxSocketBase::SetNotify
}{wxsocketbasesetnotify
},
1094 \helpref{wxSocketBase::Notify
}{wxsocketbasenotify
}
1099 \membersection{wxSocketClient::WaitOnConnect
}\label{wxsocketclientwaitonconnect
}
1101 \func{bool
}{WaitOnConnect
}{\param{long
}{ seconds = -
1},
\param{long
}{ milliseconds =
0}}
1103 Wait until a connection request completes, or until the specified timeout
1104 elapses. Use this function after issuing a call
1105 to
\helpref{Connect
}{wxsocketclientconnect
} with
{\it wait
} set to false.
1107 \wxheading{Parameters
}
1109 \docparam{seconds
}{Number of seconds to wait.
1110 If -
1, it will wait for the default timeout,
1111 as set with
\helpref{SetTimeout
}{wxsocketbasesettimeout
}.
}
1113 \docparam{millisecond
}{Number of milliseconds to wait.
}
1115 \wxheading{Return value
}
1117 WaitOnConnect returns true if the connection request completes. This
1118 does not necessarily mean that the connection was successfully established;
1119 it might also happen that the connection was refused by the peer. Use
1120 \helpref{IsConnected
}{wxsocketbaseisconnected
} to distinguish between
1121 these two situations.
1123 If the timeout elapses, WaitOnConnect returns false.
1125 These semantics allow code like this:
1128 // Issue the connection request
1129 client->Connect(addr, false);
1131 // Wait until the request completes or until we decide to give up
1132 bool waitmore = true;
1133 while ( !client->WaitOnConnect(seconds, millis) && waitmore )
1135 // possibly give some feedback to the user,
1136 // and update waitmore as needed.
1138 bool success = client->IsConnected();
1141 \wxheading{See also
}
1143 \helpref{wxSocketClient::Connect
}{wxsocketclientconnect
},
1144 \helpref{wxSocketBase::InterruptWait
}{wxsocketbaseinterruptwait
},
1145 \helpref{wxSocketBase::IsConnected
}{wxsocketbaseisconnected
}
1147 % ---------------------------------------------------------------------------
1148 % CLASS: wxSocketEvent
1149 % ---------------------------------------------------------------------------
1150 \section{\class{wxSocketEvent
}}\label{wxsocketevent
}
1152 This event class contains information about socket events.
1154 \wxheading{Derived from
}
1156 \helpref{wxEvent
}{wxevent
}
1158 \wxheading{Include files
}
1164 \helpref{wxNet
}{librarieslist
}
1166 \wxheading{Event table macros
}
1168 To process a socket event, use these event handler macros to direct input
1169 to member functions that take a wxSocketEvent argument.
1172 \begin{twocollist
}\itemsep=
0pt
1173 \twocolitem{{\bf EVT
\_SOCKET(id, func)
}}{Process a socket event, supplying the member function.
}
1176 \wxheading{See also
}
1178 \helpref{wxSocketBase
}{wxsocketbase
},
1179 \helpref{wxSocketClient
}{wxsocketclient
},
1180 \helpref{wxSocketServer
}{wxsocketserver
}
1182 \latexignore{\rtfignore{\wxheading{Members
}}}
1184 \membersection{wxSocketEvent::wxSocketEvent
}\label{wxsocketeventctor
}
1186 \func{}{wxSocketEvent
}{\param{int
}{ id =
0}}
1190 \membersection{wxSocketEvent::GetClientData
}\label{wxsocketeventgetclientdata
}
1192 \func{void *
}{GetClientData
}{\void}
1194 Gets the client data of the socket which generated this event, as
1195 set with
\helpref{wxSocketBase::SetClientData
}{wxsocketbasesetclientdata
}.
1197 \membersection{wxSocketEvent::GetSocket
}\label{wxsocketeventgetsocket
}
1199 \constfunc{wxSocketBase *
}{GetSocket
}{\void}
1201 Returns the socket object to which this event refers to. This makes
1202 it possible to use the same event handler for different sockets.
1204 \membersection{wxSocketEvent::GetSocketEvent
}\label{wxsocketeventgetsocketevent
}
1206 \constfunc{wxSocketNotify
}{GetSocketEvent
}{\void}
1208 Returns the socket event type.