简体   繁体   English

Python套接字。 OSError:[Errno 9]错误的文件描述符

[英]Python sockets. OSError: [Errno 9] Bad file descriptor

It's my client: 是我的客户:

#CLIENT
import socket
conne = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conne.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
i=0
while True:
    conne.connect ( ('127.0.0.1', 3001) )
    if i==0:
        conne.send(b"test")
        i+=1
    data = conne.recv(1024)
    #print(data)
    if data.decode("utf-8")=="0":
        name = input("Write your name:\n")
        conne.send(bytes(name, "utf-8"))
    else:
        text = input("Write text:\n")
        conne.send(bytes(text, "utf-8"))
    conne.close()

It's my server: 这是我的服务器:

#SERVER

import socket

counter=0
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 3001))
sock.listen(10)

while True:
    conn, addr = sock.accept()
    data = conn.recv(1024)
    if len(data.decode("utf-8"))>0:
        if counter==0:
            conn.send(b"0")
            counter+=1
        else:
            conn.send(b"1")
            counter+=1
    else:
        break
        print("Zero")
        conn.send("Slava")
    conn.close()
))

After starting Client.py i get this error: 启动Client.py之后,出现以下错误:

Traceback (most recent call last): File "client.py", line 10, in conne.connect ( ('127.0.0.1', 3001) ) OSError: [Errno 9] Bad file descriptor 追溯(最近一次通话最近):conne.connect中的文件“ client.py”,第10行(('127.0.0.1',3001))OSError:[Errno 9]错误的文件描述符

Problem will be created just after first input. 第一次输入后将创建问题。 This program - chat. 这个程序-聊天。 Server is waiting for messages. 服务器正在等待消息。 Client is sending. 客户端正在发送。

There are a number of problems with the code, however, to address the one related to the traceback, a socket can not be reused once the connection is closed, ie you can not call socket.connect() on a closed socket. 代码有很多问题,但是,为了解决与回溯相关的问题,一旦关闭连接,便无法重用套接字,即,您不能在已关闭的套接字上调用socket.connect() Instead you need to create a new socket each time, so move the socket creation code into the loop: 相反,您每次都需要创建一个新的套接字,因此将套接字创建代码移入循环:

import socket

i=0
while True:
    conne = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    conne.connect(('127.0.0.1', 3001))
    ...

Setting socket option SO_BROADCAST on a stream socket has no affect so, unless you actually intended to use datagrams (UDP connection), you should remove the call to setsockopt() . 在流套接字上设置套接字选项SO_BROADCAST不会产生任何影响,除非您实际上打算使用数据报(UDP连接),否则应删除对setsockopt()的调用。

At least one other problem is that the server closes the connection before the client sends the user's name to it. 至少另一个问题是服务器在客户端向其发送用户名之前关闭了连接。 Probably there are other problems that you will find while debugging your code. 调试代码时可能还会发现其他问题。

Check if 3001 port is still open. 检查3001端口是否仍然打开。

You have given 'while True:' in the client script. 您已经在客户端脚本中给出了“ while True:”。 Are you trying to connect to the server many times in an infinite loop? 您是否试图以无限循环多次连接到服务器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM