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