File: qudpsocket.html

package info (click to toggle)
python-qt4 4.0.1-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 18,632 kB
  • ctags: 2,639
  • sloc: python: 29,409; sh: 5,646; cpp: 3,168; xml: 149; makefile: 109
file content (105 lines) | stat: -rw-r--r-- 14,905 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QUdpSocket Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QUdpSocket Class Reference<br /><sup><sup>[<a href="qtnetwork.html">QtNetwork</a> module]</sup></sup></h1><p>The QUdpSocket class provides a UDP socket. <a href="#details">More...</a></p>
<p>Inherits <a href="qabstractsocket.html">QAbstractSocket</a>.</p><h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qudpsocket.html#BindFlag-enum">BindFlag</a></b> { DefaultForPlatform, ShareAddress, DontShareAddress, ReuseAddressHint }</li><li><div class="fn" />class <b><a href="qudpsocket-bindmode.html">BindMode</a></b></li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qudpsocket.html#QUdpSocket">__init__</a></b> (<i>self</i>, QObject&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" />bool <b><a href="qudpsocket.html#bind">bind</a></b> (<i>self</i>, QHostAddress&#160;<i>address</i>, unsigned long&#160;<i>port</i>)</li><li><div class="fn" />bool <b><a href="qudpsocket.html#bind-2">bind</a></b> (<i>self</i>, unsigned long&#160;<i>port</i>&#160;=&#160;0)</li><li><div class="fn" />bool <b><a href="qudpsocket.html#bind-3">bind</a></b> (<i>self</i>, QHostAddress&#160;<i>address</i>, unsigned long&#160;<i>port</i>, BindMode&#160;<i>mode</i>)</li><li><div class="fn" />bool <b><a href="qudpsocket.html#bind-4">bind</a></b> (<i>self</i>, unsigned long&#160;<i>port</i>, BindMode&#160;<i>mode</i>)</li><li><div class="fn" />bool <b><a href="qudpsocket.html#hasPendingDatagrams">hasPendingDatagrams</a></b> (<i>self</i>)</li><li><div class="fn" />long <b><a href="qudpsocket.html#pendingDatagramSize">pendingDatagramSize</a></b> (<i>self</i>)</li><li><div class="fn" />(object, QHostAddress&#160;<i>host</i>, unsigned long&#160;<i>port</i>) <b><a href="qudpsocket.html#readDatagram">readDatagram</a></b> (<i>self</i>, long&#160;<i>maxlen</i>)</li><li><div class="fn" />long <b><a href="qudpsocket.html#writeDatagram">writeDatagram</a></b> (<i>self</i>, str&#160;<i>data</i>, QHostAddress&#160;<i>host</i>, unsigned long&#160;<i>port</i>)</li><li><div class="fn" />long <b><a href="qudpsocket.html#writeDatagram-2">writeDatagram</a></b> (<i>self</i>, QByteArray&#160;<i>datagram</i>, QHostAddress&#160;<i>host</i>, unsigned long&#160;<i>port</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QUdpSocket class provides a UDP socket.</p>
<p>UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn't important. QUdpSocket is a subclass of <a href="qabstractsocket.html">QAbstractSocket</a> that allows you to send and receive UDP datagrams.</p>
<p>The most common way to use this class is to bind to an address and port using <a href="qudpsocket.html#bind">bind</a>(), then call <a href="qudpsocket.html#writeDatagram">writeDatagram</a>() and <a href="qudpsocket.html#readDatagram">readDatagram</a>() to transfer data.</p>
<p>The socket emits the <a href="qiodevice.html#bytesWritten">bytesWritten</a>() signal every time a datagram is written to the network. If you just want to send datagrams, you don't need to call <a href="qudpsocket.html#bind">bind</a>().</p>
<p>The <a href="qiodevice.html#readyRead">readyRead</a>() signal is emitted whenever datagrams arrive. In that case, <a href="qudpsocket.html#hasPendingDatagrams">hasPendingDatagrams</a>() returns true. Call <a href="qudpsocket.html#pendingDatagramSize">pendingDatagramSize</a>() to obtain the size of the first pending datagram, and <a href="qudpsocket.html#readDatagram">readDatagram</a>() to read it.</p>
<p>Example:</p>
<pre>
    void Server.initSocket()
    {
        udpSocket = new QUdpSocket(this);
        udpSocket-&gt;bind(QHostAddress.LocalHost, 7755);

        connect(udpSocket, SIGNAL(readyRead()),
                this, SLOT(readPendingDatagrams()));
    }

    void Server.readPendingDatagrams()
    {
        while (udpSocket-&gt;hasPendingDatagrams()) {
            QByteArray datagram;
            datagram.resize(udpSocket-&gt;pendingDatagramSize());
            QHostAddress sender;
            quint16 senderPort;

            udpSocket-&gt;readDatagram(datagram.data(), datagram.size(),
                                    &amp;sender, &amp;senderPort);

            processTheDatagram(datagram);
        }
    }
