close

[Solved] OS: [Errno 107] Transport endpoint is not connected

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error OS: [Errno 107] Transport endpoint is not connected in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How OS: [Errno 107] Transport endpoint is not connected Error Occurs?

Today I get the following error OS: [Errno 107] Transport endpoint is not connected in python.

How To Solve OS: [Errno 107] Transport endpoint is not connected Error ?

  1. How To Solve OS: [Errno 107] Transport endpoint is not connected Error ?

    To Solve OS: [Errno 107] Transport endpoint is not connected Error I tested your code with a little change on python 3.5.0 and it works: I think the trick is in sock.accept() method which returns a tuple:

  2. OS: [Errno 107] Transport endpoint is not connected

    To Solve OS: [Errno 107] Transport endpoint is not connected Error I tested your code with a little change on python 3.5.0 and it works: I think the trick is in sock.accept() method which returns a tuple:

Solution 1

I tested your code with a little change on python 3.5.0 and it works: I think the trick is in sock.accept() method which returns a tuple:

socket.accept() Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.

server

#server
>>> import socket
>>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sock.bind(("localhost", 8081))
>>> sock.listen(2)
>>> conn, addr = sock.accept()
>>> data= conn.recv(1024).decode("ascii") 

client:

#client
>>> import socket
>>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sock.connect(("localhost",8081))
>>> sock.send("hi".encode())
2
>>> sock.send("hiiiiiii".encode())
8
>>> sock.send(("#"*1020).encode())
1020

Solution 2


I’ve been getting the same error, and I realized that it’s because of one small issue: In the handle_client function, I put server.recv instead of conn.recv, so the server.py program was trying to receive data from its own side.

Below is the corrected version:

server.py:

import socket
import threading

def handle_client(conn, addr):
    while True:
        msg_length = conn.recv(64).decode("utf-8") # I got the error when I put server.recv
        if msg_length:
           msg = conn.recv(int(msg_length)).decode('utf-8') # Here too
           print(msg)

def start():
    server.listen()
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((socket.gethostbyname(socket.gethostname()), 5050))

start()

client.py:

import socket
import threading

def send(msg):
    client.send(str(len(msg)).encode('utf-8').ljust(64))
    client.send(msg.encode('utf-8'))

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((socket.gethostbyname(socket.gethostname()), 5050))

send("Hello world!")

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read