Python 服务器设置问题 2C

问题:
1. 从我的电脑ping服务器能成功,但是客户端发送信息服务器接收不到,
同样的客户端代码拿到服务器上服务器端是能接收到的,求教怎么处理。
我已经把服务器端的防火墙放开了python.exe应用程序的接入。
2. 服务器端在UDP协议下,进入while循环,第一次能在recvfrom()处停住等待,
接收信息后,如果处理,如果进入“该用户已在列表中”的话就会继续循环,但如果
进入“新用户登入”的话在再次进入while循环的recvfrom循环的话就会卡死,求教!PS:该问题如果我把两个东西都放到win10家庭版下,设置客户端尝试连接的ip为:127.0.0.1的时候就不会出现这个问题。

环境:
1. 服务器端:阿里云服务器win10Server操作系统,python2.7.13Shell进行测试,
已绑定固定ip:116.62.149.45
2. 客户端:win10家庭版,python2.7.13,没做内网映射,内网ip:192.168.1.102

这是在服务器端测试的截图
图片说明
图片说明

相关代码:

客户端代码:
user='Wxs'
title='E-Quoter'
ip='116.62.149.45'
port=random.randint(5000,25000)
Serverport=12319
################################################################################
# 聊天客户端主程序
def chatroommain():
    global flag
    if pingInternet():
        print 'Internet connecting success...'
        try:
            tryping()
            if flag==0:
                print 'Connect with Server success...'
                try:
                    roomUI()
                    print 'Chatroom is success...'
                except:
                    print '\nError:Chatroom is faild!'
        except:
            print '\nError:Ping Server is faild!'
    else:
        print '\nError:Please check your Internet is online!'
def roomUI():
    global flag
    if flag==0:
        app=wx.App()
        chatroom=Chatroom(None)
        chatroom.Show()
        app.MainLoop()
        del app
#########################################################################################
# 服务器注册
def pingInternet():# 客户端联网测试
    ip='www.baidu.com'
    cmd='ping %s'%ip
    backinfo=os.system(cmd)
    if backinfo:
        return False
    else:
        return True
def stopThreads():# 关闭线程
    try:
        socket.close()
    except:
        pass
def tryping():# 尝试连接服务器
    global Flag,flag
    Flag=0
    flag=0
    try:
        socket=connectServer()
        time=timemannager()
        try:
            socket.start()
            try:
                if Flag==0:
                    time.start()
                    time.join()
                    if flag==1:
                        print 'Error:Connect with Server is Faild!'
                    try:
                        stopThreads()
                        print 'Threads closing success...'
                    except:
                        flag=1
                        Flag=1
                        print 'Error:Threads closing Faild!'
            except:
                Flag=1
                flag=1
                print 'Error:Timemannager start faild!'
        except:
            Flag=1
            flag=1
            print 'Error:Socket Threads start faild!'
    except:
        flag=1
        Flag=1
        pass
    if flag==1:
        return False
class connectServer(threading.Thread):# 创建连接线程
    def __init__(self):
        threading.Thread.__init__(self)
        self.threadName='connectServer'
        self.exitFlag=threading.Event()
    def run(self):
        global Flag,user,ip,port,flag,Serverport
        try:
            sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind(('0.0.0.0',port))
            print 'Socket is ready...'
            try:
                hostname = socket.gethostname()
                hostip=socket.gethostbyname(hostname)
                msg='pingServer#'+str(user)
                sock.sendto(msg,('172.16.173.52',Serverport))
                print 'ping msg has been sent...'
                Flag=0
                while Flag==0:
                    flag=1
                    print 'Waiting for pingback...'
                    data,addr=sock.recvfrom(1024)
                    print 'here'
                    if data=='0':
                        Flag=1
                        flag=1
                        print 'Error:Has the same name online!'
                    elif data=='1':
                        Flag=1
                        flag=0
                        print 'Connect with Server is ready...'
                    elif data==None:
                        Flag=1
                        flag=1
                        print 'Connect with Server is faild!'
                    time.sleep(0.2)
            except:
                flag=1
                print 'Error:Sent ping msg is faild!'
        except:
            flag=1
            print 'Error:Socket is faild to prepare!'
    def close():
        sock.close()
class timemannager(threading.Thread):# 超时判定线程
    def __init__(self):
        threading.Thread.__init__(self)
        self.threadName='timemannager'
        self.exitFlag=threading.Event()
    def run(self):
        global Flag,flag
        time.sleep(3)
        Flag=1
    def close():
        stop.close()
 服务器端代码:
 class sock(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.threadName='recive'
        self.exitFlag=threading.Event()
    def run(self):
        global port,Flag,file_output,clientipdic,flag,clientportdic
        try:
            sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM,socket.IPPROTO_UDP)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind(('0.0.0.0',port))
            print '\nReciving thread is ready...'
            try:
                while Flag==0:
                    print '\nWaiting for new message...'
                    data,addr=sock.recvfrom(3096)
                    msg=data
                    clientip=addr[0]
                    clientport=addr[1]
                    try:
                        check=msg[:msg.index('#')]
                        name=msg[msg.index('#')+1:]
                        if check=='pingServer':
                            print 'New climent connecting! Analyzing...'
                            if name in clientipdic:
                                msg='0'
                                sock.sendto(msg,addr)
                            else:
                                print 'new client logon!'
                                for each in clientipdic:
                                    head=name
                                    back='已登录!'
                                    backmsg=name+back
                                    address=str(clientipdic[each])
                                    sock.sendto(msg,(address,int(clientportdic[each])))
                                msg='1'
                                sock.sendto(msg,(clientip,clientport))
                                clientipdic[name]=clientip
                                clientportdic[name]=str(clientport)
                    except:
                        newmsg=data
                        for each in clientipdic:
                            address=str(clientipdic[each])
                            sock.sendto(newmsg,(address,int(clientportdic[each])))
                    time.sleep(0.1)
            except:
                flag=1
                Flag=1
                print 'Error:Recive threading sets faild!'
                file_output.write('Error:Recive threading sets faild!')
        except:
            flag=1
            Flag=1
            print 'Error:socket setting is Faild!'
            file_output.write('Error:socket setting is Faild!')
    def close():
        socket.close()

1个回答

第一,你客户端socket应该不用bind吧。。
另外,照你的描述,应该还是防火墙阻止 的可能性比较大,你试着把防火墙完全关了测试一下,看行不行

YoungQuant
YoungQuant 另外,为什么在服务器上跑测试的时候,客户端一连接成功服务器就掉线了呢?在我自己的电脑上测试也会有相应的问题,但一般都能连接5-10个之后才会崩溃,请问是内存溢出的问题吗?
2 年多之前 回复
YoungQuant
YoungQuant 可是如果客户端不bind的话,我客户端怎么接受服务器转发的信息呢?我现在试试把我电脑的防火墙也关了试试
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!