开发者

Python Sockets Errno 107: Transport Endpoint is not Connected [closed]

开发者 https://www.devze.com 2023-02-22 08:39 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

Edit: There is a very goos reason that the term RTFM exists. Reading the ... Manual works. If I had actually read the manual, I would have known that socket.accept() produces a client socket, to be used for the purpose of communication with the client. Apparently you are supposed to use that!

I am writing a simple chat program in Python using sockets. My code so far is below (I'm para-coding, the real thing is too long).

Server Class and Imports:

import socket
import os
import socket
from subprocess import Popen, PIPE
import threading

class Server:
    def __init__(self,name,port=5001):
        self.name = name
        self.port = port
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server_socket.bind(('',self.port))
        self.server_listen()

    def server_listen(self):
        self.server_socket.listen(5)
        while 1:
            self.client, self.address = self.server_socket.accept()
            self.discourse()

    def discourse(self):
        DisplayMessages(self.server_socket).start() #This is defined below
        while 1:
            #This sends messages to the client, also listening with DisplayMessages
            user_input = str(input(self.name + ' > '))
            send_bufr = '!dmsg :' + self.name + ' ' + user_input + '\r\n'
            self.client.send(bytearray(send_bufr, 'utf-8'))

I will omit the Client class as it's the same thing except for it connects with:

    self.client_socket.connect((self.host,self.port))

rather than accepting connections like the server. It sends input with an identical method to self.discourse in the server. The DisplayMessage class is below. It starts as it's own thread and is where the error occurs (only on the server, not the client).

class DisplayMessages(threading.Thread):
def __init__(self, socket):
    self.pipe = '/tmp/pyimPipe'
    if not os.path.exists(self.pipe):
        os.mkfifo(self.pipe)
    self.term = Popen(['xterm','-e','cat', self.pipe])
    self.pipe_interface = open(self.pipe, 'w', 1)
    self.socket = socket
    threading.Thread.__init__(self)

def run(self):
    self.display()

def display(self):
    recv_bufr = ''
    while 1:
        recv_bufr = (recv_bufr + 
                     self.socket.recv(1024).decode('utf-8'))
        messages = recv_bufr.split('\n')
        recv_bufr = messages.pop()

        for line in messages:
            line = line.rstrip()
            if line.find('!dmsg') != -1:
                self.message(line)
            else:
                print("Alert: Broken Message Recieved")

def message(self, line):
    message = line.split(':')[1:]
    if len(message) != 1:
        message = ':'.join(message)
    else:
        message = message[0]
    sender_name = message.split()[0]
    message = message.split()[1:]
    if len(message) != 1:
        message_text = ''
        for bit in message:
            message_text = message_text + ' ' + bit
    else:
        message_text = message[0]
    wrt_bufr = str(sender_name) + ':' + ' ' + message_text.strip() + '\n'
    self.pipe_interface.write(wrt_bufr)
    self.pipe_interface.flush()


def alert(self):
    pass 

The error occurs on this line:

recv_bufr = (recv_bufr + 
             self.socket.recv(1024).decode('utf-8'))

but only when the server class is using the DisplayMessage class. I apologize for the length o开发者_开发百科f this question. I wanted to post too much code rather than too little. Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消