我们如何同时使用 tcpSerSock.listen(0) 和 tcpSerSock.send(str.encode(message))

how can we use both tcpSerSock.listen(0) and tcpSerSock.send(str.encode(message)) at the same time

我的 raspberry pi 是服务器,我正在尝试从 rpi 向 android 发送连续消息,同时从客户端(android 应用程序)接收命令,我真的不知道这是否是可能的,我无法做到,这不是反馈消息,这是我的代码希望你能帮助我谢谢。

import apptopi
from socket import *
from time import ctime
from nanpy import (ArduinoApi, SerialManager)

apptopi.setup()

connection = SerialManager()
a = ArduinoApi(connection = connection)

ctrCmd = ['Up','Down','Left','Right','Stop','Connect']

add = 0
add += 1
a = str(add) //**this is a sample that i want to send continously

HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)

tcpSerSock.listen(0)
tcpSerSock.send(str.encode(a))     <== i really don't know how to send 
                                      continuously


while True:
    print 'Waiting for connection'
    tcpCliSock,addr = tcpSerSock.accept()
    print '...connected from :', addr
    try:
            while True:
                    data = ''
                    data = tcpCliSock.recv(BUFSIZE)
                    if not data:
                            break
                    if data == ctrCmd[0]:
                            apptopi.forw()
                            print 'forward'
                    if data == ctrCmd[1]:
                            apptopi.back()
                            print 'backward'
                    if data == ctrCmd[2]:
                            apptopi.left()
                            print 'leftturn'
                    if data == ctrCmd[3]:
                            apptopi.right()
                            print 'rightturn'
                    if data == ctrCmd[4]:
                            apptopi.stp()
                            print 'stop'

    except KeyboardInterrupt:
            apptopi.close()
            GPIO.cleanup()
tcpSerSock.close();

好的,一种方法是为此使用 select() 函数。 documentation中有关于其操作的信息。

例如,我对您的程序进行了修改(见下文)。我没有raspberry pi,所以那部分代码被注释掉了,但是你可以根据需要替换它。

该示例使用 select() 的超时功能向客户端发送 "continuous" 消息,同时监视它们的传入消息。您可以将消息内容和超时调整为适合您的任何内容。注意,您可能还需要响应客户端消息,因为此代码仅在超时后才向客户端发送数据。进行所需的任何更改。

import sys
import socket
import select

ctrCmd = ['Up','Down','Left','Right','Stop','Connect']

HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)

tcpSerSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSerSock.bind(ADDR)

tcpSerSock.listen(1)
print 'Waiting for connection'

sendInterval = 1.0  # interval(sec) for sending messages to connected clients 

rxset = [tcpSerSock]
txset = []
while 1:
    rxfds, txfds, exfds = select.select(rxset, txset, rxset, sendInterval)
    if rxfds:
        for sock in rxfds:
            if sock is tcpSerSock:
                # a client is connecting
                tcpCliSock, addr = tcpSerSock.accept()
                tcpCliSock.setblocking(0)
                rxset.append(tcpCliSock)
                print '...connected from :', addr
            else:
                # a client socket has data or has closed the connection
                try:
                    data = sock.recv(BUFSIZE)
                    if not data:
                        print "...connection closed by remote end"
                        rxset.remove(sock)
                        sock.close()
                    else:
                        if data == ctrCmd[0]:
                            #apptopi.forw()
                            print 'forward'
                        if data == ctrCmd[1]:
                            #apptopi.back()
                            print 'backward'
                        if data == ctrCmd[2]:
                            #apptopi.left()
                            print 'leftturn'
                        if data == ctrCmd[3]:
                            #apptopi.right()
                            print 'rightturn'
                        if data == ctrCmd[4]:
                            #apptopi.stp()
                            print 'stop'           
                except:
                    print "...connection closed by remote end"
                    rxset.remove(sock)
                    sock.close()
    else:
        # timeout - send data to any active client
        for sock in rxset:
            if sock is not tcpSerSock:
                sock.send("Hello!\n") 

我用来测试的简单客户端程序在这里:

import sys
import socket
import time

ctrCmd = ['Up','Down','Left','Right','Stop','Connect']

HOST = '127.0.0.1'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)

tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

tcpCliSock.connect(ADDR)
time.sleep(1)
for i in range(len(ctrCmd)):
    tcpCliSock.send(ctrCmd[i])
    time.sleep(1)
data = tcpCliSock.recv(BUFSIZE)
print data
tcpCliSock.close()

希望这对您有所帮助,祝您好运。