i trying simple host/client transfer working. followed tutorial, , server goes without problems, when client tries connect returns "connection reset peer." honstly don't know error is.
class gensocket: def __init__(self, sock=none): if sock none: self.sock = socket.socket(socket.af_inet, socket.sock_stream) else: self.sock = sock def bind(self, ip, port): self.sock.bind((ip, port)) def listen(self, maxcon): self.sock.listen(maxcon) while true: self.sock.accept() def accept(self): self.sock.accept() while true: print (self.recieve()) def connect(self, host, port): self.sock.connect((host, port)) def send(self, msg, type): totalsent = 0 prefix = str(type) + str(len(msg)).rjust(5, '0') print prefix while totalsent < 6: sent = self.sock.send(prefix) if sent == 0: raise runtimeerror("connection broken!") print "sent prefix" totalsent = 0 while totalsent < len(msg): sent = self.sock.send(msg[totalsent:]) if sent == 0: raise runtimeerror("connection broken!") totalsent = + sent def recieve(self): bytes_recd = 0 while bytes_recd < 6: prefix = self.sock.recv(6) if prefix == '': raise runtimeerror("connection broken!") prefix.append(prefix) bytes_recd = + len(prefix) print "recieved prefix" chunks = [] msglen = prefix[1:] bytes_recd = 0 while bytes_recd < msglen: chunk = self.sock.recv(min(msglen - bytes_recd, 2048)) if chunk == '': raise runtimeerror("connection broken!") chunks.append(chunk) bytes_recd = + len(chunk) return ''.join(chunks)
socket.accept() return new socket object need use sending , receiving. have @ the docs.
e.g.:
def accept(self): conn, addr = self.sock.accept() while true: print (self.recieve(conn)) and change self.recieve() use new socket.
also, implementation not going accept() new client while it's serving first, can specify greater 0 maxcon value listen() call.
this make new client (up maxcon value) connect , wait in line waited served, implementation looks it's going quit when client stops connection (i didn't check carefully, though :) ) better give 0 backlog value listen().
Comments
Post a Comment