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
Your issue is caused in the python code, which never sends the "bye" command.
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))
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")) {
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.