問題
我有一個偵聽埠 55555 的 TCP 服務器 (Java)。一旦客戶端連接到它,它就會偵聽資料包,如果資料包正確,則打開一個新套接字并將新埠發送到客戶端。我知道服務器端代碼可以作業,因為我撰寫了一個可以正常作業的小型 java 客戶端,但是我的 python 客戶端只能連接(當它發送資料包時,服務器似乎沒有對其作出反應)。
我希望有人知道如何讓它作業,并提前感謝您的幫助!
我還嘗試過什么
- 將“\n”“\r\n”附加到python資料包:相同的結果
- 搜索 Stackoverflow,一個多小時沒有找到任何東西
代碼
服務器
// Server.java
private void run() {
while (true) {
try {
ssocket = new ServerSocket(config.defaultPort);
System.out.println("Socket opened on port " config.defaultPort);
Socket socket = ssocket.accept();
System.out.println("Connection established");
DataInputStream dis = new DataInputStream(socket.getInputStream());
String msg = dis.readUTF();
System.out.println("Packet received");
ssocket.close();
if (msg.startsWith(config.specialChars.get("initConnectionServer").toString())) {
System.out.println("Connection initiated by another server");
// Connection was initiated by another server
// parse message
String[] parts = msg.substring(1).split(config.specialChars.get("listSeparator").toString());
String name = parts[0];
String passwd = parts[1];
/// check name and password
if (BCrypt.checkpw(passwd, config.ServerPW) && name.equals(config.ServerName)) {
System.out.println("Password and Name match our Server Network");
// add new server worker
ServerWorkerServer t = new ServerWorkerServer();
t.start();
workers.add(t);
// send new port
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("" config.specialChars.get("newPortResponse") t.port);
dos.flush();
System.out.println("new port sent back: " t.port);
socket.close();
ssocket.close();
} else {
if (name.equals(config.ServerName)) {
System.out.println("Password does not match our server network");
System.out.println(passwd);
} else {
System.out.println("Name does not match our server network");
System.out.println(name);
}
}
} else {
System.out.println("Message is not valid");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
java中的測驗客戶端:
// client.java
public static void main(String[] args) {
try{
Socket s = new Socket("localhost",55555);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("\uE001OG-ServerNet\uE000password");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
在 python 中測驗客戶端:
# client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 55555))
s.send(b"\uE001OG-ServerNet\uE000password")
輸出
所有輸出都來自 server.java 由各自的客戶端連接后
客戶端.java
Socket opened on port 55555
Connection established
Packet received
Connection initiated by another server
Password and Name match our Server Network
new port sent back: 29517
客戶端.py
Socket opened on port 55555
Connection established
筆記
config.specialChars.get("initConnectionServer") 回傳 \uE001
config.specialChars.get("listSeparator") 回傳 \uE000
config.specialChars.get("newPortResponse") 回傳 \uE002
uj5u.com熱心網友回復:
這兩個陳述句不傳輸相同的資料:
Java: out.writeUTF("\uE001OG-ServerNet\uE000password");
Python: s.send(b"\uE001OG-ServerNet\uE000password")
Java 陳述句將給定的字串解釋為 Unicode。它將字串轉換為 UTF-8,從而傳輸位元組序列:
\xee\x80\x81OG-ServerNet\xee\x80\x80password
Python 陳述句將給定的字串解釋為位元組序列,因為它是用b"..."語法顯式宣告的。這意味著它將傳輸以下位元組序列:
\\uE001OG-ServerNet\\uE000password
要傳輸與 Java 代碼相同的內容,請勿使用b"..."but "...".encode()。這會將字串解釋為 Unicode 并將其轉換為 UTF-8 位元組序列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/412095.html
標籤:
上一篇:Multiprocessing FlaskOSError:[Errno48]Addressalreadyinuse/OSError:[Errno38]非套接字上的套接字操作
