簡體   English   中英

TCP 服務器 (Java) 未從 TCP 客戶端 (Python) 接收數據包

[英]TCP Server (Java) not receiving Packets from TCP Client (Python)

問題

我有一個偵聽端口 55555 的 TCP 服務器 (Java)。一旦客戶端連接到它,它就會偵聽數據包,如果數據包正確,則打開一個新套接字並將新端口發送到客戶端。 我知道服務器端代碼可以工作,因為我編寫了一個可以正常工作的小型 java 客戶端,但是我的 python 客戶端只能連接(當它發送數據包時,服務器似乎沒有對其作出反應)。

我希望有人知道如何讓它工作,並提前感謝您的幫助!

我還嘗試過什么

  1. 將“\\n”“\\r\\n”附加到python數據包:相同的結果
  2. 搜索 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")返回\

config.specialChars.get("listSeparator")返回\

config.specialChars.get("newPortResponse")返回\

這兩個語句不傳輸相同的數據:

 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"..."而是"...".encode() 這會將字符串解釋為 Unicode 並將其轉換為 UTF-8 字節序列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM