首页 > 解决方案 > Python Socket编程客户端未收到所有数据包

问题描述

所以我正在学习使用套接字进行 Python 网络编程的概念。我试图创建一个简单的消息传递应用程序,其中有一个服务器、一个发送者和一个接收者。发送者将消息发送到服务器,服务器将消息传递给接收者,然后在接收者中显示。

但主要问题是,当我从发件人发送消息时,每发送 3 条消息后的消息被打印出来,其余的都丢失/忽略。以下是代码片段。

服务器.py:

import socket
from threading import *

c = None #Client socket1
addr = None #Client address1
c2 = None #Client socket2
addr2 = None #Client address2

server_socket1 = socket.socket() #by default it is SOCK_STREAM (TCP) and has porotocal AF_INET (IPv4) 

server_socket1.bind(('localhost',9999)) #server machine's ip and port on which it will send and recieve connections from

server_socket1.listen() 
print("Server started successfully!!!")
print("Waiting for connections...\n\n")

while (((c is None)and(addr is None)) and ((c2 is None) and (addr2 is None))):
    if((c is None) and (addr is None)):
        c,addr = server_socket1.accept()
        print("User connected to client1 socket!!")
        c.send(bytes("Connected to the apps server!!!","utf-8"))
        name = c.recv(1024).decode()
        print("Client connected with name "+name+ " ip address "+str(addr))
        c.send(bytes("******Welcome to Messenger Lite "+name+" ******","utf-8"))

    if((c2 is None) and (addr2 is None)):
        c2,addr2 = server_socket1.accept()
        print("\n\nUser connected to client2 socket!!")
        c2.send(bytes("\n\nConnected to the apps server!!!","utf-8"))
        name2 = c2.recv(1024).decode()
        print("Client connected with name "+name2+ " ip address "+str(addr2))
        c.send(bytes("******Welcome to Messenger Lite "+name2+" ******","utf-8"))


while True:
    if(c.recv(1024)!=None):
        msg = c.recv(1024).decode()
        c2.send(bytes(msg,"utf-8"))

发件人.py:

import socket

client_socket = socket.socket() #by default it is SOCK_STREAM (TCP) and has porotocal AF_INET (IPv4) 

client_socket.connect(('192.168.217.1',9999)) #server machine's ip and port on which it will send and recieve connections from
name = input ("Enter your name : ")
client_socket.send(bytes(name,"utf-8"))

print(client_socket.recv(1024).decode())
print(client_socket.recv(1024).decode())

while True:
        message = input("\n\nEnter a message : ")
        client_socket.send(bytes(message,"utf-8"))
        print("Message Sent!!!")

接收器.py:

import socket

client_socket = socket.socket() #by default it is SOCK_STREAM (TCP) and has porotocal AF_INET (IPv4) 

client_socket.connect(('192.168.217.1',9999)) #server machine's ip and port on which it will send and recieve connections from
name = input ("Enter your name : ")
client_socket.send(bytes(name,"utf-8"))

print(client_socket.recv(1024).decode())
print(client_socket.recv(1024).decode())

while True:
    if(client_socket.recv(1024).decode()):
        print(client_socket.recv(1024).decode())

这里的逻辑是,当服务器启动时,首先发送方连接,然后接收方连接,server.py 中的变量会相应地初始化。

但输出看起来像这样:

右边是发送者窗口,左边是接收者窗口

如图所示,中间的所有消息都被忽略,并且打印了 3 条消息之后的消息。可能是什么问题??

标签: pythonsocketspython-sockets

解决方案


您的代码正在丢弃一些传入的消息,因为您在调用recv()时没有使用它返回的内容。

我会重写这个:

while True:
    if(c.recv(1024)!=None):
        msg = c.recv(1024).decode()
        c2.send(bytes(msg,"utf-8"))

作为:

while True:
    msg = c.recv(1024)
    if(msg != None):
        msg = msg.decode()
        c2.send(bytes(msg, "utf-8"))

推荐阅读