socket.error: [Errno 107] Transport endpoint is not connected
when calling
msg = self.socket.recv(self.buffer)
My client receives the error:
socket.error: [Errno 104] Connection reset by peer
when calling
msg = self.socket.recv(self.buffer)
I was working on the server and client over the weekend and sending
and receiving worked fine, I wanted to debug a few things and I get
this when I try to run it (no changes made from what I had on the
weekend)
My car has this problem. It makes a noise when it rolls along the road.
I've brought the wheel for you to take a look at, can you fix it,
please? ;-)
A little more context might be helpful - at least the error traceback
and the code that makes the socket calls. I presume you are using TCP
judging by the client error message. This implies the server isn't
listening. Have you correctly bound the socket to an IP address and port
before issuing an accept() call?
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
Then if you have any connection problem, that's because there is a
connection problem. Not because you mess up with your net code.
JM
here's the server:
class commServer:
"""Class to hold a tcp server and interact with with it
allows for a wrapper around socket class to keep code clean"""
def __init__ (self, host, hostid, port, buff =1024):
self.host = host
self.hostid = hostid #id of the server
self.port = port
self.buffer = buff
self.socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.conn = None
self.addr = None
def bindServ(self):
"""Connect to the server specified by self.host, self.port"""
self.socket.bind((self.host, self.port))
def closeConn(self):
"""Disconnect from the server connected to"""
self.conn.close()
def listen(self):
self.socket.listen(1)
def accept(self):
self.conn, self.addr = self.socket.accept()
#lets you send a string msg to the server
def sendMSG(self, msg):
self.conn.send(msg)
#lets you receive data from the server
def recvMSG(self):
msg = self.socket.recv(self.buffer)
if msg == "": #if the client disconnected let's not throw
return False
else:
return msg
class Negotiator:
"""Negotiator for the server handles all communication with the
client to
verify the server and prepare the file the client wants for
download"""
def __init__(self, host, hostid, port, rsa_key):
self.server = commServer(host,hostid,port)
def Negotiate(self):
self.server.bindServ()
self.server.listen()
self.server.accept()
#Plan on being asked for server confirmation
clmsg = self.server.recvMSG() # it fails right here on the
server
calling the Server Negotiator as:
host = "127.0.0.1"
port = 8005
HOSTID = a string
key = an RSA key
servernegotiator = Negotiator(host,HostID, port, key)
if servernegotiator.Negotiate() == False:
print "something went wrong"
print "Done"
for the client it is:
class commClient:
"""Class to hold a tcp client and interact with with it
allows for a wrapper around socket class to keep code clean"""
def __init__ (self, host, hostid, port, buff =1024):
self.host = host
self.hostid = hostid #id of the server
self.port = port
self.buffer = buff
self.socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
def connectServ(self):
"""Connect to the server specified by self.host, self.port"""
self.socket.connect((self.host, self.port))
def disconnServ(self):
"""Disconnect from the server connected to"""
self.socket.close()
#lets you send a string msg to the server
def sendMSG(self, msg):
self.socket.send(msg)
#lets you receive data from the server
def recvMSG(self):
msg = self.socket.recv(self.buffer)
if msg == "": #if the server disconnected let's not throw
something later
return False
else:
return msg
class Negotiator:
"""The Negotiator handles all communications and message handling
necessary for verifying the server, and that the file is available
to
download"""
def __init__(self, host, hostid, port, rsa_key):
"""client should be a commClient object that has not been
connected
to the server."""
self.client = commClient(host, hostid, port)
self.clientKey = rsa_key
self.serverKey = None
self.CScipher = None #AES cipher for client -> server
self.SCcipher = None #AES cipher for server -> client
self.CShalves = None #tuple for random halves by client
self.SChalves = None #tuple for random halves by server
self.file = None
def Negotiate(self, fname):
"""Contact the server, verify the server,
negotiates for a file to be downloaded by the client. It
returns
the file name to be downloaded, and the cipher to decrypt
it."""
self.client.connectServ()
print "connected"
#tell the server you want to connect
clmsg = message(CONN, (self.client.getHost(),
self.client.getHostID())) #message acts
as a wrapper around a message type and the data for the type
self.client.sendMSG(clmsg.getSendable()) # here is were it
fails
the Negotiator is called as:
host = "127.0.0.1"
port = 8005
HOSTID is the same string as before
key is an RSA key
clientnegotiator = Negotiator(host, HostID, port, key)
filename = clientnegotiator.Negotiate("hostid")
the stack traces are:
Server side:
Traceback (most recent call last):
File "Server.py", line 17, in <module>
if servernegotiator.Negotiate() == False:
File "/home/twistedphrame/Desktop/communication/
ServerNegotiator.py", line 184, in Negotiate
clmsg = self.server.recvMSG()
File "/home/twistedphrame/Desktop/communication/
ServerNegotiator.py", line 67, in recvMSG
msg = self.socket.recv(self.buffer)
socket.error: [Errno 107] Transport endpoint is not connected
Client Side:
File "Client.py", line 17, in <module>
filename = clientnegotiator.Negotiate("hostid")
File "/home/twistedphrame/Desktop/communication/
ClientNegotiator.py", line 209, in Negotiate
srvmsg = self.client.recvMSG()
File "/home/twistedphrame/Desktop/communication/
ClientNegotiator.py", line 55, in recvMSG
msg = self.socket.recv(self.buffer)
each time a handler is spawned is it client specific? in other words
when two clients send something to the server do handlers spawn for
each of them or does everything just go into a single handler?
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
If you want data persistant over connections, store them in your handler
class.
class MyTCPHandler(SocketServer.BaseRequestHandler):
cnxNumber = 0
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
# just send back the same data, but upper-cased
self.request.send(self.data.upper())
cnxNumber +=1
print "handling connection N�%s" % cnxNumber
JM
at this point i switched over to this scheme and now I'm getting an
error durring instantiation of the server:
Server.py:
from Crypto.PublicKey import RSA
from ServerNegotiator import ServerNegotiator
from sharedComs import *
f = open("hostid")
tup = stringToTuple(f.readline()[0:-1])
HostID = f.readline()[0:-1]
f.close()
key = RSA.construct((long(tup[0]),long(tup[1]), long(tup[2]),
long(tup[3]),
long(tup[4]),long(tup[5])))
host = "localhost"
port = 8005
servernegotiator = ServerNegotiator(host,HostID, port, key)
servernegotiator.start()
ServerNegotiatior.py lines 185 - end
class ServerNegotiator:
def __init__(self, host, port, hostid, rsa_key, buf = 512):
negotiator = Negotiator(host, hostid, rsa_key,buf)
self.server = SocketServer.TCPServer((host, port), negotiator)
def start(self):
self.server.serve_forever()
Traceback (most recent call last):
File "Server.py", line 16, in <module>
servernegotiator = ServerNegotiator(host,HostID, port, key)
File "/home/twistedphrame/Desktop/communication/
ServerNegotiator.py", line 188, in __init__
self.server = SocketServer.TCPServer((host, port), negotiator)
File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
self.server_bind()
File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
TypeError: an integer is required
Jordan Apgar wrote:
>
> servernegotiator = ServerNegotiator(host,HostID, port, key)
>
you swapped port & hostID in your call
JM
tThanks guys it's working now... feel a little stupid though.
Cool!