</pre>
<p>With QUdpSocket, you can also establish a virtual connection to a UDP server using <a href="qabstractsocket.html#connectToHost">connectToHost</a>() and then use <a href="qiodevice.html#read">read</a>() and <a href="qiodevice.html#write">write</a>() to exchange datagrams without specifying the receiver for each datagram.</p>
<p>The <a href="network-broadcastsender.html">Broadcast Sender</a> and <a href="network-broadcastreceiver.html">Broadcast Receiver</a> examples illustrate how to use QUdpSocket in applications.</p>
<p>See also <a href="qtcpsocket.html">QTcpSocket</a>.</p>
<hr /><h2>Type Documentation</h2><h3 class="fn"><a name="BindFlag-enum" />QUdpSocket.BindFlag</h3><p>This enum describes the different flags you can pass to modify the behavior of <a href="qudpsocket.html#bind">QUdpSocket.bind</a>().</p>
<table border="1" cellpadding="2" cellspacing="1" width="100%">
<tr>
<th width="25%">Constant</th>
<th width="15%">Value</th>
<th width="60%">Description</th>
</tr>
<tr>
<td valign="top"><tt>QUdpSocket.ShareAddress</tt></td>
<td align="center" valign="top"><tt>0x1</tt></td>
<td valign="top">Allow other services to bind to the same address and port. This is useful when multiple processes share the load of a single service by listening to the same address and port (e.g., a web server with several pre-forked listeners can greatly improve response time). However, because any service is allowed to rebind, this option is subject to certain security considerations. Note that by combining this option with ReuseAddressHint, you will also allow your service to rebind an existing shared address. On Unix, this is equivalent to the SO_REUSEADDR socket option. On Windows, this option is ignored.</td>
</tr>
<tr>
<td valign="top"><tt>QUdpSocket.DontShareAddress</tt></td>
<td align="center" valign="top"><tt>0x2</tt></td>
<td valign="top">Bind the address and port exclusively, so that no other services are allowed to rebind. By passing this option to <a href="qudpsocket.html#bind">QUdpSocket.bind</a>(), you are guaranteed that on successs, your service is the only one that listens to the address and port. No services are allowed to rebind, even if they pass ReuseAddressHint. This option provides more security than ShareAddress, but on certain operating systems, it requires you to run the server with administrator privileges. On Unix and Mac OS X, not sharing is the default behavior for binding an address and port, so this option is ignored. On Windows, this option uses the SO_EXCLUSIVEADDRUSE socket option.</td>
</tr>
<tr>
<td valign="top"><tt>QUdpSocket.ReuseAddressHint</tt></td>
<td align="center" valign="top"><tt>0x4</tt></td>
<td valign="top">Provides a hint to <a href="qudpsocket.html">QUdpSocket</a> that it should try to rebind the service even if the address and port are already bound by another socket. On Windows, this is equivalent to the SO_REUSEADDR socket option. On Unix, this option is ignored.</td>
</tr>
<tr>
<td valign="top"><tt>QUdpSocket.DefaultForPlatform</tt></td>
<td align="center" valign="top"><tt>0x0</tt></td>
<td valign="top">The default option for the current platform. On Unix and Mac OS X, this is equivalent to (DontShareAddress + ReuseAddressHint), and on Windows, its equivalent to ShareAddress.</td>
</tr>
</table>
<br />
<br />
<p>This enum was introduced in Qt 4.1.</p>
<p>The BindMode type is a typedef for <a href="qflags.html">QFlags</a>&lt;BindFlag&gt;. It stores an OR combination of BindFlag values.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QUdpSocket" />QUdpSocket.__init__ (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Creates a <a href="qudpsocket.html">QUdpSocket</a> object.</p>
<p><i>parent</i> is passed to the <a href="qobject.html">QObject</a> constructor.</p>
<p>See also <a href="qabstractsocket.html#socketType">socketType</a>().</p>
<h3 class="fn"><a name="bind" />bool QUdpSocket.bind (<i>self</i>, <a href="qhostaddress.html">QHostAddress</a>&#160;<i>address</i>, unsigned long&#160;<i>port</i>)</h3><p>Binds this socket to the address <i>address</i> and the port <i>port</i>. When bound, the signal <a href="qiodevice.html#readyRead">readyRead</a>() is emitted whenever a UDP datagram arrives on the specified address and port. This function is useful to write UDP servers.</p>
<p>On success, the functions returns true and the socket enters <a href="qabstractsocket.html#SocketState-enum">BoundState</a>; otherwise it returns false.</p>
<p>The socket is bound using the <a href="qudpsocket.html#BindFlag-enum">DefaultForPlatform</a> <a href="qudpsocket.html#BindFlag-enum">BindMode</a>.</p>
<p>See also <a href="qudpsocket.html#readDatagram">readDatagram</a>().</p>
<h3 class="fn"><a name="bind-2" />bool QUdpSocket.bind (<i>self</i>, unsigned long&#160;<i>port</i>&#160;=&#160;0)</h3><p>This is an overloaded member function, provided for convenience.</p>
<p>Binds to <i>address</i> on port <i>port</i>, using the <a href="qudpsocket.html#BindFlag-enum">BindMode</a> <i>mode</i>.</p>
<p>This function was introduced in Qt 4.1.</p>
<h3 class="fn"><a name="bind-3" />bool QUdpSocket.bind (<i>self</i>, <a href="qhostaddress.html">QHostAddress</a>&#160;<i>address</i>, unsigned long&#160;<i>port</i>, <a href="qudpsocket-bindmode.html">BindMode</a>&#160;<i>mode</i>)</h3><p>This is an overloaded member function, provided for convenience.</p>
<p>Binds to <a href="qhostaddress.html">QHostAddress</a>:Any on port <i>port</i>.</p>
<h3 class="fn"><a name="bind-4" />bool QUdpSocket.bind (<i>self</i>, unsigned long&#160;<i>port</i>, <a href="qudpsocket-bindmode.html">BindMode</a>&#160;<i>mode</i>)</h3><p>This is an overloaded member function, provided for convenience.</p>
<p>Binds to <a href="qhostaddress.html">QHostAddress</a>:Any on port <i>port</i>, using the <a href="qudpsocket.html#BindFlag-enum">BindMode</a> <i>mode</i>.</p>
<p>This function was introduced in Qt 4.1.</p>
<h3 class="fn"><a name="hasPendingDatagrams" />bool QUdpSocket.hasPendingDatagrams (<i>self</i>)</h3><p>Returns true if at least one datagram is waiting to be read; otherwise returns false.</p>
<p>See also <a href="qudpsocket.html#pendingDatagramSize">pendingDatagramSize</a>() and <a href="qudpsocket.html#readDatagram">readDatagram</a>().</p>
<h3 class="fn"><a name="pendingDatagramSize" />long QUdpSocket.pendingDatagramSize (<i>self</i>)</h3><p>Returns the size of the first pending UDP datagram. If there is no datagram available, this function returns -1.</p>
<p>See also <a href="qudpsocket.html#hasPendingDatagrams">hasPendingDatagrams</a>() and <a href="qudpsocket.html#readDatagram">readDatagram</a>().</p>
<h3 class="fn"><a name="readDatagram" />(object, <a href="qhostaddress.html">QHostAddress</a>&#160;<i>host</i>, unsigned long&#160;<i>port</i>) QUdpSocket.readDatagram (<i>self</i>, long&#160;<i>maxlen</i>)</h3><p>Receives a datagram no larger than <i>maxSize</i> bytes and stores it in <i>data</i>. The sender's host address and port is stored in *<i>address</i> and *<i>port</i> (unless the pointers are 0).</p>
<p>Returns the size of the datagram on success; otherwise returns -1.</p>
<p>If <i>maxSize</i> is too small, the rest of the datagram will be lost. To avoid loss of data, call <a href="qudpsocket.html#pendingDatagramSize">pendingDatagramSize</a>() to determine the size of the pending datagram before attempting to read it. If <i>maxSize</i> is 0, the datagram will be discarded.</p>
<p>See also <a href="qudpsocket.html#writeDatagram">writeDatagram</a>(), <a href="qudpsocket.html#hasPendingDatagrams">hasPendingDatagrams</a>(), and <a href="qudpsocket.html#pendingDatagramSize">pendingDatagramSize</a>().</p>
<h3 class="fn"><a name="writeDatagram" />long QUdpSocket.writeDatagram (<i>self</i>, str&#160;<i>data</i>, <a href="qhostaddress.html">QHostAddress</a>&#160;<i>host</i>, unsigned long&#160;<i>port</i>)</h3><p>Sends the datagram at <i>data</i> of size <i>size</i> to the host address <i>address</i> at port <i>port</i>. Returns the number of bytes sent on success; otherwise returns -1.</p>
<p>Datagrams are always written as one block. The maximum size of a datagram is highly platform-dependent, but can be as low as 8192 bytes. If the datagram is too large, this function will return -1 and <a href="qabstractsocket.html#error">error</a>() will return <a href="qabstractsocket.html#SocketError-enum">DatagramTooLargeError</a>.</p>
<p>Sending datagrams larger than 512 bytes is in general disadvised, as even if they are sent successfully, they are likely to be fragmented by the IP layer before arriving at their final destination.</p>
<p>See also <a href="qudpsocket.html#readDatagram">readDatagram</a>().</p>
<h3 class="fn"><a name="writeDatagram-2" />long QUdpSocket.writeDatagram (<i>self</i>, <a href="qbytearray.html">QByteArray</a>&#160;<i>datagram</i>, <a href="qhostaddress.html">QHostAddress</a>&#160;<i>host</i>, unsigned long&#160;<i>port</i>)</h3><p>This is an overloaded member function, provided for convenience.</p>
<p>Sends the datagram <i>datagram</i> to the host address <i>host</i> and at port <i>port</i>.</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td width="25%">PyQt&#160;4.0.1 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.trolltech.com">Trolltech&#160;AS</a> 2006</td><td align="right" width="25%">Qt&#160;4.1.4</td></tr></table></div></address></body></html>