[英]How to get continous data on server from client?
I am learning sockets here I have created a simple server & client, here when I am sending multiple times data from client side on server side only once it is reflecting kindly assist whats missing here我正在学习 sockets 在这里我创建了一个简单的服务器和客户端,在这里当我从客户端向服务器端发送多次数据时,只有在它反映了这里缺少的内容时,请协助
Server服务器
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
print(host)
port = 12345 # Reserve a port for your service.
s.bind(('localhost', port)) # Bind to the port
print(s)
s.listen(5) # Now wait for client connection.
print("socket is listening")
while True:
connection, address = s.accept() # Establish connection with client.
print('Got connection from', address)
connection.send(b'Thank you for connecting')
buf = connection.recv(64)
if len(buf) > 0:
print(buf)
Client客户
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 12345))
send = '1'
while True:
if send == '1':
forsending = input("Enter data to send to server\n")
clientsocket.send(forsending.encode())
send = input("Do u want 2 resend\npress 1 for yes ny other for no")
else:
print(send)
break
Also from server side why connection.send(b'Thank you for connecting')
is not showing on client end同样从服务器端为什么
connection.send(b'Thank you for connecting')
没有显示在客户端
You need to do clientsocket.recv
on the client side您需要在客户端执行
clientsocket.recv
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 12345))
send = '1'
while True:
if send == '1':
forsending = input("Enter data to send to server\n")
clientsocket.send(forsending.encode())
buf = clientsocket.recv(64)
if len(buf) > 0:
print(buf)
send = input("Do u want 2 resend\npress 1 for yes ny other for no")
else:
print(send)
break
This pattern is called the Client/Server pattern.这种模式称为客户端/服务器模式。 I would recommend using the PyZMQ library.
我建议使用 PyZMQ 库。 You can read more here:
你可以在这里阅读更多:
https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/client_server.html https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/client_server.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.