重复/连续python服务器和客户端

nat*_*yer 8 python sockets arduino

我想建立一个系统,我有一个python客户端和服务器不断发送/接收数据.我发现的所有代码示例都显示了如何将单个消息发送到套接字,而不是如何连续设置发送/接收数据.

现在我的代码是:

client.py

import socket
import time

while True:
    try:
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client_socket.connect(("192.168.0.250", 10220))
        data = "GET\nSONAR\n\n"
        print 'send to server: ' + data
        client_socket.send(data)
        client_socket.close()
    except Exception as msg:
        print msg
Run Code Online (Sandbox Code Playgroud)

我希望代码能够每分钟多次发送命令,但是现在它似乎并不总是发送消息,我不知道为什么.为什么控制流不连续?

server.py

import socket

host = '192.168.0.100'
port = 8220
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((address))
server_socket.listen(5)

while True:
    try:
        print "Listening for client . . ."
        conn, address = server_socket.accept()
        print "Connected to client at ", address
        #pick a large output buffer size because i dont necessarily know how big the incoming packet is                                                                                              
        output = conn.recv(2048);
        if output:
            print "Message received from client:"
            print output

        #conn.send("This is a response from the server.")                                                                                                                                            
        conn.close()
        #print "Test message sent and connection closed." 
Run Code Online (Sandbox Code Playgroud)

这在第一次尝试时工作正常,但我不能让服务器自动再次收听下一条消息 - 它总是挂在"倾听客户端......".

有什么想法吗?

谢谢!

Chr*_*ris 10

这实际上对我来说很好,但我必须调整client.py中的端口以匹配server.py中的端口.我还必须添加一个异常来处理server.py中的KeyboardInterrupt,这样就有办法退出程序.

倾听客户的意见...连接到客户端('127.0.0.1',53944)从客户端收到的消息:GET SONAR

倾听客户的意见...连接到客户端('127.0.0.1',53945)从客户端收到的消息:GET SONAR

编辑:

我采取了改进建筑的方法.我创建一个连接并使用它来传递多个消息,在客户端中的每个消息之间暂停以等待服务器发送确认.

client.py:

import socket
import time

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 8220))

for index in xrange(5):
    data = "GET\nSONAR%d\n\n" % index
    print 'send to server: ' + data
    client_socket.send(data)
    while client_socket.recv(2048) != "ack":
        print "waiting for ack"
    print "ack received!"

#send disconnect message                                                                                                                           
dmsg = "disconnect"
print "Disconnecting"
client_socket.send(dmsg)

client_socket.close()
Run Code Online (Sandbox Code Playgroud)

server.py:

import socket
import sys

host = 'localhost'
port = 8220
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)

print "Listening for client . . ."
conn, address = server_socket.accept()
print "Connected to client at ", address
#pick a large output buffer size because i dont necessarily know how big the incoming packet is                                                    
while True:
    output = conn.recv(2048);
    if output.strip() == "disconnect":
        conn.close()
        sys.exit("Received disconnect message.  Shutting down.")
        conn.send("dack")
    elif output:
        print "Message received from client:"
        print output
        conn.send("ack")
Run Code Online (Sandbox Code Playgroud)