Python套接字库:OSError:[WinError 10057]不允许发送或接收数据的请求,因为套接字未连接

crcmnpdw  于 2022-12-20  发布在  Python
关注(0)|答案(1)|浏览(424)

OSError:[WinError 10057]由于套接字未连接并且(当使用sendto调用在数据报套接字上发送时)未提供地址,发送或接收数据的请求被禁止。
我得到了上述错误..我的服务器和客户端可以发送和接收他们的第一个消息,但我得到这个错误,如果我试图发送多个消息。
我的服务器代码在这里

import socket
import threading
import time
from tkinter import *

#functions
def t_recv():
    r = threading.Thread(target=recv)
    r.start()

def recv():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listensocket:
        port = 5354
        maxconnections = 9
        ip = socket.gethostbyname(socket.gethostname())
        print(ip)
        server = (ip, port)
        FORMAT = 'utf-8'

        listensocket.bind((server))
        listensocket.listen(maxconnections)
        (clientsocket, address) = listensocket.accept()
        msg = f'\[ALERT\] {address} has joined the chat.'
        lstbox.insert(0, msg)

    while True:
        sendermessage = clientsocket.recv(1024).decode(FORMAT)
        if not sendermessage == "":
            time.sleep(3)
            lstbox.insert(0, 'Client: ' +sendermessage)

def t_sendmsg():
    s = threading.Thread(target=sendmsg)
    s.start()

at = 0

def sendmsg():
    global at
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as g:
        hostname = 'Lenovo-PC'
        port = 5986

        if at==0:
            g.connect((hostname, port))
            msg = messagebox.get()
            lstbox.insert(0, 'You: ' +msg)
            g.send(msg.encode())
            at += 1

        else:
            msg = messagebox.get()
            lstbox.insert(0, 'You: ' +msg)
            g.send(msg.encode())

和我的客户端代码是相同的,只有微小的差异

import socket
import time
import threading
from tkinter import *

#functions
def t_recv():
    r = threading.Thread(target=recv)
    r.start()

def recv():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listensocket:
        port = 5986
        maxconnections = 9
        ip = socket.gethostname()
        print(ip)
        FORMAT = 'utf-8'
        host = 'MY_IP'    # My actual ip is there in the code

        listensocket.bind((host, port))
        listensocket.listen(maxconnections)
        (clientsocket, address) = listensocket.accept()

        while True:
            sendermessage = clientsocket.recv(1024).decode(FORMAT)
            if not sendermessage == "":                 
                time.sleep(3)                 
                lstbox.insert(0, 'Server: ' +sendermessage)

def t_sendmsg():
    s = threading.Thread(target=sendmsg)
    s.start()

at = 0

def sendmsg():
    global at
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as g:
        hostname = 'Lenovo-PC'
        port = 5354

        if at==0:
            g.connect((hostname, port))
            msg = messagebox.get()
            lstbox.insert(0, 'You: ' +msg)
            g.send(msg.encode())
            at += 1

        else:
            msg = messagebox.get()
            lstbox.insert(0, 'You: ' +msg)
            g.send(msg.encode())

请让我知道什么样的变化是需要作出的,以使它运行的每一个消息。
我试着把

g.connect((hostname, port))

循环中的上一行,以便在每次循环迭代时连接。但这没有帮助。

xqnpmsa8

xqnpmsa81#

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as g:
    ...

    if at==0:
        g.connect((hostname, port))
        ...
        g.send(msg.encode())
        at += 1

    else:
        ...
        g.send(msg.encode())

if at==0条件下,它连接到服务器,在else部分中,它没有连接到服务器,但仍试图在未连接套接字上发送一些东西。

相关问题