持续监听 TCP 端口 [英] Continuous listening to TCP port

查看:55
本文介绍了持续监听 TCP 端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个代码,它能够通过 TCP 协议从端口接收数据.我每 15 分钟从 ESP8266 接收一次数据,然后 ESP 进入 deepSleep 模式.如何更改它以使其连续工作?我想在 while 循环中创建一个新连接,但它不起作用.

I've made a code which is able to receive data from the port over TCP protocol. I receive data from ESP8266 every 15 minutes, and then ESP goes to a deepSleep mode. How to change it to make it work continuosly? I wanted to create a new connection in while loop, but it doesn't work.

我的代码

import sys
import socket

TCP_IP = '192.168.42.1'
TCP_PORT = 8888
BUFFER_SIZE = 1024
param = []
i=0

#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.bind((TCP_IP,TCP_PORT))
#s.listen(1)

#print 'Listening for client...'

#conn, addr = s.accept()
#print 'Connection address:', addr
while 1:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((TCP_IP,TCP_PORT))
    s.listen(1)

    print 'Listening for client...'
    conn, addr = s.accept()
    print 'Connection address:', addr
    data = conn.recv(BUFFER_SIZE)
    if data == ";" :
            conn.close()
            print "Received all the data"
            i=0
            for x in param:
                    print x
            #break
    elif data:
            print "received data: ", data
            param.insert(i,data)
            i+=1
            #print "End of transmission"

我修改后的代码.

import sys
import socket

TCP_IP = '192.168.42.1'
TCP_PORT = 8888
BUFFER_SIZE = 1024
param = []
i=0

#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.bind((TCP_IP,TCP_PORT))
#s.listen(1)

#print 'Listening for client...'

#conn, addr = s.accept()
#print 'Connection address:', addr
while 1:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((TCP_IP,TCP_PORT))
    s.listen(1)
    while 1: 
            print 'Listening for client...'
            conn, addr = s.accept()
            print 'Connection address:', addr
            data = conn.recv(BUFFER_SIZE)
            if data == ";" :
                    conn.close()
                    print "Received all the data"
                    i=0
                    for x in param:
                            print x
                    #break
            elif data:
                    print "received data: ", data
                    param.insert(i,data)
                    i+=1
                    #print "End of transmission"
    s.close()

我创建了第二个 while 循环.我现在可以连续收听,但我只收到来自 ESP 的一个数据包(ESP 发送 9 个数据包).如何解决这个问题?

I created second while loop. I can listen continuously now, but I receive only one packet from the ESP (ESP send 9 packets). How to solve that issue?

推荐答案

如果你想持续监听远程端的连接和数据,你可以使用 select() 来实现

If you want to continuously listen for connections and data from your remote end, you can achieve this using select()

使用 select() 的代码的修改版本如下所示.这也将处理关闭连接的远程端:

A modified version of your code that uses select() is shown below. This will also handle the remote end closing the connection:

import sys
import socket
import select

TCP_IP = '127.0.0.1'
TCP_PORT = 8888
BUFFER_SIZE = 1024
param = []

print 'Listening for client...'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((TCP_IP,TCP_PORT))
server.listen(1)
rxset = [server]
txset = []

while 1:
    rxfds, txfds, exfds = select.select(rxset, txset, rxset)
    for sock in rxfds:
        if sock is server:
            conn, addr = server.accept()
            conn.setblocking(0)
            rxset.append(conn)
            print 'Connection from address:', addr
        else:
            try:
                data = sock.recv(BUFFER_SIZE)
                if data == ";" :
                    print "Received all the data"
                    for x in param:
                        print x
                    param = []
                    rxset.remove(sock)
                    sock.close()
                else:
                    print "received data: ", data
                    param.append(data)
            except:
                print "Connection closed by remote end"
                param = []
                rxset.remove(sock)
                sock.close()

注意,我已经用环回替换了您的 IP 地址,但您明白了.

NB I've replaced your IP address with the loopback but you get the idea.

希望这可能会有所帮助.

Hope this may be helpful.

这篇关于持续监听 TCP 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