无法通过Python中的套接字进行客户端 – 服务器通信

在过去的两周里,我一直在反对插座问题,但无济于事.我有一个12’客户端’机器和一台服务器机器的设置.服务器被赋予了大量任务,将其分成12个较小的任务,然后将它们分发给12个客户端.客户流失,一旦完成任务,他们应该让服务器知道他们已经通过套接字通信完成了.出于某种原因,这只是一点点工作或根本不工作(服务器和客户端,只是坐在while循环中).

这是服务器上的代码:

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind(('localhost', RandomPort))
socket.listen(0)
socket.settimeout(0.9)

[Give all the clients their tasks, then do the following:]

while 1:
    data = 'None'
    IP = [0,0]   
    try:
        Client, IP = socket.accept()
        data = Client.recv(1024)
        if data == 'Done':
            Client.send('Thanks')
        for ClientIP in ClientIPList():
            if ClientIP == IP[0] and data == 'Done':
                FinishedCount += 1 
            if FinishedCount == 12:
                break
    except:
        pass

以下是所有客户端的代码:

[Receive task from server and execute. Once finished, do the following:]

while 1:
    try:
        f = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        f.connect((IPofServer, RandomPort)) 
        f.settimeout(0.5)
        f.send('Done')
        data = f.recv(1024)
        if data == 'Thanks':
            f.shutdown(socket.SHUT_RDWR)
            f.close()
            break
    except:
        f.close()
        time.sleep(2+random.random()*5)

我使用过Wireshark,发现数据包正在飞来飞去.然而,“FinishedCount”似乎永远不会增加……有什么明显的错误,我在设置它时错过了吗?这是我第一次接触插座….

谢谢大家的帮助!

编辑:我对代码进行了以下更改:

在服务器上:socket.listen现在是socket.listen(5)

最佳答案 好吧,这花了我一段时间,但我想我弄清楚是什么导致了这个:

> glglgl的答案是正确的 – 使用’localhost’会使机器运行
只听自己而不是网络上的其他机器.这个
是罪魁祸首.
>将que中允许的数量从0增加到5会减少
在客户端上出现“连接被拒绝”错误的可能性
侧.
>我错误地认为套接字连接是无限的
while循环可以无限快速关闭 – 但是,有一个
无限时,两边的循环有时会导致客户端
有时会被计算两次,因为while循环不是
同步.当然,这导致了“客户不可知”
finishedCount增加两倍,这导致服务器相信所有
客户是在他们不是的时候完成的.使用chown的代码(谢谢
chown!),这可以像这样处理:

def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind((HOST, PORT))
    sock.listen(0)

    FINISHEDCLIENTS = []

    while 1:
        data = 'None'
        IP = [0, 0]
        try:
            client, ip = sock.accept()
            data = client.recv(1024)
            print "%s: Server recieved: '%s'" % (time.ctime(), data)

            if data == 'Done':
                print "%s: Server sending: 'Thanks'" % time.ctime()
                client.send('Thanks')

                if ip[0] in CLIENT_IPS and ip[0] not in FINISHEDCLIENTS: 
                    FINISHEDCLIENTS.append(ip[0])

                    if len(FINISHEDCLIENTS) == 12:
                        #raise MyException
                        break

        except Exception, e:
            print "%s: Server Exception - %s" % (time.ctime(), e)

在客户端,我将代码更改为此(当然,
RandomPort与上面的服务器脚本中使用的相同):

SentFlag = 0
data = 'no'
while SentFlag == 0:
    try:
        f = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        f.connect((IPofServer, RandomPort))
        f.settimeout(20)
        f.send('Done')
        data = f.recv(1024)
        if data == 'Thanks':
            f.shutdown(socket.SHUT_RDWR)
            f.close()
            SentFlag = 1
    except:
        f.close()
        time.sleep(2*random.random())

PS:我对.shutdown()vs .close()的理解是关闭连接,但如果它正在进行另一次通信,则不一定是套接字.无论它正在做什么,.shutdown()都会关闭套接字.我虽然没有任何证据.

我认为应该这样做 – 再次感谢大家帮助修复此代码!

点赞