客户端是否必须断开与服务器的连接才能让另一个客户端连接? Python 套接字

发布于 2025-01-09 18:19:43 字数 1228 浏览 0 评论 0原文

我正在尝试连接到传感器 #1(该传感器具有放大器,您可以通过 TCP/IP 进行通信),该传感器位于我的笔记本电脑和 PLC 所在的网络上。我已经成功连接到传感器 #2 & 3 在 PLC 未向其发送请求的机器上,但我无法从与 PLC 主动通信的传感器 #1 获得响应。连接到服务器时是否需要断开与服务器的连接,以便另一个客户端连接到它并发出请求?我附上了一个示例程序,用于验证我与每个传感器的连接是否成功,但我与传感器 #1 的连接从未成功,因为它从未响应

import socket
import time
import sys

client1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client1.connect(('192.168.3.22', 8500)) #Sensor 2
client2.connect(('192.168.3.23', 8500)) #Sensor 3
client3.connect(('192.168.3.21', 8500)) #Sensor 1
print(client1)
print(client2)
print(client3)

status_result_reading="RT\r"

client1.send(bytes(status_result_reading,'ascii'))
client2.send(bytes(status_result_reading,'ascii'))
client3.send(bytes(status_result_reading,'ascii'))
time.sleep(.2)
msg1 = client1.recv(200)
msg2 = client2.recv(200)
#msg3 = client3.recv(200)
#print(msg1.decode('ascii')+'\t'+msg2.decode('ascii')+'\t'+msg3.decode('ascii'))
print(msg1.decode('ascii')+'\t'+msg2.decode('ascii'))


#Disconnect from the robot and close the socket object
client1.close()
client2.close()
client3.close()
print('close')
sys.exit()

I am trying to connect to sensor #1(the sensor has amplifier that you can communicate with via tcp/ip) that is on the network that both my laptop and the PLC are on. I have already successfully connected to sensors #2 & 3 on the machine which the PLC is not sending requests to but I cannot get a response from sensor #1 which the PLC is actively communicating with. Is it necessary when connecting to a server that you need to disconnect from it in order for another client to connect to it and make a request? I have attached a sample program I made to verify whether my connections to each sensor are successful but my connection to sensor #1 is never successful as it never responds

import socket
import time
import sys

client1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client1.connect(('192.168.3.22', 8500)) #Sensor 2
client2.connect(('192.168.3.23', 8500)) #Sensor 3
client3.connect(('192.168.3.21', 8500)) #Sensor 1
print(client1)
print(client2)
print(client3)

status_result_reading="RT\r"

client1.send(bytes(status_result_reading,'ascii'))
client2.send(bytes(status_result_reading,'ascii'))
client3.send(bytes(status_result_reading,'ascii'))
time.sleep(.2)
msg1 = client1.recv(200)
msg2 = client2.recv(200)
#msg3 = client3.recv(200)
#print(msg1.decode('ascii')+'\t'+msg2.decode('ascii')+'\t'+msg3.decode('ascii'))
print(msg1.decode('ascii')+'\t'+msg2.decode('ascii'))


#Disconnect from the robot and close the socket object
client1.close()
client2.close()
client3.close()
print('close')
sys.exit()

.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文