我在我的套接字 python 程序中找不到错误
I can't find the error in my socket python program
我正在学习套接字教程,突然我的代码中出现了一个我无法理解或找不到的错误
任何人都可以帮我找到错误吗?我参考了教程代码,但它不起作用。
唯一的办法就是copy/paste我不喜欢
这是我的 server.py 文件
import socket
import threading
HEADER = 64
FORMAT = 'utf-8'
PORT = 5050
DISCONNECTED_ADDR = "!disCONNECTED"
SERVER = socket.gethostbyname(socket.gethostname())
print(SERVER)
ADDR = (SERVER, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECTED_ADDR:
connected = False
print(f"[{addr}] {msg}")
conn.send("Msg received".encode(FORMAT))
conn.close()
def main():
print(f"[LISTENING] Server listening on {SERVER}")
server.listen()
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print("STARTED")
print("APP STARTED")
main()
这是我的 client.py 文件
import socket
HEADER = 64
FORMAT = 'utf-8'
PORT = 5050
DISCONNECTED_ADDR = "!disCONNECTED"
SERVER = "#myipaddress"
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length)-1)
client.send(send_length)
client.send(message)
print(client.recv(2048).decode(FORMAT))
send("Hello World!")
input()
send("Hello Everyone!")
input()
send("Hello Tim!")
send(DISCONNECTED_ADDR)
Can anyone help me find the bug?
send_length += b' ' * (HEADER - len(send_length)-1)
您错误计算了客户端中 header 的填充长度。我不知道为什么你认为需要减少 1 - 它不合适,正确:
send_length += b' ' * (HEADER - len(send_length))
我正在学习套接字教程,突然我的代码中出现了一个我无法理解或找不到的错误
任何人都可以帮我找到错误吗?我参考了教程代码,但它不起作用。 唯一的办法就是copy/paste我不喜欢
这是我的 server.py 文件
import socket
import threading
HEADER = 64
FORMAT = 'utf-8'
PORT = 5050
DISCONNECTED_ADDR = "!disCONNECTED"
SERVER = socket.gethostbyname(socket.gethostname())
print(SERVER)
ADDR = (SERVER, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECTED_ADDR:
connected = False
print(f"[{addr}] {msg}")
conn.send("Msg received".encode(FORMAT))
conn.close()
def main():
print(f"[LISTENING] Server listening on {SERVER}")
server.listen()
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print("STARTED")
print("APP STARTED")
main()
这是我的 client.py 文件
import socket
HEADER = 64
FORMAT = 'utf-8'
PORT = 5050
DISCONNECTED_ADDR = "!disCONNECTED"
SERVER = "#myipaddress"
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length)-1)
client.send(send_length)
client.send(message)
print(client.recv(2048).decode(FORMAT))
send("Hello World!")
input()
send("Hello Everyone!")
input()
send("Hello Tim!")
send(DISCONNECTED_ADDR)
Can anyone help me find the bug?
send_length += b' ' * (HEADER - len(send_length)-1)
您错误计算了客户端中 header 的填充长度。我不知道为什么你认为需要减少 1 - 它不合适,正确:
send_length += b' ' * (HEADER - len(send_length))