- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 socket 模块在 python 中创建一个服务器/客户端应用程序,但无论出于何种原因,我的服务器一直终止连接。奇怪的是,这在 Windows 中可以完美运行,但在 Linux 中却不行。我到处寻找可能的解决方案,但没有一个有效。以下是利用该错误的代码的净化版本,但成功率更高。通常它永远不会起作用。希望这仍然是足够的信息。谢谢!
服务器:
import logging
import socket
import threading
import time
def getData():
HOST = "localhost"
PORT = 5454
while True:
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) #because linux doesn't like reusing addresses by default
s.bind( ( HOST, PORT ) )
logging.debug( "Server listens" )
s.listen( 5 )
conn, addr = s.accept()
logging.debug( "Client connects" )
print "Connected by,", addr
dataRequest = conn.recv( 1024 )
logging.debug( "Server received message" )
time.sleep( .01 ) #usually won't have to sample this fast
data = """Here is some data that is approximately the length
of the data that I am sending in my real server. It is a string that
doesn't contain any unordinary characters except for maybe a tab."""
if not timeThread.isAlive(): #lets client know test is over
data = "\t".join( [ data, "Terminate" ] )
conn.send( data )
s.close()
print "Finished"
print "Press Ctrl-C to quit"
break
else:
logging.debug( "Server sends data back to client" )
conn.send( data )
logging.debug( "Server closes socket" )
s.close()
def timer( t ):
start = time.time()
while ( time.time() - start ) < t:
time.sleep( .4 )
#sets flag for another thread not here
def main():
global timeThread
logging.basicConfig( filename="test.log", level=logging.DEBUG )
#time script runs for
t = 10 #usually much longer (hours)
timeThread = threading.Thread( target=timer, args=( t, ) )
dataThread = threading.Thread( target=getData, args=() )
timeThread.start()
dataThread.start()
#just for testing so I can quit threads when sockets break
while True:
time.sleep( .1 )
timeThread.join()
dataThread.join()
if __name__ == "__main__":
main()
客户:
import logging
import socket
def getData():
dataList = []
termStr = "Terminate"
data = sendDataRequest()
while termStr not in data:
dataList.append( data )
data = sendDataRequest()
dataList.append( data[ :-len( termStr )-1 ] )
def sendDataRequest():
HOST = "localhost"
PORT = 5454
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
while True:
try:
s.connect( ( HOST, PORT ) )
break
except socket.error:
print "Connecting to server..."
logging.debug( "Client sending message" )
s.send( "Hey buddy, I need some data" ) #approximate length
try:
logging.debug( "Client starts reading from socket" )
data = s.recv( 1024 )
logging.debug( "Client done reading" )
except socket.error, e:
logging.debug( "Client throws error: %s", e )
print data
logging.debug( "Client closes socket" )
s.close()
return data
def main():
logging.basicConfig( filename="test.log", level=logging.DEBUG )
getData()
if __name__ == "__main__":
main()
编辑:添加回溯
Traceback (most recent call last):
File "client.py", line 39, in <moduel>
main()
File "client.py", line 36, in main
getData()
File "client.py", line 10, in getData
data = sendDataRequest()
File "client.py", line 28, in sendDataRequest
data = s.recv( 1024 )
socket.error: [Errno 104] Connection reset by peer
编辑:添加调试
DEBUG:root:Server listens
DEBUG:root:Client sending message
DEBUG:root:Client connects
DEBUG:root:Client starts reading from socket
DEBUG:root:Server received message
DEBUG:root:Server sends data back to client
DEBUG:root:Server closes socket
DEBUG:root:Client done reading
DEBUG:root:Server listens
DEBUG:root:Client sending message
DEBUG:root:Client connects
DEBUG:root:Client starts reading from socket
DEBUG:root:Server received message
DEBUG:root:Server sends data back to client
DEBUG:root:Client done reading
DEBUG:root:Client sending message
DEBUG:root:Client starts reading from socket
DEBUG:root:Server closes socket
DEBUG:root:Client throws error: [Errno 104] Connection reset by peer
DEBUG:root:Server listens
Tom 的理论似乎是正确的。我会想办法更好地关闭连接。
这没有解决,但接受的答案似乎指出了问题。
编辑:我尝试使用 Tom 的 getData() 函数,看起来服务器仍然过早地关闭了连接。应该是可重复的,因为我也无法在 Windows 中使用它。
服务器输出/回溯:
Connected by, ('127.0.0.1', 51953)
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.6/threading.py", line 484, in run
self.__target(*self.__args, **self.__kwargs)
File "server.py", line 15, in getData
s.bind( ( HOST, PORT ) )
File "<string>", line 1, in bind
error: [Errno 22] Invalid argument
客户端输出/回溯:
Here is some data that is approximately the length
of the data that I am sending in my real server. It is a string that
doesn't contain any unordinary characters except for maybe a tab.
Traceback (most recent call last):
File "client.py", line 49, in <moduel>
main()
File "client.py", line 46, in main
getData()
File "client.py", line 11, in getData
data = sendDataRequest()
File "client.py", line 37, in sendDataRequest
print data
UnboundLocalError: local variable 'data' referenced before assignment
日志:
DEBUG:root:Server listens
DEBUG:root:Client sending message
DEBUG:root:Client connects
DEBUG:root:Client starts reading from socket
DEBUG:root:Server received message
DEBUG:root:Server sends data back to client
DEBUG:root:Server closes connection
DEBUG:root:Client done reading
DEBUG:root:Client closes socket
DEBUG:root:Client sending message
DEBUG:root:Client starts reading from socket
DEBUG:root:Client throws error: [Errno 104] Connection reset by peer
更新:我使用了 Tom 的 getData()
函数,但将 s.bind()
移到了循环之前并让它开始工作。老实说,我不知道为什么会这样,所以如果有人能解释为什么服务器关闭它的客户端套接字是安全的,而不是当它关闭它的服务器套接字时,那就太好了。谢谢!
最佳答案
虽然我无法重现此问题(在 Windows 7 64 位、Python 2.7 上),但我最好的猜测是发生了以下情况:
您从客户端添加的堆栈跟踪似乎支持这一理论。是否可以通过一些额外的日志记录或类似的方式来证明情况并非如此?
其他注意事项:如果您的客户端在收到的第一个数据中没有找到终止字符串,它会打开一个新的服务器套接字。这在我看来是错误的 - 你应该从同一个套接字读取数据,直到你拥有所有数据。
编辑:结合更多的东西:
在您的示例日志输出中,您没有更新代码,所以我看不到每个日志行的来源。但是,看起来您有 2 个并行运行的客户端(可能在不同的进程或线程中?),这看起来很可疑,这会导致:
我刚刚注意到最后一件事。在此处的示例中 https://docs.python.org/2/library/socket.html#example服务器不会关闭套接字,它会关闭通过监听套接字生成的连接。可能是您有 2 个客户端连接到同一个服务器套接字实例,当您关闭服务器套接字时,您实际上是在断开两个连接的客户端,而不仅仅是第一个。如果您正在运行多个客户端,则记录某种身份,例如。 DEBUG:root:Client(6) 完成阅读
可能有助于证明这一点。
您能否为服务器的数据线程主循环尝试以下操作,将显示问题是否与关闭监听套接字而不是连接套接字有关:
def getData():
HOST = "localhost"
PORT = 5454
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
# s.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) #because linux doesn't like reusing addresses by default
s.bind( ( HOST, PORT ) )
logging.debug( "Server listens" )
s.listen( 5 )
while True:
conn, addr = s.accept()
logging.debug( "Client connects" )
print "Connected by,", addr
dataRequest = conn.recv( 1024 )
logging.debug( "Server received message" )
time.sleep( .01 ) #usually won't have to sample this fast
data = """Here is some data that is approximately the length
of the data that I am sending in my real server. It is a string that
doesn't contain any unordinary characters except for maybe a tab."""
if not timeThread.isAlive(): #lets client know test is over
data = "\t".join( [ data, "Terminate" ] )
conn.send( data )
conn.close()
print "Finished"
print "Press Ctrl-C to quit"
break
else:
logging.debug( "Server sends data back to client" )
conn.send( data )
logging.debug( "Server closes connection" )
conn.close()
关于python - 另一个 'Connection reset by peer' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24874894/
来历及功能 peer.exe进程程序文件是由北京光芒时代国际传媒网络技术有限公司为其在飞速网发布的PC客户端:RaySource程序的一个组成部分。被描述为“Grid Service”,其功能主要
希望你们一切都好 我是 webRTC 的初学者,如果我的问题感觉像菜鸟,我很抱歉,但我想知道是否有任何正确的方法来关闭对等点之间的连接,尤其是使用 simple-peer.js,期待您的精彩回复 这是
我在使用 Windows 8 商店应用程序时遇到了一些问题。我的问题很简单: 我在一台电脑上有一个 Windows 8 商店应用程序,在另一台电脑上有相同的应用程序。个人电脑不在同一个网络中,但有互联
我正在从事一个尝试设置 PayPal 点对点支付的项目。然而,自适应支付 API 似乎是当前 PayPal API 的一个非常旧的版本,最近没有更新。 我是否遗漏了什么,或者 API 的这一部分是否仍
我的应用程序使用 GKSession 和 GKSessionModePeer。它必须处理任意连接和断开连接的点,因为这是一个长时间运行的应用程序,用户应该能够进入后台并稍后返回。这在大多数情况下都很好
我在 Google Meet 上开会,看到你可以打开实时字幕。他们实际上有一个演示 here关于如何进行实时语音到文本,所以这一点不会让我感到困惑。 我也一直想尝试使用 WebRTC(我相信 Goog
我正在尝试在聊天模块中共享图像/视频。我已经提到了Sample代码,但找不到任何帮助。 我已经提到过http://quickblox.com/modules/chat/它说通过插入我们全功能的聊天模块
我想让 WiFi peer-2-peer 中的一组连接过程对用户来说是无缝的。有办法吗? 蓝牙是怎么做到的? 最佳答案 基本上有两种方法可以做到这一点,我在 WIFI DIRECT, CONNECTI
这是我原来问题的后续:Qt Server+Client App: encryption fails (updated with code)还将此发布到 Qt 论坛:http://qt-project.
我有两个位于同一子网上的 debian 服务器。它们通过开关连接。我知道 UDP 不可靠。 Question 1: I assume the link layer is ethernet. And M
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
我正在创建一个应用程序 (C#),它将通过网络发送一些消息。传出消息将由私钥签名,传入消息将使用私钥解密。 如果有人窃取私钥,我希望能够撤销它(向所有其他客户端发送撤销消息)。由于我是被盗私钥的所有者
我仍在学习 SIP 及其所有协议(protocol),特别是尝试将 PJSIP 集成到 iPhone 应用程序中以进行 p2p 调用。我对使用 PJSUA 的对等 2 对等连接有疑问。我能够通过使用
我想用 Webrtc 创建应用程序,它可以在网站、移动 ios 应用程序和 android 应用程序上运行。 我找到了这个链接:- http://xsockets.net/api/net-c/cust
我使用 Spark Streaming 从 Twitter 接收推文。我收到很多警告说: replicated to only 0 peer(s) instead of 1 peers 这个警告有什么
我有一个需要大量数据的模拟程序。我将数据加载到 GPU 中进行计算,数据中存在很多依赖性。由于 1 个 GPU 不足以处理数据,所以我将其升级为 2 个 GPU。但限制是,如果我需要其他 GPU 上的
我在调试我的代码时遇到问题,因为我无法理解引发的套接字错误。这是回溯。 Traceback (most recent call last): File "clickpression.py", lin
我试图将Fabric配置为可以在Kubernetes集群中使用,并且在一切正常的同时,我很难将链码(使用composer-cli)部署到网络上。看来,chaincode容器看不到创建它们的对等方。 2
在Java中,您可以使用Socket和ServerSocket来获取两个进程之间通信的双向 channel 。但其中只有一个必须拥有 ServerSocket 并被视为“服务器”,另一个则被视为“客户
我用不同的名称和不同的字段名称创建了自己的用户表,我想让 kamailio 将它用作对等用户的“acc”表,我该怎么做? 最佳答案 kamailio 中的 acc 表用于会计记录(即账单数据)。 我知
我是一名优秀的程序员,十分优秀!