Python服务器和Java客户端:接收数据的奇怪行为

时间:2014-12-04 13:07:58

标签: java android python sockets

我使用python创建服务器,而Java(Android)创建客户端。我面临的问题是从一个到另一个正确地获取数据。

我让Java客户端尝试这样的登录:

SettingsManager settingsman = SettingsManager.getInstance(params[0]);
                int port = Integer.parseInt(settingsman.getPort());
                String ip = settingsman.getIp();
                server = new Socket(ip, port);
                input = new DataInputStream(server.getInputStream());
                output = new DataOutputStream(server.getOutputStream());
                output.writeChars(LOGIN_REQ);
                output.writeChars(settingsman.getUserName());
                output.writeChars(settingsman.getPassword());
                String token = Integer.toString(input.read());
                //Check if the login has failed
                if(token == "0"){
                    token = null;
                }
                return token;

Python服务器就像这样接收它:

    opcode = c.recv(2)
    opcode  = opcode.decode('utf-8')
    print("OPCODE:" + opcode + str(type(opcode)))
    if(opcode == "0"):
        "Login action"
        print("STARTING TO RECV!")
        login = c.recv(1024).decode('utf-8')
        print("RECEIVED LOGIN: " + login)
        password = c.recv(1024).decode('utf-8')
        print("PASSWORD: " + password)
        print("USERNAME: " + username)
        setman = SettingsManager()
        setuser = setman.seccam_user()
        setpass = setman.seccam_password()

        if login == setuser & password == setpass:
            print("Login is good!")
            """Generate random token and save it to the settings file"""
    elif(opcode == "1"):
        pass
    else:
        print("OTHER!")

这是输出:

OPCODE:0<class 'str'>
OTHER!

即使Opcode似乎是一个字符串并且是正确的值,程序也不会正确输入If语句

谁能告诉我我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

好吧,我解决了我的问题。在我的Java代码中使用WriteBytes工作得更好!