Python 当客户端和服务器在同一进程上运行时,为什么会拒绝连接?

Python 当客户端和服务器在同一进程上运行时,为什么会拒绝连接?,python,sockets,networking,Python,Sockets,Networking,我在同一进程中运行客户端和服务器时遇到问题。每次我尝试将客户端连接到服务器时,都会出现以下错误: 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 "/h

我在同一进程中运行客户端和服务器时遇到问题。每次我尝试将客户端连接到服务器时,都会出现以下错误:

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
这是客户端服务器的实际代码,非常基本,我只想让它们连接:

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

任何帮助或建议都将不胜感激。感谢阅读。

我的第一个猜测是,当客户端尝试连接时,服务器线程还没有真正启动;客户端正在连接,但没有任何内容正在侦听。创建一个线程并将控制权转移给它需要大量的时间。您可以在客户端连接之前睡觉,或者重试几次,或者在套接字打开时想象一下服务器线程信号。

而不是处理线程同步的麻烦和低级套接字问题(例如
socket.send
不保证发送通过它的整个字符串!),试试看

以下是使用Twisted的演示版本,没有同步问题:

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

defaultHost
defaultPort
的值是什么?或者重新表述Andrewski注释:您的服务器绑定到
localhost
,您确定客户端连接到
localhost
,而不是连接到与其他网络接口关联的主机名吗?
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()