Reputation: 13
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. 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).
I hope somebody has an idea how to get this working and thanks in advance for your help!
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:
// 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:
# client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 55555))
s.send(b"\uE001OG-ServerNet\uE000password")
all output is from server.java upon connection by the respective client
client.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
Socket opened on port 55555
Connection established
config.specialChars.get("initConnectionServer")
returns \uE001
config.specialChars.get("listSeparator")
returns \uE000
config.specialChars.get("newPortResponse")
returns \uE002
Upvotes: 0
Views: 126
Reputation: 123433
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. It will convert the string to UTF-8 and will thus transmit the byte sequence:
\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. 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()
. This will interpret the string as Unicode and convert it to a UTF-8 byte sequence.
Upvotes: 0