[PyQt] PyQt4 server trial

Mark Summerfield mark at qtrac.eu
Fri May 25 15:55:40 BST 2007


On Fri 25-May-07, Uğur Çetin wrote:
> Hi,
>
> I tried to make a simple PyQt4 server that should work without a gui.
> But I think I'm missing some points in this, because the codes I wrote
> simply doesn't work(any connection is refused). Can you tell me where
> the problem is?

I had the same problem using QHostAddress.Any.

Try changing both occurrences of

    QtNetwork.QHostAddress.Any

to

    QtNetwork.QHostAddress("0.0.0.0")

which according to the docs means the same thing, but at least for me,
works.

Also, you can check the return value of listen().

>
> content of servertry.py
> -----------------------------
> from PyQt4 import QtCore, QtNetwork
> import sys
>
>
> class MyServer(QtNetwork.QTcpServer):
>     def __init__(self, parent):
>         QtNetwork.QTcpServer.__init__(self, parent)
>         self.listen(QtNetwork.QHostAddress(QtNetwork.QHostAddress.Any),
> 23105) def incomingConnection(self):
>         print "A connection has come!"
>
> app = QtCore.QCoreApplication(sys.argv)
> server = MyServer(QtCore.QObject())
> sys.exit(app.exec_())
>
> content of clienttry.py
> ----------------------------
> import socket
> from PyQt4 import QtNetwork
>
> try:
>     mySocket = socket.socket()
>     mySocket.connect(("", 23105))
> except:
>     print "python socket didn't work, trying PyQt4 socket"
>     mySocket = QtNetwork.QTcpSocket()
>    
> mySocket.connectToHost(QtNetwork.QHostAddress(QtNetwork.QHostAddress.LocalH
>ost), 23105)
>     print "waiting for connection..."
>     mySocket.waitForConnected()
>     print "connection state is: %d" % mySocket.state()



-- 
Mark Summerfield, Qtrac Ltd., www.qtrac.eu




More information about the PyQt mailing list