Socket Error 106: 'Transport endpoint is already connected'

P

palmem

I am trying to write a simple FTP server in order to learn about
sockets
This is my first time trying sockets

This code should take a connection on port 8110, dump it to a client
"thread" (not a thread yet), print "Test\n" to the thread, and close
everything.
It fails on creating the client thread with error 106: 'Transport
endpoint is already connected'

On running the code:
dftp starting
getting the socket at 127.0.0.1 at port 8110
listening for 5 connections
entering main loop
####pause here until telnet####
found a client @ <socket._socketobject object at 0x2ba16f6cd650> addr:
('127.0.0.1', 58643)
Creating a new client socket
Trying to connect
Traceback (most recent call last):
File "./dftpd.py", line 46, in ?
main()
File "./dftpd.py", line 41, in main
clientThread = Connection(clientsocket, address);
File "/home/palmer/prog/dftp/dftpd/connection.py", line 29, in
__init__
self.sock.connect(a);
File "<string>", line 1, in connect
socket.error: (106, 'Transport endpoint is already connected')

Telnet output:
$ telnet localhost 8110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

Here's my code:
config is a stand-in for a config-file reader, now it just runs
localhost:8110 and 5 connections
debug just prints to standard out (the number afterwords is the level,
but now everything is printed)
#################dftpd.py#########################
import debug
import socket
import config
from connection import Connection

shouldRun=True;

def main():
debug.stdout("dftp starting", 1)
debug.stdout("getting the socket at " + str(config.listenAddress()) +
" at port " + str(config.listenPort()), 10);
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
sock.bind((config.listenAddress(), config.listenPort()));
debug.stdout("listening for " + str(config.listenConnections()) + "
connections", 10);
sock.listen(config.listenConnections());
debug.stdout("entering main loop", 10);
while shouldRun:
(clientsocket, address) = sock.accept();
debug.stdout("found a client @ " + str(clientsocket) + " addr: " +
str(address), 10);
clientThread = Connection(clientsocket, address);
print clientThread.write("Test\n");
clientThread.close;
sock.close();

main()

########################connection.py########################
import debug;
import socket;

class Connection:
def __init__(self, s, a):
debug.stdout("Creating a new client socket", 15);
self.sock = s;
debug.stdout("Trying to connect", 15);
self.sock.connect(a);
debug.stdout("Making the file over the socket", 15);
self.file = self.sock.makefile();
def read():
return file.read();
def write(out):
file.write(out);
def close():
file.close();
sock.close();
 
D

Dennis Lee Bieber

This code should take a connection on port 8110, dump it to a client
(clientsocket, address) = sock.accept();

This returns the parameters of the initiating client as a connected
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.

clientThread = Connection(clientsocket, address);

said:
debug.stdout("Creating a new client socket", 15);
self.sock = s;
debug.stdout("Trying to connect", 15);
self.sock.connect(a);

What is there to connect too -- the accept() call is already a live
connection.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top