3

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
asked Oct 29, 2018 at 9:40

2 Answers 2

4

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")) {
answered Oct 29, 2018 at 10:04
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. i thought that the range method included the loop limit. i put your suggestions in my code and it worked like a charm! Thanks!
1

Well, basically you have answer in you logs

Exception in thread "Thread-0" java.lang.NullPointerException
 at Jserver.run(Jserver.java:28)
received: null

So fromClient variable is null. You can add simple check for it to avoid such kind of exceptions and skip loop continuation.

if (fromClient == null) {
 continue;
}
answered Oct 29, 2018 at 10:07

1 Comment

@Ferrybig answered it perfectly, so you can ignore this one :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.