Python client Java server server receives null

Nightfall :

As the name suggest the client sends null to the server. i cant figure out why

Python client

import socket

HOST = "localhost"
PORT = 5000

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))

dict = {
    0: "doug",
    1: "korg",
    2: "stefan",

}
for x in range(0, 3):
    if x == 3:
        sock.sendall("Bye\n")
        print(sock.recv(1024))
    sock.sendall(b"{}\n".format(dict.get(x)))
    print(dict.get(x))
    print(sock.recv(1024))

Java server:

    public void run() {
        String fromClient;
        String toClient;
        try {
            ServerSocket server = new ServerSocket(5000);
            System.out.println("wait for connection on port 5000");

            boolean run = true;
            Socket client = server.accept();

            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter out = new PrintWriter(client.getOutputStream(), true);


            while (run) {
                System.out.println("got connection on port 5000");
                fromClient = in.readLine();
                System.out.println("received: " + fromClient);


                if (fromClient.equals("Bye")) {
                    toClient = "Aight later man";
                    System.out.println("send eyB");
                    out.println(toClient);
                    client.close();
                    System.out.println("socket closed");
                    break;
                }
                out.println("cool next User Please");
            }
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

while the first 3 strings are being send from the client to the server and the server responds back, when Bye is send the server receives null and throws a nullpointerexception.

my code is based on this post. Communication between python client and java server

the problem exist in String as ByteString form.

output of server:

wait for connection on port 5000
got connection on port 5000
received: doug
got connection on port 5000
received: korg
got connection on port 5000
received: stefan
got connection on port 5000
Exception in thread "Thread-0" java.lang.NullPointerException
    at Jserver.run(Jserver.java:28)
received: null

Process finished with exit code 0

output of client:

doug
cool next User Please

korg
cool next User Please

stefan
cool next User Please
Ferrybig :

Your issue is caused in the python code, which never sends the "bye" command.

The bug in the python code:

Your python code loops from 0 (inclusive) to 3 (exclusive), since the 3 is excluded, the code never goes into the "bye" block, and never sends this. (print x to confirm this)

Place the "bye" code after the loop.

for x in range(0, 3):
    sock.sendall(b"{}\n".format(dict.get(x)))
    print(dict.get(x))
    print(sock.recv(1024))
sock.sendall("Bye\n")
print(sock.recv(1024))

The bug in the Java code

Yes, there is also a bug in the Java code, mainly that it does not deal with stream closures properly.

When the Python program is being shutdown at the end of the code, the Java program will receive a clean exit, this is an exception-less way of closing the socket, and will result in readLine returning a null value.

This null then causes the following exception:

Exception in thread "Thread-0" java.lang.NullPointerException

Since seeing an null value means the socket is closed, you can also thread this as either a proper "bye" response or as a error condition.

            if (fromClient == null || fromClient.equals("Bye")) {

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=90916&siteId=1