简体   繁体   English

从硬件(GPRS模块)发送时由对等体重置连接,但从PC发送时则很好

[英]Connection reset by peer when sending from hardware (GPRS module) but fine when send from PC

tl;dr TL;博士

  • Sending the same data from PC and Quectel GPRS module 从PC和Quectel GPRS模块发送相同的数据
  • When send from Quectel, server raises an exception Connection reset by peer 从Quectel发送时,服务器引发异常Connection reset by peer
  • But Quectel works in production environment which has the same EC2-micro instance and a load balancer. 但是Quectel在具有相同EC2-micro实例和负载均衡器的生产环境中工作。
  • Except for Quectel, another GPRS module - Neoway M680 - works with this EC2 instance. 除了Quectel之外,另一个GPRS模块--Neoway M680 - 适用于这个EC2实例。

Setup 设定

Local - Setup 本地 - 设置
I have a Quectel M66 , a GPRS module that I'm using to connect to the server ( AWS EC2 ) and transfer some data. 我有一个Quectel M66 ,一个GPRS模块,我用它连接到服务器( AWS EC2 )并传输一些数据。
I also have a python script I've made to connect and send the same data using a PC. 我还有一个python script我用连接并使用PC发送相同的数据。 Here below is the python script 下面是python脚本

import socket
import sys
from io import open
from time import sleep

'''
Python script to send data to remote server
'''
#replace address with the remote server address
HOST, PORT = '127.0.0.1', 3000 

if len(sys.argv) < 2:
    error = "Error:"
    print("{} {}".format(error, "Pass in the file to be send"))
    exit(1)

filename = sys.argv[1]

with open(filename, "r", newline="\r") as f:
    lines = f.readlines()

data = "".join(lines)

# Create a socket (SOCK_STREAM means a TCP socket)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data, "utf-8"))

    # Receive data from the server and shut down
    received = r"{}".format(sock.recv(1024))
    sock.close()

print("Received: {}".format(received))

Remote - Setup 远程 - 设置
I'm running an EC2-micro instance that's running a python script that just listens to a port and prints the data it received, also send a response which is hard-coded. 我正在运行一个运行python脚本的EC2-micro实例,它只是监听一个端口并打印它收到的数据,同时发送一个硬编码的响应。 Here is the script 这是脚本

#!/usr/bin/env python3

'''
Python code running on EC2-micro
'''

import socket
import errno
from datetime import datetime

print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

HOST = '0.0.0.0'  # Standard loopback interface address (localhost)
PORT = 3000       # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            try:
                data = r"{}".format(conn.recv(1024))
                print("Received: {}".format(data))
                #respond back with $E0A0
                conn.sendall(bytes("$E0A0:8B\r$E0FF\r", "utf-8"))
                conn.close()
                s.close()
                break
            except socket.error as e:
                if e.errno != errno.ECONNRESET:
                    raise
                if e.errno == errno.ECONNRESET:
                    printf("Connection reset by peer error again")
                    raise #it is failing here
                pass

Testing 测试

Quectel GPRS Module Quectel GPRS模块

When I try sending data using the Quectel module the using the AT Commands , what I see from the hardware (quectel) side is that the connection has been CLOSED 当我尝试使用Quectel模块使用AT Commands发送数据时,我从硬件(quectel)方面看到的是连接已经CLOSED

17:29:05.652 [Tx] AT+QIOPEN="TCP","127.0.0.1",3000\r
17:29:05.672 [Rx] \r\nOK\r\n\r\nCONNECT OK\r\n
17:29:07.699 [Tx] AT+SEND=1\r
17:29:07.718 [Rx] >
17:29:08.603 [Tx] A
17:29:08.647 [Rx] \r\nSEND OK\r\n
17:29:09.446 [Rx] CLOSED

And the code running on EC2 crashes with: 并且在EC2上运行的代码崩溃:

Connection reset by peer error again
Traceback (most recent call last):
  File "./server.py", line 22, in <module>
    data = r"{}".format(conn.recv(1024))
ConnectionResetError: [Errno 104] Connection reset by peer

But this doesn't happen when I test using the python script (the first code given above) 但是当我使用python脚本测试时,这不会发生 (上面给出的第一个代码)

Running python script from PC 从PC运行python脚本

$ python client.py data
Received: b'$E0A0:8B\r$E0FF\r'

Some more observations: 更多观察:

  • This hardware (quectel) is being used in production environment and it works there as expected. 这个硬件(quectel)正在生产环境中使用,它按预期在那里工作。 Only in this new independent instance (with no load balancing ) it fails with Connection reset by peer 只有在这个新的独立实例中(没有load balancing ),它才会失败,同时Connection reset by peer
  • I learned that this could be because of (from here ) 我了解到这可能是因为(从这里
    • Resource limitation at server side 服务器端的资源限制
    • High traffic 交通拥挤
  • But this new instance doesn't do anything else. 但是这个新实例没有做任何其他事情。 I have also checked CloudWatch and no spike in CPU usage was seen . 我还检查了CloudWatch没有看到CPU使用率的高峰

Question

  1. Do you think this is an issue at the server side? 您认为这是服务器端的问题吗?
    • Since the Quectel module responded with SEND OK , from the documentation we can be sure that the data has left the module. 由于Quectel模块以SEND OK响应,因此从文档中我们可以确定数据已离开模块。 And TCP promises delivery of data TCP承诺提供数据
    • But clearly, before data could be received/read we get Connection reset by peer 但显然,在接收/读取数据之前,我们会Connection reset by peer
  2. Is it a performance/infrastructure issue? 这是性能/基础设施问题吗?
    • All I could find which sounded reasonable was that the server might be less on memory or other resources 我能找到的听起来合理的是服务器内存或其他资源可能较少

This was a backend issue. 这是一个后端问题。 A port white-listing issue. 端口白名单问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过带有Python的请求模块的对等方重置连接。 卷毛和邮递员工作正常。 - Connection reset by peer w/ Python's Request Module. Curl and Postman work fine. 在Python中使用Twitch IRC时“通过对等方重置连接” - “Connection reset by peer” when using Twitch IRC in Python 捕获VM映像时,Softlayer获得“对等连接重置”错误 - Softlayer Got “Connection reset by peer” error when capturing a VM image 当套接字侦听积压较小时,Python 中的对等方重置连接 - Connection reset by peer in Python when socket listen backlog is small 在其他计算机上测试Web服务器时,对等方重置连接 - Connection reset by peer when testing web server on different computers 击中 docker 时:(56)接收失败:对等方重置连接 - When hitting docker: (56) Recv failure: Connection reset by peer Peer使用Python模块urllib重置连接 - Connection Reset by Peer with Python module urllib 来自文档的Python SSL示例给出“对等重置连接”错误 - Python SSL example from docs gives “Connection reset by peer” error Python-请求模块-接收流更新-对等重置连接 - Python - Requests module - Receving streaming updates - Connection reset by peer 由Peer pymongo重置连接 - Connection reset by Peer pymongo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM