[PyQt] Having a problem with QTcpSocket

Hans-Peter Jansen hpj at urpla.net
Mon Nov 23 15:05:07 GMT 2009


On Monday 23 November 2009, 15:19:06 Nick Gaens wrote:
>  Hans-Peter Jansen schreef:
[...]
>  ----------------------------
>  def __init__(self):
>          self.server = QtNetwork.QTcpServer(self)
>          self.server.serverPort = 55555
>          self.server.newConnection.connect(self.clientConnecting)
>          self.server.listen() # defaults to QHostAddress.Any
>
>  def clientConnecting(self): # used by the "server"
>          if self.server.hasPendingConnections():
>              connectingClient = self.server.nextPendingConnection()
>              connectingClient.readyRead.connect(self.receiveData)
>
>  def connectToClient(self, ip):  # used by the "client"
>          socket = QtNetwork.QTcpSocket()
>          socket.readyRead.connect(self.receiveData)
>          socket.connectToHost(QtNetwork.QHostAddress(ip), 55555) # ip of
> server if socket.waitForConnected(5000):
>              print "Connected!"
>  ----------------------------
[...]
>
>  How odd that I receive this mail *the moment* my code started working
> :-P.
>
>  I found that the serverPort may not be overwritten.
>
>  I removed this assignment:
>
> self.server.serverPort = 55555

Assigning properties this way usually won't work in PyQt (although Phil 
tackles this already). 

try this:
          self.server.listen(QtNetwork.QHostAddress.Any, 55555)


>  and the clients now can connect to the server w/o any problem.. Downside
> is that the port is random..

Pete





More information about the PyQt mailing list