繁体   English   中英

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

[英]Python socket library: OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected

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))

循环中的上述行,以便每次循环迭代时它都会连接。 但这没有帮助。

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部分则没有。 但仍在尝试在未连接的套接字上发送内容。

暂无
暂无

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

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