gpt4 book ai didi

python - 'socket.error : [Errno 9] Bad file descriptor' as I run my client

转载 作者:太空宇宙 更新时间:2023-11-03 17:09:33 29 4
gpt4 key购买 nike

当我运行客户端时,我的服务器崩溃了,

它说:

Traceback (most recent call last):
File "C:/Users/Or/PycharmProjects/2.6/cyber.py", line 17, in <module>
data = client_socket.recv(1024)
File "C:\Python27\lib\socket.py", line 174, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor

服务器:

import socket
import random
import time

server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8820))
server_socket.listen(1)

(client_socket, client_address) = server_socket.accept()

TIME = time.asctime(time.localtime())
NAME = "Frank"
RAND = random.randrange(1,10)
EXIT = client_socket.close()
options = [TIME, NAME, RAND, EXIT]

data = client_socket.recv(1024)
if data == str(TIME):
client_socket.send('Time is: ' + TIME)
elif data == str(NAME):
client_socket.send('My name is: ' + NAME)
elif data == str(RAND):
client_socket.send(RAND)
elif data == str(EXIT):
EXIT




client_socket.close()
server_socket.close()

客户:

import socket

my_socket = socket.socket()
my_socket.connect(('127.0.0.1', 8820))

theData = raw_input("enter command")
options = ['TIME', 'NAME', 'RAND', 'EXIT']
for z in options:
if theData == z:
my_socket.send(theData)
data = my_socket.recv(1024)
print data
else:
theData = raw_input("enter a real command")

我该怎么办?

最佳答案

服务器中的以下行会阻塞,直到客户端连接为止:

(client_socket, client_address) = server_socket.accept()

客户端连接后,服务器会执行以下操作:

  .... = client_socket.close()

然后你的服务器会执行以下操作:

 client_socket.recv(1024)

那是行不通的。

what should I do?

删除这一行:

EXIT = client_socket.close()

您的代码还有其他几个问题:

1) 您不知道 send() 实际会发送多少数据:

socket.send(bytes[, flags])
...
Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data. https://docs.python.org/3/library/socket.html#socket.socket.send

换句话说,send() 不一定能够一次性发送整个消息 - 因此 send() 返回实际发送的字节数。您可以使用 sendall() 来改善该问题。

2) 但是,使用 send()sendall() 时,您不知道数据是否以 10 个 block 、5 个 block 或全部以 1 block 的形式发送 block ,因此您必须使用循环来 recv() 整个消息:

data = []

while True:
chunk = my_socket.recv()
if not chunk: break #When the other side closes the socket, chunk = ''
data.append(chunk)

all_data = ''.join(data)

当对方关闭套接字时,recv()将返回一个空字符串。

3)现在是最重要的:

data = client_socket.recv(1024)

您的服务器必须在循环中使用 recv() 以确保它从客户端读取整个消息:

data = []

while True:
chunk = client_socket.recv()
if not chunk: break #When the other side closes the socket, chunk = ''
data.append(chunk)

all_data = ''.join(data)

但是读循环如何知道它何时读取了所有数据呢?请注意,如所写,当另一端关闭套接字导致 recv() 返回空白字符串(从而终止 while 循环)时,循环终止。但是,在您的情况下,客户端在 send() 之后不会关闭套接字,因此服务器的 recv() 永远不会返回空白字符串,并且 while 循环将继续尝试recv()——但客户端将完成消息发送并等待响应。死锁。

关于python - 'socket.error : [Errno 9] Bad file descriptor' as I run my client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34238713/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com