【问题标题】:Python Socket Programming client not receiving all packetsPython Socket编程客户端未收到所有数据包
【发布时间】:2020-06-10 15:01:34
【问题描述】:

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

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

Server.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中的变量被相应地初始化。

但输出看起来像这样:

Right is the Sender window and left is the receiver window

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

【问题讨论】:

    标签: python sockets python-sockets


    【解决方案1】:

    您的代码正在丢弃一些传入的消息,因为您在调用 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"))
    

    【讨论】:

    • 我试过了,但它仍在打印消息。中间仍有一条消息被遗漏。
    猜你喜欢
    • 1970-01-01
    • 2021-06-29
    • 2018-07-15
    • 2014-08-29
    • 2014-10-19
    • 2021-06-14
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多