]>
git.saurik.com Git - wxWidgets.git/blob - interface/socket.h
08788d2c37a5b1d6041940eb874a1f171e898b40
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: documentation for wxIPV4address class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
17 class wxIPV4address
: public wxIPaddress
21 Set address to any of the addresses of the current machine. Whenever
22 possible, use this function instead of LocalHost(),
23 as this correctly handles multi-homed hosts and avoids other small
24 problems. Internally, this is the same as setting the IP address
27 @returns Returns @true on success, @false if something went wrong.
33 Returns the hostname which matches the IP address.
35 bool Hostname(const wxString
& hostname
);
36 Return value wxString
Hostname();
40 Returns a wxString containing the IP address in dot quad (127.0.0.1) format.
45 Set address to localhost (127.0.0.1). Whenever possible, use the
47 function instead of this one, as this will correctly handle multi-homed
48 hosts and avoid other small problems.
54 Returns the current service.
56 bool Service(const wxString
& service
);
57 Return value
bool Service(unsigned short service
);
58 Return value
unsigned short Service();
72 wxSocketServer::WaitForAccept, wxSocketBase::SetNotify, wxSocketBase::Notify,
73 wxSocketServer::AcceptWith
75 class wxSocketServer
: public wxSocketBase
79 Constructs a new server and tries to bind to the specified @e address.
80 Before trying to accept new connections, test whether it succeeded with
81 @ref wxSocketBase::isok wxSocketBase:IsOk.
84 Specifies the local address for the server (e.g. port number).
86 Socket flags (See wxSocketBase::SetFlags)
88 wxSocketServer(const wxSockAddress
& address
,
89 wxSocketFlags flags
= wxSOCKET_NONE
);
92 Destructor (it doesn't close the accepted connections).
97 Accepts an incoming connection request, and creates a new
98 wxSocketBase object which represents
99 the server-side of the connection.
100 If @a wait is @true and there are no pending connections to be
101 accepted, it will wait for the next incoming connection to
102 arrive. @b Warning: This will block the GUI.
103 If @a wait is @false, it will try to accept a pending connection
104 if there is one, but it will always return immediately without blocking
105 the GUI. If you want to use Accept in this way, you can either check for
106 incoming connections with WaitForAccept()
107 or catch @b wxSOCKET_CONNECTION events, then call Accept once you know
108 that there is an incoming connection waiting to be accepted.
110 @returns Returns an opened socket connection, or @NULL if an error
111 occurred or if the wait parameter was @false and there
112 were no pending connections.
114 @see WaitForAccept(), wxSocketBase::SetNotify,
115 wxSocketBase::Notify, AcceptWith()
117 wxSocketBase
* Accept(bool wait
= true);
120 Accept an incoming connection using the specified socket object.
123 Socket to be initialized
125 @returns Returns @true on success, or @false if an error occurred or if the
126 wait parameter was @false and there were no pending
129 bool AcceptWith(wxSocketBase
& socket
, bool wait
= true);
132 This function waits for an incoming connection. Use it if you want to call
133 Accept() or AcceptWith()
134 with @e wait set to @false, to detect when an incoming connection is waiting
138 Number of seconds to wait.
139 If -1, it will wait for the default timeout,
140 as set with SetTimeout.
142 Number of milliseconds to wait.
144 @returns Returns @true if an incoming connection arrived, @false if the
147 bool WaitForAccept(long seconds
= -1, long millisecond
= 0);
155 wxIPaddress is an abstract base class for all internet protocol address
156 objects. Currently, only wxIPV4address
157 is implemented. An experimental implementation for IPV6, wxIPV6address,
163 class wxIPaddress
: public wxSockAddress
167 Internally, this is the same as setting the IP address
169 On IPV4 implementations, 0.0.0.0
170 On IPV6 implementations, ::
172 @returns Returns @true on success, @false if something went wrong.
174 virtual bool AnyAddress();
177 Internally, this is the same as setting the IP address
178 to @b INADDR_BROADCAST.
179 On IPV4 implementations, 255.255.255.255
181 @returns Returns @true on success, @false if something went wrong.
183 virtual bool BroadcastAddress();
187 Returns the hostname which matches the IP address.
189 virtual bool Hostname(const wxString
& hostname
);
190 Return value
virtual wxString
Hostname();
194 Returns a wxString containing the IP address.
196 virtual wxString
IPAddress();
199 Determines if current address is set to localhost.
201 virtual bool IsLocalHost();
204 Set address to localhost.
205 On IPV4 implementations, 127.0.0.1
206 On IPV6 implementations, ::1
208 @returns Returns @true on success, @false if something went wrong.
210 virtual bool LocalHost();
214 Returns the current service.
216 virtual bool Service(const wxString
& service
);
217 Return value
virtual bool Service(unsigned short service
);
218 Return value
virtual unsigned short Service();
224 @class wxSocketClient
232 wxSocketClient::WaitOnConnect, wxSocketBase::SetNotify, wxSocketBase::Notify
234 class wxSocketClient
: public wxSocketBase
241 Socket flags (See wxSocketBase::SetFlags)
243 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
246 Destructor. Please see wxSocketBase::Destroy.
252 Connects to a server using the specified address.
253 If @a wait is @true, Connect will wait until the connection
254 completes. @b Warning: This will block the GUI.
255 If @a wait is @false, Connect will try to establish the connection and
256 return immediately, without blocking the GUI. When used this way, even if
257 Connect returns @false, the connection request can be completed later.
258 To detect this, use WaitOnConnect(),
259 or catch @b wxSOCKET_CONNECTION events (for successful establishment)
260 and @b wxSOCKET_LOST events (for connection failure).
263 Address of the server.
265 Bind to the specified local address and port before connecting.
266 The local address and port can also be set using SetLocal,
267 and then using the 2-parameter Connect method.
269 If @true, waits for the connection to complete.
271 @returns Returns @true if the connection is established and no error
274 @see WaitOnConnect(), wxSocketBase::SetNotify,
277 bool Connect(wxSockAddress
& address
, bool wait
= true);
278 bool Connect(wxSockAddress
& address
, wxSockAddress
& local
,
283 Wait until a connection request completes, or until the specified timeout
284 elapses. Use this function after issuing a call
285 to Connect() with @e wait set to @false.
288 Number of seconds to wait.
289 If -1, it will wait for the default timeout,
290 as set with SetTimeout.
292 Number of milliseconds to wait.
294 @returns WaitOnConnect returns @true if the connection request completes.
295 This does not necessarily mean that the connection was
296 successfully established; it might also happen that the
297 connection was refused by the peer. Use IsConnected to
298 distinguish between these two situations.
300 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
308 You are unlikely to need to use this class: only wxSocketBase uses it.
314 wxSocketBase, wxIPaddress, wxIPV4address
316 class wxSockAddress
: public wxObject
330 Delete all informations about the address.
335 Returns the length of the socket address.
345 This event class contains information about socket events.
351 wxSocketBase, wxSocketClient, wxSocketServer
353 class wxSocketEvent
: public wxEvent
359 wxSocketEvent(int id
= 0);
362 Gets the client data of the socket which generated this event, as
363 set with wxSocketBase::SetClientData.
365 void* GetClientData();
368 Returns the socket object to which this event refers to. This makes
369 it possible to use the same event handler for different sockets.
371 wxSocketBase
* GetSocket();
374 Returns the socket event type.
376 wxSocketNotify
GetSocketEvent();
384 wxSocketBase is the base class for all socket-related objects, and it
385 defines all basic IO functionality.
387 Note: (Workaround for implementation limitation for wxWidgets up to 2.5.x)
388 If you want to use sockets or derived classes such as wxFTP in a secondary
390 call wxSocketBase::Initialize() (undocumented) from the main thread before
392 any sockets - in wxApp::OnInit for example.
393 See http://wiki.wxwidgets.org/wiki.pl?WxSocket or
394 http://www.litwindow.com/knowhow/knowhow.html for more details.
400 wxSocketEvent, wxSocketClient, wxSocketServer, @ref overview_samplesockets
403 class wxSocketBase
: public wxObject
407 Default constructor. Don't use it directly; instead, use
408 wxSocketClient to construct a socket client, or
409 wxSocketServer to construct a socket server.
414 Destructor. Do not destroy a socket using the delete operator directly;
415 use Destroy() instead. Also, do not create
416 socket objects in the stack.
421 Functions that perform basic IO functionality.
437 Functions that perform a timed wait on a certain IO condition.
449 wxSocketServer::WaitForAccept
451 wxSocketClient::WaitOnConnect
452 Functions that allow applications to customize socket IO as needed.
464 This function shuts down the socket, disabling further transmission and
465 reception of data; it also disables events for the socket and frees the
466 associated system resources. Upon socket destruction, Close is automatically
467 called, so in most cases you won't need to do it yourself, unless you
468 explicitly want to shut down the socket, typically to notify the peer
469 that you are closing the connection.
474 @ref construct() wxSocketBase
476 @ref destruct() ~wxSocketBase
483 Destroys the socket safely. Use this function instead of the delete operator,
484 since otherwise socket events could reach the application even after the
485 socket has been destroyed. To prevent this problem, this function appends
486 the wxSocket to a list of object to be deleted on idle time, after all
487 events have been processed. For the same reason, you should avoid creating
488 socket objects in the stack.
489 Destroy calls Close() automatically.
491 @returns Always @true.
496 This function simply deletes all bytes in the incoming queue. This function
497 always returns immediately and its operation is not affected by IO flags.
498 Use LastCount() to verify the number of bytes actually discarded.
499 If you use Error(), it will always return @false.
501 wxSocketBase
Discard();
504 Returns @true if an error occurred in the last IO operation.
505 Use this function to check for an error condition after one of the
506 following calls: Discard, Peek, Read, ReadMsg, Unread, Write, WriteMsg.
511 Returns a pointer of the client data for this socket, as set with
514 void* GetClientData();
517 Returns current IO flags, as set with SetFlags()
519 wxSocketFlags
GetFlags();
522 This function returns the local address field of the socket. The local
523 address field contains the complete local address of the socket (local
524 address, local port, ...).
526 @returns @true if no error happened, @false otherwise.
528 bool GetLocal(wxSockAddress
& addr
);
531 This function returns the peer address field of the socket. The peer
532 address field contains the complete peer host address of the socket
533 (address, port, ...).
535 @returns @true if no error happened, @false otherwise.
537 bool GetPeer(wxSockAddress
& addr
);
540 Functions that allow applications to receive socket events.
554 Use this function to interrupt any wait operation currently in progress.
555 Note that this is not intended as a regular way to interrupt a Wait call,
556 but only as an escape mechanism for exceptional situations where it is
557 absolutely necessary to use it, for example to abort an operation due to
558 some exception or abnormal problem. InterruptWait is automatically called
559 when you Close() a socket (and thus also upon
560 socket destruction), so you don't need to use it in these cases.
562 wxSocketServer::WaitForAccept,
566 wxSocketClient::WaitOnConnect
568 void InterruptWait();
571 Returns @true if the socket is connected.
576 This function waits until the socket is readable. This might mean that
577 queued data is available for reading or, for streamed sockets, that
578 the connection has been closed, so that a read operation will complete
579 immediately without blocking (unless the @b wxSOCKET_WAITALL flag
580 is set, in which case the operation might still block).
585 Returns @true if the socket is not connected.
587 bool IsDisconnected();
590 Returns @true if the socket is initialized and ready and @false in other
596 Returns the number of bytes read or written by the last IO call.
597 Use this function to get the number of bytes actually transferred
598 after using one of the following IO calls: Discard, Peek, Read,
599 ReadMsg, Unread, Write, WriteMsg.
601 wxUint32
LastCount();
604 Returns the last wxSocket error. See @ref overview_wxsocketbase "wxSocket
606 Please note that this function merely returns the last error code,
607 but it should not be used to determine if an error has occurred (this
608 is because successful operations do not change the LastError value).
609 Use Error() first, in order to determine
610 if the last IO call failed. If this returns @true, use LastError
611 to discover the cause of the error.
613 wxSocketError
LastError();
616 According to the @a notify value, this function enables
617 or disables socket events. If @a notify is @true, the events
618 configured with SetNotify() will
619 be sent to the application. If @a notify is @false; no events
622 void Notify(bool notify
);
625 This function peeks a buffer of @a nbytes bytes from the socket.
626 Peeking a buffer doesn't delete it from the socket input queue.
627 Use LastCount() to verify the number of bytes actually peeked.
628 Use Error() to determine if the operation succeeded.
631 Buffer where to put peeked data.
635 @returns Returns a reference to the current object.
637 @see Error(), LastError(), LastCount(),
640 wxSocketBase
Peek(void* buffer
, wxUint32 nbytes
);
643 This function reads a buffer of @a nbytes bytes from the socket.
644 Use LastCount() to verify the number of bytes actually read.
645 Use Error() to determine if the operation succeeded.
648 Buffer where to put read data.
652 @returns Returns a reference to the current object.
654 @see Error(), LastError(), LastCount(),
657 wxSocketBase
Read(void* buffer
, wxUint32 nbytes
);
660 This function reads a buffer sent by WriteMsg()
661 on a socket. If the buffer passed to the function isn't big enough, the
662 remaining bytes will be discarded. This function always waits for the
663 buffer to be entirely filled, unless an error occurs.
664 Use LastCount() to verify the number of bytes actually read.
665 Use Error() to determine if the operation succeeded.
668 Buffer where to put read data.
672 @returns Returns a reference to the current object.
674 @see Error(), LastError(), LastCount(),
675 SetFlags(), WriteMsg()
677 wxSocketBase
ReadMsg(void* buffer
, wxUint32 nbytes
);
680 This function restores the previous state of the socket, as saved
682 Calls to SaveState and RestoreState can be nested.
689 This function saves the current state of the socket in a stack. Socket
690 state includes flags, as set with SetFlags(),
691 event mask, as set with SetNotify() and
692 Notify(), user data, as set with
694 Calls to SaveState and RestoreState can be nested.
701 Sets user-supplied client data for this socket. All socket events will
702 contain a pointer to this data, which can be retrieved with
703 the wxSocketEvent::GetClientData function.
705 void SetClientData(void* data
);
708 Sets an event handler to be called when a socket event occurs. The
709 handler will be called for those events for which notification is
710 enabled with SetNotify() and
714 Specifies the event handler you want to use.
716 The id of socket event.
718 @see SetNotify(), Notify(), wxSocketEvent, wxEvtHandler
720 void SetEventHandler(wxEvtHandler
& handler
, int id
= -1);
723 Use SetFlags to customize IO operation for this socket.
724 The @a flags parameter may be a combination of flags ORed together.
725 The following flags can be used:
729 Normal functionality.
733 Read/write as much data as possible and return immediately.
737 Wait for all required data to be read/written unless an error occurs.
741 Block the GUI (do not yield) while reading/writing data.
743 @b wxSOCKET_REUSEADDR
745 Allows the use of an in-use port (wxServerSocket only)
747 @b wxSOCKET_BROADCAST
749 Switches the socket to broadcast mode
753 Stops the socket from being bound to a specific adapter (normally used in
754 conjunction with @b wxSOCKET_BROADCAST)
756 A brief overview on how to use these flags follows.
757 If no flag is specified (this is the same as @b wxSOCKET_NONE),
758 IO calls will return after some data has been read or written, even
759 when the transfer might not be complete. This is the same as issuing
760 exactly one blocking low-level call to recv() or send(). Note
761 that @e blocking here refers to when the function returns, not
762 to whether the GUI blocks during this time.
763 If @b wxSOCKET_NOWAIT is specified, IO calls will return immediately.
764 Read operations will retrieve only available data. Write operations will
765 write as much data as possible, depending on how much space is available
766 in the output buffer. This is the same as issuing exactly one nonblocking
767 low-level call to recv() or send(). Note that @e nonblocking here
768 refers to when the function returns, not to whether the GUI blocks during
770 If @b wxSOCKET_WAITALL is specified, IO calls won't return until ALL
771 the data has been read or written (or until an error occurs), blocking if
772 necessary, and issuing several low level calls if necessary. This is the
773 same as having a loop which makes as many blocking low-level calls to
774 recv() or send() as needed so as to transfer all the data. Note
775 that @e blocking here refers to when the function returns, not
776 to whether the GUI blocks during this time.
777 The @b wxSOCKET_BLOCK flag controls whether the GUI blocks during
778 IO operations. If this flag is specified, the socket will not yield
779 during IO calls, so the GUI will remain blocked until the operation
780 completes. If it is not used, then the application must take extra
781 care to avoid unwanted reentrance.
782 The @b wxSOCKET_REUSEADDR flag controls the use of the SO_REUSEADDR standard
783 setsockopt() flag. This flag allows the socket to bind to a port that is
785 This is mostly used on UNIX-based systems to allow rapid starting and stopping
787 otherwise you may have to wait several minutes for the port to become available.
788 wxSOCKET_REUSEADDR can also be used with socket clients to (re)bind to a
789 particular local port
790 for an outgoing connection.
791 This option can have surprising platform dependent behavior, so check the
793 your platform's implementation of setsockopt(). Note that on BSD-based systems
795 use of wxSOCKET_REUSEADDR implies SO_REUSEPORT in addition to SO_REUSEADDR to
798 The @b wxSOCKET_BROADCAST flag controls the use of the SO_BROADCAST standard
799 setsockopt() flag. This flag allows the socket to use the broadcast address,
801 used in conjunction with @b wxSOCKET_NOBIND and wxIPaddress::BroadcastAddress.
803 @b wxSOCKET_NONE will try to read at least SOME data, no matter how much.
804 @b wxSOCKET_NOWAIT will always return immediately, even if it cannot
805 read or write ANY data.
806 @b wxSOCKET_WAITALL will only return when it has read or written ALL
808 @b wxSOCKET_BLOCK has nothing to do with the previous flags and
809 it controls whether the GUI blocks.
810 @b wxSOCKET_REUSEADDR controls special platform-specific behavior for
811 reusing local addresses/ports.
813 void SetFlags(wxSocketFlags flags
);
816 This function allows you to set the local address and port,
817 useful when an application needs to reuse a particular port. When
818 a local port is set for a wxSocketClient,
819 @b bind will be called before @b connect.
821 bool SetLocal(wxIPV4address
& local
);
824 SetNotify specifies which socket events are to be sent to the event handler.
825 The @a flags parameter may be combination of flags ORed together. The
826 following flags can be used:
828 @b wxSOCKET_INPUT_FLAG
830 to receive wxSOCKET_INPUT
832 @b wxSOCKET_OUTPUT_FLAG
834 to receive wxSOCKET_OUTPUT
836 @b wxSOCKET_CONNECTION_FLAG
838 to receive wxSOCKET_CONNECTION
840 @b wxSOCKET_LOST_FLAG
842 to receive wxSOCKET_LOST
846 In this example, the user will be notified about incoming socket data and
847 whenever the connection is closed.
848 For more information on socket events see @ref overview_wxsocketbase "wxSocket
851 void SetNotify(wxSocketEventFlags flags
);
854 This function sets the default socket timeout in seconds. This timeout
855 applies to all IO calls, and also to the Wait() family
856 of functions if you don't specify a wait interval. Initially, the default
857 timeout is 10 minutes.
859 void SetTimeout(int seconds
);
862 Functions to retrieve current state and miscellaneous info.
887 This function unreads a buffer. That is, the data in the buffer is put back
888 in the incoming queue. This function is not affected by wxSocket flags.
889 If you use LastCount(), it will always return @e nbytes.
890 If you use Error(), it will always return @false.
897 @returns Returns a reference to the current object.
899 @see Error(), LastCount(), LastError()
901 wxSocketBase
Unread(const void* buffer
, wxUint32 nbytes
);
904 This function waits until any of the following conditions is @true:
906 The socket becomes readable.
907 The socket becomes writable.
908 An ongoing connection request has completed (wxSocketClient only)
909 An incoming connection request has arrived (wxSocketServer only)
910 The connection has been closed.
911 Note that it is recommended to use the individual Wait functions
912 to wait for the required condition, instead of this one.
915 Number of seconds to wait.
916 If -1, it will wait for the default timeout,
917 as set with SetTimeout.
919 Number of milliseconds to wait.
921 @returns Returns @true when any of the above conditions is satisfied,
922 @false if the timeout was reached.
924 @see InterruptWait(), wxSocketServer::WaitForAccept,
925 WaitForLost(), WaitForRead(),
926 WaitForWrite(), wxSocketClient::WaitOnConnect
928 bool Wait(long seconds
= -1, long millisecond
= 0);
931 This function waits until the connection is lost. This may happen if
932 the peer gracefully closes the connection or if the connection breaks.
935 Number of seconds to wait.
936 If -1, it will wait for the default timeout,
937 as set with SetTimeout.
939 Number of milliseconds to wait.
941 @returns Returns @true if the connection was lost, @false if the timeout
944 @see InterruptWait(), Wait()
946 bool Wait(long seconds
= -1, long millisecond
= 0);
949 This function waits until the socket is readable. This might mean that
950 queued data is available for reading or, for streamed sockets, that
951 the connection has been closed, so that a read operation will complete
952 immediately without blocking (unless the @b wxSOCKET_WAITALL flag
953 is set, in which case the operation might still block).
956 Number of seconds to wait.
957 If -1, it will wait for the default timeout,
958 as set with SetTimeout.
960 Number of milliseconds to wait.
962 @returns Returns @true if the socket becomes readable, @false on timeout.
964 @see InterruptWait(), Wait()
966 bool WaitForRead(long seconds
= -1, long millisecond
= 0);
969 This function waits until the socket becomes writable. This might mean that
970 the socket is ready to send new data, or for streamed sockets, that the
971 connection has been closed, so that a write operation is guaranteed to
972 complete immediately (unless the @b wxSOCKET_WAITALL flag is set,
973 in which case the operation might still block).
976 Number of seconds to wait.
977 If -1, it will wait for the default timeout,
978 as set with SetTimeout.
980 Number of milliseconds to wait.
982 @returns Returns @true if the socket becomes writable, @false on timeout.
984 @see InterruptWait(), Wait()
986 bool WaitForWrite(long seconds
= -1, long millisecond
= 0);
989 This function writes a buffer of @a nbytes bytes to the socket.
990 Use LastCount() to verify the number of bytes actually written.
991 Use Error() to determine if the operation succeeded.
994 Buffer with the data to be sent.
998 @returns Returns a reference to the current object.
1000 @see Error(), LastError(), LastCount(),
1003 wxSocketBase
Write(const void* buffer
, wxUint32 nbytes
);
1006 This function writes a buffer of @a nbytes bytes from the socket, but it
1007 writes a short header before so that ReadMsg()
1008 knows how much data should it actually read. So, a buffer sent with WriteMsg
1009 @b must be read with ReadMsg. This function always waits for the entire
1010 buffer to be sent, unless an error occurs.
1011 Use LastCount() to verify the number of bytes actually written.
1012 Use Error() to determine if the operation succeeded.
1015 Buffer with the data to be sent.
1017 Number of bytes to send.
1019 @returns Returns a reference to the current object.
1021 wxSocketBase
WriteMsg(const void* buffer
, wxUint32 nbytes
);
1026 @class wxDatagramSocket
1034 wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount,
1035 wxSocketBase::SetFlags,
1037 class wxDatagramSocket
: public wxSocketBase
1044 Socket flags (See wxSocketBase::SetFlags)
1046 wxDatagramSocket(wxSocketFlags flags
= wxSOCKET_NONE
);
1049 Destructor. Please see wxSocketBase::Destroy.
1051 ~wxDatagramSocket();
1054 This function reads a buffer of @a nbytes bytes from the socket.
1055 Use wxSocketBase::LastCount to verify the number of bytes actually read.
1056 Use wxSocketBase::Error to determine if the operation succeeded.
1059 Any address - will be overwritten with the address of the peer that sent
1062 Buffer where to put read data.
1066 @returns Returns a reference to the current object, and the address of
1067 the peer that sent the data on address param.
1069 @see wxSocketBase::Error, wxSocketBase::LastError, wxSocketBase::LastCount,
1070 wxSocketBase::SetFlags,
1072 wxDatagramSocket
ReceiveFrom(wxSockAddress
& address
,
1077 This function writes a buffer of @a nbytes bytes to the socket.
1078 Use wxSocketBase::LastCount to verify the number of bytes actually wrote.
1079 Use wxSocketBase::Error to determine if the operation succeeded.
1082 The address of the destination peer for this data.
1084 Buffer where read data is.
1088 @returns Returns a reference to the current object.
1090 wxDatagramSocket
SendTo(const wxSockAddress
& address
,