同じプロセスでクライアントとサーバーを実行する際に問題が発生しています。 、私は基本的に、ファイル同期プログラムを作成しようとしているクライアントとサーバーが同じプロセスで実行されているときに接続が拒否されるのはなぜですか?
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
ここで私が使用したコードがあります:私は、サーバーへの私のクライアントを接続しようとするたびに、それは私に、このエラーを与えるだろう。私はStackOverflowの新機能ですので、もっと詳しく説明していないのであれば、私には容赦してください。ここで私は、クライアントとサーバーのコードをテストしていたコードは次のとおりです。ここで
thread.start_new_thread(sC.server ,('localhost', 50001))
sC.client('/home/homer/Downloads/test.txt')
は、クライアント・サーバの実際のコードは、それが、私はちょうどそれらを接続する、かなり基本的ですが、次のとおりです。
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,))
すべてのヘルプまたはアドバイスをいただければ幸いです。読んでくれてありがとう。
andrewdskiコメントを言い直すために 'defaultHost'と' defaultPort' – andrewdski
かの値がどのようなものです:あなたのサーバがlocalhost' 'に結合しクライアントが 'localhost'に接続し、別のネットワークインターフェースに関連付けられたホスト名には接続しないのでしょうか? –