发布代码。 没有它我们只能在我们的魔法水晶球从服务中恢复时提供帮助。 == irmen 发布代码。 ok。 这里是代码: #Echo服务器程序 导入套接字 HOST =''''#符号名称表示本地主机 PORT = 50007#任意非特权端口 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.setsockopt(socket .SOL_SOCKET,socket.SO_REUSEADDR,1) s.bind((HOST,PORT)) s.listen(1) conn ,addr = s.accept() print''Connected by'',add r $> conn.settimeout(1) toread = 99 retrytime = 0 reads = 0 读取和重试时间< 10: 试试: data = conn.recv(min(32,toread-reads)) 如果不是数据:continue 打印数据 读取+ = len(数据) 除外: retrytime + = 1 print" timeout%d" %retrytime 继续 如果读取== toread: conn.send(" OK") 否则: conn.send(不行) conn.close() ***** ***********我是单独的 行************************ ********************* #Echo客户端程序 import socket HOST =''localhost''#远程主机 PORT = 50007#与服务器使用的端口相同 s = socket .socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((HOST,PORT)) for i in range(12): print" time%d" %i s.send(''0123456789'') #data = s.recv(1024) #print" data %d" %i,数据 #s.shutdown(socket.SHUT_WR)#no more more data = s.recv(1024) s .close() print''收到'',repr(数据) 客户端应该得到响应,要么OK,要么OK。或者不行。 但事实是,客户端获得由对等方重置连接。 (如下所示)大约五分之一。 ------------------------ ---------- 回溯(最近一次调用最后一次): 文件" c.py",第10行,< module> s.send(''0123456789'') socket.error:(104,''由同行重置连接') ---------------------------------- 无论如何,服务器是一直做得很好。 对设计或实施的任何评论都会非常感谢 赞赏。 - ahlongxp 中国东北大学软件学院 啊****** @ gmail.com http ://www.herofit.cn Hi, everyone,I''m implementing a simple client/server protocol.Now I''ve got a situation:client will send server command,header paires and optionally body.server checks headers and decides whether to accept(read) the body.if server decided to throw(dump) the request''s body, it''ll send back aresponse message, such as "resource already exists" and close theconnection.the problem is, client will never get the response but a "peer reset"exception.any comments or help will be appreciated.--ahlongxpSoftware College,Northeastern University,China ah******@gmail.com http://www.herofit.cn 解决方案 me again."Connection reset by peer" happens about one in fifth.I''m using python 2.5.1 and ubuntu 7.04.--ahlongxpSoftware College,Northeastern University,Chinaahlon...@gmail.comhttp://www.herofit.cnahlongxp wrote:me again."Connection reset by peer" happens about one in fifth.I''m using python 2.5.1 and ubuntu 7.04. -- ahlongxpSoftware College,Northeastern University,Chinaahlon...@gmail.comhttp://www.herofit.cnPost the code.Without it we can only help when our magic crystal balls are back from service.==irmenPost the code.ok.here is the code:# Echo server programimport socketHOST = '''' # Symbolic name meaning the local hostPORT = 50007 # Arbitrary non-privileged ports = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)s.bind((HOST, PORT))s.listen(1)conn, addr = s.accept()print ''Connected by'', addrconn.settimeout(1)toread = 99retrytime = 0reads = 0while reads < toread and retrytime < 10:try:data = conn.recv(min(32,toread-reads))if not data: continueprint datareads += len(data)except:retrytime += 1print "timeout %d" % retrytimecontinueif reads == toread:conn.send("OK")else:conn.send("NOT OK")conn.close()****************I''m the separateline*********************************************# Echo client programimport socketHOST = ''localhost'' # The remote hostPORT = 50007 # The same port as used by the servers = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((HOST, PORT))for i in range(12):print "time %d" % is.send(''0123456789'')#data = s.recv(1024)#print "data %d" %i, data#s.shutdown(socket.SHUT_WR)#no more writedata=s.recv(1024)s.close()print ''Received'', repr(data)client is supposed to get the response, either "OK" or "NOT OK".but the fact is, client gets "Connection reset by peer" (as shownbelow) about one in fifth.----------------------------------Traceback (most recent call last):File "c.py", line 10, in <module>s.send(''0123456789'')socket.error: (104, ''Connection reset by peer'')----------------------------------anyway, server is doing well all the time.any comments on the design or implementation will be greatlyappreciated.--ahlongxpSoftware College,Northeastern University,China ah******@gmail.com http://www.herofit.cn 这篇关于socket:在客户端获得响应之前由服务器重置连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 19:39
查看更多