![](/img/trans.png)
[英]Python(Server) receiving Data from Java(client) In separate lines.. (TCP)
[英]Python Server and Java Client: Weird behaviour receiving data
我正在使用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!
即使操作碼似乎是一個字符串並且是正確的值,該程序仍無法正確輸入If語句
誰能告訴我我在做什么錯?
謝謝
好吧,我解決了我的問題。 在我的Java代碼中使用WriteBytes效果更好!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.