简体   繁体   English

我的Python套接字服务器只能从客户端接收一条消息

[英]My Python socket server can only receive one message from the client

Hi i got a problem with my socket server or the client the problem is i can only send one message from the client to the server then the server stops receiving for some reason i want it to receive more then one. 嗨,我的套接字服务器或客户端出现问题,问题是我只能从客户端向服务器发送一条消息,然后服务器由于某种原因停止接收,我希望它接收到的消息多于一个。

Server.py Server.py

import socket 

host = ''
port = 1010

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind((host, port)) 
s.listen(1) 
conn, addr = s.accept() 
print ("Connection from", addr)
while True: 
    databytes = conn.recv(1024)
    if not databytes:
        break
    data = databytes.decode('utf-8')
    print("Recieved: "+(data))
    if data == "dodo":  
        print("hejhej")
    if data == "did":
        response = ("Command recived") 
        conn.sendall(response.encode('utf-8'))
conn.close()

client.py client.py

import socket 

host = '127.0.0.1'
port = 1010 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((host, port)) 
print("Connected to "+(host)+" on port "+str(port)) 
initialMessage = input("Send: ") 
s.sendall(initialMessage.encode('utf-8'))

while True:
    response = input("Send: ") 
    if response == "exit": 
        s.sendall(response.encode('utf-8')) 
s.close()

There is nothing wrong with your code, but the LOGIC of it is wrong, in the Client.py file and particularly in this loop: 您的代码没有任何问题,但是在Client.py文件中,尤其是在此循环中,其LOGIC错误:

while True:
    response = input("Send: ") 
    if response == "exit": 
        s.sendall(response.encode('utf-8')) 

This will not send to your Server side anything but string exit because of this: 由于以下原因,这不会将任何内容发送到Server端,而是字符串exit

if response == "exit":

So you are asking your Client.py script to only send anything the user inputs matching the string exit otherwise it will not send. 因此,您要让Client.py脚本仅发送与字符串exit匹配的用户输入中的任何内容,否则它将不发送。

It will send anything at the beginning before this while loop since you wrote: 自从您编写之后,它将在while循环之前的开始处发送任何内容:

initialMessage = input("Send: ") 
s.sendall(initialMessage.encode('utf-8'))

But after you are inside that while loop then you locked the s.sendall to only send exit string 但是在进入while循环之后,您将s.sendall锁定为仅发送exit字符串

You have to clean up your code logic. 您必须清理代码逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python(套接字)从客户端向客户端发送和接收消息? - How to send and receive message from client to client with Python (socket)? 我的客户端程序使用python socket API从服务器仅接受一个字节 - My client program is accepting only one byte from the server using python socket api 总是从ssl socket消息中分两部分接收(c#client,python3 server) - Always receive from ssl socket message in two parts (c# client, python3 server) 对于多人游戏,只有一个客户端在多线程服务器中接收消息 - ONLY one client receive message in multithreaded server for a multiplayer game C ++服务器无法通过套接字从python客户端读取我的消息 - C++ server cannot read my message from python client via socket 我在使Python套接字服务器从Python套接字客户端接收命令时遇到问题 - I got problems with making a Python socket server receive commands from a Python socket client 我希望客户端也可以从广播服务器套接字 python 发送私人消息 - i want that client can send private message also from a broadcast server socket python 服务器只与一个客户端连接,Python Socket - Server has connection only with one client, Python Socket Python socket 聊天服务器只监听一个客户端 - Python socket chat server only listens to one client Python 多线程套接字服务器无法从 2 个用户接收消息 - Python Multithread socket server can't receive msg from 2 users
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM