GHe
GHe

Reputation: 519

Why would connection refused when client and server is running on the same process?

I'm having a problem running a client and server in the same process. Every time I try to connect my client to the server, it would give me this error:

Traceback (most recent call last):
File "dirWatch.py", line 78, in <module>
startDirWatch(sLink)
File "dirWatch.py", line 68, in startDirWatch
sC.client('/home/homer/Downloads/test.txt')
File "/home/homer/Desktop/CSC400/gsync/serverClient.py", line 15, in client
sock.connect((host,port))
File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused

Here's the code I used, I'm basically trying to make a file-synchronization program. I'm new to StackOverflow so please excuse me if I haven't provided more details. Here is the code I'm testing the client and server code:

thread.start_new_thread(sC.server ,('localhost', 50001))
sC.client('/home/homer/Downloads/test.txt')

Here is the actual code for client server, it's pretty basic, I just want them to connect:

def client(filename, host = defaultHost, port = defaultPort):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.connect((host,port))
    sock.send((filename + '\n').encode())

    sock.close()

def serverthread(clientsock):
    sockfile = clientsock.makefile('r')
    filename = sockfile.readline()[:-1]
    try:
        print filename

    except:
       print('Error recieving or writing: ', filename)
    clientsock.close()

def server(host, port):
    serversock = socket(AF_INET, SOCK_STREAM)
    serversock.bind((host,port))
    serversock.listen(5)
    while True:
        clientsock, clientaddr = serversock.accept()
        print('Connection made');
        thread.start_new_thread(serverthread, (clientsock,))

Any help or advice would be appreciated. Thanks for reading.

Upvotes: 2

Views: 2334

Answers (2)

Jean-Paul Calderone
Jean-Paul Calderone

Reputation: 48335

Rather than deal with the hassles of thread synchronization and low-level socket quirks (like socket.send isn't guaranteed to send the whole string you pass it!), give Twisted a try!

Here's a version of your demo using Twisted, with no synchronization issues:

from twisted.internet import reactor
from twisted.internet.protocol import ServerFactory, ClientFactory
from twisted.protocols.basic import LineOnlyReceiver

class FileSyncServer(LineOnlyReceiver):
    def lineReceived(self, line):
        print "Received a line:", repr(line)
        self.transport.loseConnection()

class FileSyncClient(LineOnlyReceiver):
    def connectionMade(self):
        self.sendLine(self.factory.filename)
        self.transport.loseConnection()


def server(host, port):
    factory = ServerFactory()
    factory.protocol = FileSyncServer
    reactor.listenTCP(port, factory, interface=host)


def client(filename, host, port):
    factory = ClientFactory()
    factory.protocol = FileSyncClient
    factory.filename = filename
    reactor.connectTCP(host, port, factory)


server("localhost", 50001)
client("test.txt", "localhost", 50001)
reactor.run()

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

My first guess would simply be that when the client tries to connect, the server thread hasn't really gotten going yet; the client is connecting but nothing is listening. It takes substantial time to create a thread and transfer control to it. You could either sleep before the client connect, or retry it a few times, or get fancy about it and have the server thread signal when the socket is open.

Upvotes: 1

Related Questions