Python 套接字编程客户端未收到所有数据包
Python Socket Programming client not receiving all packets
所以我正在学习 Python 使用套接字进行网络编程的概念。
我试图创建一个简单的消息传递应用程序,其中有一个服务器、一个发送者和一个接收者。
发件人将消息发送给服务器,服务器传递给接收者,然后显示在接收者中。
但主要问题是,当我从发件人发送一条消息时,每发送 3 条消息后的消息就会被打印出来,其余都是 lost/ignored。
这是代码片段。
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"))
Sender.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!!!")
Receiver.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 条消息后的消息。可能是什么问题??
您的代码正在丢弃一些传入消息,因为您在调用 recv()
时没有使用它 returns。
我会重写这个:
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"))
所以我正在学习 Python 使用套接字进行网络编程的概念。 我试图创建一个简单的消息传递应用程序,其中有一个服务器、一个发送者和一个接收者。 发件人将消息发送给服务器,服务器传递给接收者,然后显示在接收者中。
但主要问题是,当我从发件人发送一条消息时,每发送 3 条消息后的消息就会被打印出来,其余都是 lost/ignored。 这是代码片段。
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"))
Sender.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!!!")
Receiver.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 条消息后的消息。可能是什么问题??
您的代码正在丢弃一些传入消息,因为您在调用 recv()
时没有使用它 returns。
我会重写这个:
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"))