<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Nick Gaens schreef:
<blockquote cite="mid:4B096318.5060801@gmail.com" type="cite">
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
<title></title>
Phil Thompson schreef:
<blockquote cite="mid:251a2005392be6028a13e734dbadd047@localhost"
type="cite">
<pre wrap="">On Sat, 21 Nov 2009 17:19:00 +0100, Nick Gaens <a
moz-do-not-send="true" class="moz-txt-link-rfc2396E"
href="mailto:nickgaens@gmail.com"><nickgaens@gmail.com></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Hello all,
I'm trying to get two clients to speak to each other, using an instance
</pre>
</blockquote>
<pre wrap=""><!---->of
</pre>
<blockquote type="cite">
<pre wrap="">QtNetwork.QTcpSocket for each client.
The clients are on different computers with IP's: 192.168.1.100 and .102
On the first client, I run the code below:
------------------
def __init__(self):
self.socket = QtNetwork.QTcpSocket(self)
self.socket.setLocalAddress(QtNetwork.QHostAddress("192.168.1.100"))
self.socket.setLocalPort(55555)
self.socket.readyRead.connect(self.receiveData)
def connectToOtherClient(self, ip):
self.socket.connectToHost(QtNetwork.QHostAddress(ip), 55555)
if self.socket.waitForConnected(10000):
print "Connected!"
else:
print self.socket.state() # Prints '0' (zero), meaning
UnconnectedState
------------------
So I create a QTcpSocket, I tell it the IP I want to use and after having
the user asking for the remote IP address, connectToOtherClient() is
invoked.
On the second client, I just create an instance of this class, using
"192.168.1.102" as IP there in __init__(). No invocation of
connectToOtherClient() there, ofcourse..
The problem is: it doesn't connect at all.. The socket state remains
"UnconnectedState"..
What am I doing wrong here?
</pre>
</blockquote>
<pre wrap=""><!---->
Clients talk to servers, not other clients. Have a look at QTcpServer.
Phil
</pre>
</blockquote>
I've now instantiated a QTcpServer, which "creates" a socket for an
incoming connection (QTcpServer.nextPendingConnection(), that is).<br>
Both clients have such a server instance, since they both should be
able to connect to each other. But still, the
QTcpSocket.connectToHost() invocation doesn't do a thing.<br>
I'm starting to think this is some bug / error in PyQt4 itself instead
of in my code. The C++-Qt4 Fortune Server + Client example runs
flawlessly between the two machines, so there are no network blocks or
something like that. Also, the C++-code has the same logical flow as my
code below.<br>
<br>
----------------------------<br>
def __init__(self):<br>
self.server = QtNetwork.QTcpServer(self)<br>
self.server.serverPort = 55555<br>
self.server.newConnection.connect(self.clientConnecting)<br>
self.server.listen() # defaults to QHostAddress.Any<br>
<br>
def clientConnecting(self): # used by the "server"<br>
if self.server.hasPendingConnections():<br>
connectingClient = self.server.nextPendingConnection()<br>
connectingClient.readyRead.connect(self.receiveData)<br>
<br>
def connectToClient(self, ip): # used by the "client"<br>
socket = QtNetwork.QTcpSocket()<br>
socket.readyRead.connect(self.receiveData)<br>
socket.connectToHost(QtNetwork.QHostAddress(ip), 55555) # ip of
server<br>
if socket.waitForConnected(5000):<br>
print "Connected!"<br>
----------------------------<br>
<br>
I *really* do not understand why this is *not* working..<br>
<br>
Nick<br>
</blockquote>
Anyone? :-( I've tried various things, but none of them helped.. I've
found out that both NIC's actually send / receive some bytes (proven by
the suddenly increasing number of sent / received bytes on both
computers), but it doesn't actually connect them..<br>
<br>
Nick<br>
</body>
</html>