YoungQuant 2017-05-29 19:06 采纳率: 0%
浏览 1169
已结题

Python 服务器设置问题

问题:
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条回答

  • 搬运工木木 2017-05-31 05:50
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题