简体   繁体   English

TCP 服务器 (Java) 未从 TCP 客户端 (Python) 接收数据包

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

Problem问题

I have a TCP Server (Java) that listens on port 55555. As soon as a client connects to it, it listens for a packet and if the packet is correct, opens a new socket and sends the new port to the client.我有一个侦听端口 55555 的 TCP 服务器 (Java)。一旦客户端连接到它,它就会侦听数据包,如果数据包正确,则打开一个新套接字并将新端口发送到客户端。 I know that the server side code can work, because I have written a small java client that works fine with it, but my python client can only connect (when it sends a packet the server doesn't seem to react to it).我知道服务器端代码可以工作,因为我编写了一个可以正常工作的小型 java 客户端,但是我的 python 客户端只能连接(当它发送数据包时,服务器似乎没有对其作出反应)。

I hope somebody has an idea how to get this working and thanks in advance for your help!我希望有人知道如何让它工作,并提前感谢您的帮助!

What else I have tried我还尝试过什么

  1. appending "\\n" "\\r\\n" to the python packet: same result将“\\n”“\\r\\n”附加到python数据包:相同的结果
  2. searching Stackoverflow, haven't found anything in over an hour搜索 Stackoverflow,一个多小时没有找到任何东西

Code代码

The Server服务器

// 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);
        }
    }
}

test client in java: 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);}
}

test client in python:在 python 中测试客户端:

# client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 55555))
s.send(b"\uE001OG-ServerNet\uE000password")

Output输出

all output is from server.java upon connection by the respective client所有输出都来自 server.java 由各自的客户端连接后

client.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

client.py客户端.py

Socket opened on port 55555
Connection established

Notes笔记

config.specialChars.get("initConnectionServer") returns \ config.specialChars.get("initConnectionServer")返回\

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

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

These two statements do not transmit the same data:这两个语句不传输相同的数据:

 Java:   out.writeUTF("\uE001OG-ServerNet\uE000password");
 Python: s.send(b"\uE001OG-ServerNet\uE000password")

The Java statement interprets the given string as Unicode. Java 语句将给定的字符串解释为 Unicode。 It will convert the string to UTF-8 and will thus transmit the byte sequence:它将字符串转换为 UTF-8,从而传输字节序列:

 \xee\x80\x81OG-ServerNet\xee\x80\x80password

The Python statement instead interprets the given string as a byte sequence, because it was explicitly declared as such with the b"..." syntax. Python 语句将给定的字符串解释为字节序列,因为它是使用b"..."语法显式声明的。 This means it will transmit the following byte sequence:这意味着它将传输以下字节序列:

 \\uE001OG-ServerNet\\uE000password

To transmit the same thing as the Java code do not use b"..." but "...".encode() .要传输与 Java 代码相同的内容,请不要使用b"..."而是"...".encode() This will interpret the string as Unicode and convert it to a UTF-8 byte sequence.这会将字符串解释为 Unicode 并将其转换为 UTF-8 字节序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM