Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-不允许发送或接收数据的请求,因为套接字未连接_Python_Sockets - Fatal编程技术网

Python-不允许发送或接收数据的请求,因为套接字未连接

Python-不允许发送或接收数据的请求,因为套接字未连接,python,sockets,Python,Sockets,我正在Pygame中创建一个游戏,它需要多人游戏的客户端服务器部分 首先,我要检查连接是否少于两个。如果是这种情况,客户端将显示一个屏幕,显示“等待连接” 我已使客户端成功地向服务器发送“1”消息,如果服务器未满,服务器将以“1”响应。因此,如果服务器没有响应1,则服务器已满,客户端可以继续 然而,我在标题中提到了这个错误 服务器代码: import socket import sys import threading from _thread import * import time s=s

我正在Pygame中创建一个游戏,它需要多人游戏的客户端服务器部分

首先,我要检查连接是否少于两个。如果是这种情况,客户端将显示一个屏幕,显示“等待连接”

我已使客户端成功地向服务器发送“1”消息,如果服务器未满,服务器将以“1”响应。因此,如果服务器没有响应1,则服务器已满,客户端可以继续

然而,我在标题中提到了这个错误

服务器代码:

import socket
import sys
import threading
from _thread import *
import time

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host=socket.gethostname()
ip=socket.gethostbyname(host)
port=8000

connections=[]

print('Your local ip address is',ip)

s.bind((host,port))
s.listen(2)

def  threaded_client(connection):

    while True:
        data=connection.recv(2048) #anything we receive
        if not data:
            break

    connection.close()


def checkconnected(connections):
    noofconn=len(connections)



while True:

    print('Waiting for a connection...')
    connection,address=s.accept()
    print(address,'has connected to server hosted at port',address[1])
    connections.append(address)



    data=connection.recv(1024)

    received=[]
    counter=0

    for letter in data:
        received.append(data[counter])
        counter+=1

    received=(chr(received[0]))     

    if received=='1':#handling initial connections
        if len(connections)!=2:
            s.sendall(b'1')

    if not data:
        break


    start_new_thread(threaded_client,(connection,))


s.close()
调用它的客户端代码:

host=socket.gethostname()
            ip=socket.gethostbyname(host)
            s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            address=address
            port=8000

            if address==ip:
                ishost=True

            else:
                ishost=False

            try:
                s.connect((address,port))
                connectionwaitingmenu()
                connected=False

                while connected==False:
                    s.sendall(b'1')
                    data=s.recv(1024)
                    received=[]
                    counter=0

                    for letter in data:
                        received.append(data[counter])
                        counter+=1

                    received=(chr(received[0])) 

                    if received=='1':
                        connected=False
                    elif received!='1':
                        connected=True
                        classselection()

错误发生在服务器代码中的s.sendallb'1'行。

代码中还有一些其他问题,但标题中出现错误的原因是您使用了错误的套接字在服务器端发送和接收数据

当服务器接受新的客户端连接时,addr=server.accept返回一个新的套接字,它表示与客户端通信的通道。与此客户端的所有进一步通信都是通过在conn上读写实现的。您不应该在s上调用recv或sendall,s是服务器套接字

代码应该如下所示:

# Assuming server is a bound/listening socket
conn, addr = server.accept()

# Send to client
conn.sendall(b'hello client')

# Receive from client
response = conn.recv(1024)

# NO
server.send(b'I am not connected')
this_wont_work = server.recv(1024)