Python client Java server server receives null - java

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.
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")) {

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;
}

Related

TCP Socket with Python server and Java client

I am trying to exchange some string data between a Python Server (ideally, a Raspberry Pi with some device connected through GPIO) and a Java Client (again, the main target would be an Android app). The following code, anyway, is running on a standard local PC.
This is the code for the server, taken (and slightly modified) from here:
import socketserver
import datetime
class MyTCPHandler(socketserver.StreamRequestHandler):
def handle(self):
now = datetime.datetime.now()
answer = now
self.data = self.rfile.readline().strip()
print("Read!")
if str(self.data) == 'date':
answer = now.date()
elif str(self.data) == 'time':
answer = now.time()
self.wfile.write((str(answer)+"\n").encode('utf-8'))
print("Sent!")
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
print("Server is running on {}, port {}".format(HOST, PORT))
server.serve_forever()
The Java client is the following:
public class SocketTest {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 9999);
PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out.println("date".getBytes());
String resp = in.readLine();
System.out.println("Received: " + resp);
} catch (IOException ex) {
Logger.getLogger(SocketTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
No exception is thrown whatsoever, it just gets stuck waiting for the response on the client side, and I can never see the "Read!" message on the server side.
The "date".getBytes() comes from somewhere on the net where I found that the Python sockets expect bytes (UTF-8), but in Java I'm sending strings directly, so it might be wrong.
Any help will be appreciated!
Turnes out, it was a flushing problem.
Apparently, the buffer is not flushed when the end of line is reached (which is how I was expecting it to behave).
Adding a simple out.flush() solved the problem.

Unable to send anything from Python server to Java client

I've set up a Raspberry Pi 3 and I want to make a program that sends data whenever a button is pushed on my breadboard. I have a Python server running on my RPi, and a Java client running on my Windows laptop. However, whenever I send data to my Java client, it receives the data, and then for some reason, the RPi server closes the program due to "broken pipe". However this cannot be true, because my Java program receives data from the Pi! The Java program then closes due to the Pi server closing. But from what I've read online, Python's "error 32: broken pipe" is triggered when the remote socket closes prematurely!
What's going on here? Why can't I keep my server running?
(PS: The data that my Java program receives is wrong, but it receives data nonetheless. I send "1\n", and I receive null.)
Here is the code for my RPi server program:
import RPi.GPIO as GPIO
from time import sleep
import atexit
import socket
import sys
GPIO.setmode(GPIO.BOARD)
GPIO.setup(5, GPIO.IN)
GPIO.setup(7, GPIO.OUT)
def cleanup():
print("Goodbye.")
s.close()
GPIO.cleanup()
atexit.register(cleanup)
THRESHOLD= 0.3
host= sys.argv[1]
port= 42844
length= 0
def displayDot():
GPIO.output(7,True)
sleep(0.2)
GPIO.output(7,False)
def displayDash():
GPIO.output(7,True)
sleep(0.5)
GPIO.output(7,False)
try:
print("Initializing connection...")
s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serverAddress= (host, 42844)
s.bind(serverAddress)
print("Connection initialized!")
print("Waiting for client...")
s.listen(1) #Puts the server socket into server mode
client, address= s.accept()
print(address)
while True:
if not GPIO.input(5):
length+= 0.1
GPIO.output(7,True)
s.sendall('1\n')
print("HELLO??")
else:
if length!=0:
if length>=THRESHOLD:
print("Dash") #displayDash()
else:
print("Dot") #displayDot()
s.sendall('0')
length= 0
GPIO.output(7,False)
except KeyboardInterrupt:
print("\nScript Exited.")
cleanup();
Here's the code for the Java client program:
import java.net.*;
import java.io.*;
public class MorseClient{
public static void main(String[] args) throws IOException{
String hostname= null; //Initialize
int portNumber= 0; //Initialize
try {
hostname= args[0];
portNumber= Integer.parseInt(args[1]);
}
catch(ArrayIndexOutOfBoundsException aiobe) {
System.err.println("ERROR. Please specify server address, and port number, respectively");
System.exit(1);
}
Socket redoSocket;
long initTime;
try(
Socket echoSocket= new Socket(hostname, portNumber);
PrintWriter out= new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in= new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdin= new BufferedReader(new InputStreamReader(System.in));
){
redoSocket= echoSocket;
System.out.println("Connection made!");
String userInput= "";
//Order of priority
//Connection time= 0
//Latency= 0
//Bandwidth= 1
redoSocket.setPerformancePreferences(0,0,1);
//Optimizes reliability
redoSocket.setTrafficClass(0x04);
echoSocket.setKeepAlive(true);
String returned= "";
while(true){
returned= in.readLine();
System.out.println(returned);
if(!(returned.isEmpty())){
System.out.println(returned);
System.out.println("YEP");
}
System.out.println(returned);
if(returned==null){
System.out.println("HAHA");
System.out.println("Attempting to reconnect...");
redoSocket= new Socket(hostname,portNumber);
System.out.println(redoSocket.isConnected());
}
}
}
catch(Exception e){
if(e instanceof ConnectException || e instanceof SocketException || e instanceof NullPointerException)
System.err.println("Connection closed by server");
else
System.err.println(e.toString());
}
}
}
The output for the Pi server is:
python ServerMorse.py 192.168.1.101
Initializing connection...
Connection initialized!
Waiting for client...
('192.168.1.14', 58067)
('192.168.1.14', 58067)
Traceback (most recent call last):
File "ServerMorse.py", in <module>
s.sendall('1\n')
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 32] Broken pipe
Goodbye.
And the output for the Java client:
java MorseClient 192.168.1.101 42844
Connection made!
null
Connection closed by server
Good lord, why are you writing a server with sockets? Use Flask.
http://flask.pocoo.org/
Also, pretty sure s should not be sending all. It should be like this:
conn, addr = server.accept()
conn.sendall(.... # <- this is what sends
Here is some sample code from a server I wrote with sockets once..might be useful:
def server():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
address = ('127.0.0.1', 5020)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(address)
server.listen(1)
conn, addr = server.accept()
...
...
conn.sendall(response_ok(some_stuff))
...
conn.close()
(response_ok is a function I wrote)

Unable to correct StreamCorruptedException in full duplex code [duplicate]

This question already has an answer here:
StreamCorruptedException: invalid type code: AC
(1 answer)
Closed 6 years ago.
I am trying to implement a full duplex TCP connection between two applications.
My gist of the idea is to do the following :
Client
socket.connect(ServerAddress,Timeout);
socketArray.add(socket);
outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.writeObject(message);
outputStream.flush();
/** Wait for reply**/
inputStream = new ObjectInputStream(socket.getInputStream());
message = (Message)inputStream.readObject();
if (message.type == X) {
replyToServer(message,socketArray);
}
replyToServer(Message,socketArray) {
for(Socket socket : socketArray) {
outputStream = new ObjectOutputStream(socket.getOutputStream()); // **getting streamCorruptedException** here
outputStream.writeObject(message);
outputStream.flush();
}
}
Server
Here I listen to the incoming connections, and dependent on the message type, I want to reply to it after changing some parameters of the message.
inputStream = new ObjectInputStream(clientSocket.getInputStream()); //notice inputStream and outputStream are created only once here, maybe that is the issue ?
outputStream = new ObjectOutputStream(clientSocket.getOutputStream());
while ( (message = (Message)inputStream.readObject() ) != null) {
if (message.type == Y) {
message.type = X ; //notice this is used in Client code
outputStream.writeObject(message);
outputStream.flush();
} else if (message.type == X ) {
// don't send anything to client, we are done processing this message
}
}
After looking around in google, I got some idea that ObjectInputStream and ObjectOutputStream must be somehow synchronized. But I did not get a concrete understanding. If someone can point out mistake in my code, it will be helpful.
Code flow :
Client sends a message to server with message type Y, asking for information.
Server sees the type Y, sets information in the object, changes message type to X, and replies to client.
Client receives replies from all others.
Note : There are 5 applications running both server and client. Hence the client will wait until it gets replies from all the others before proceeding.
Now client must send the received information to all 5 applications, and does that in replyToServer method. The exception occurs there.
PS : This is pseudo code, and if any more details are required to understand code flow , please let me know in the comments.
Actual stack trace
err: java.io.StreamCorruptedException
err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1530)
err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1483)
.....`
outputStream = new ObjectOutputStream(socket.getOutputStream()); // **getting streamCorruptedException** here
Impossible. You are getting StreamCorruptedException: invalid type code AC at the other end when you execute new ObjectInputStream(...).
The reason is that you've created multiple ObjectOutputStreams for the same socket, but without creating correspondingly multiple ObjectInputStreams at the peer. You should use the same object input and output streams for the life of the socket, at both ends.
while ( (message = (Message)inputStream.readObject() ) != null) {
This isn't valid either. ObjectInputStream.readObject() doesn't return null at end of stream. It can return null any time you wrote a null. The loop should terminate when EOFException is thrown.

Why TCP/IP server with listening port in java not working

I have written a code in java to interface my computer with a transmitter a transmitter device, with a communication board already implemented and ready to connect via TCP/IP to any server with a specific address IP (say 192.168.2.2) and listening to a specific port number (say 4000).
I followed the exact strep how to create a server side application in Java offering a that listening port, so that I can connect to that transmitter.
I don't understand why when I try to debug the code, it blocks a the line clientSocket = serverSocket.accept(), and throws a timeout exception.
Could someone help me find out where the error might be in my code?
Any help would be greatly appreciated.
Thanks.
Here is the code:
public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Declares server and client socket, as well as the input and the output stream
ServerSocket serverSocket = null;
Socket clientSocket = null;
PrintWriter out;
//BufferedReader in;
BufferedReader in;
try{
InetAddress addr = InetAddress.getByName("192.168.2.2");
//Opens a server socket on port 4000
serverSocket = new ServerSocket(4000) ;
//Sets the timeout
serverSocket.setSoTimeout(30000);
System.out.println("Server has connected");
//Create a connection to server
System.out.println("Server listening connection from client ....");
//Listens and waits for client's connection to the server
clientSocket = serverSocket.accept();
// Creates input and output streams to socket
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Reads response from socket
while((in.readLine())!= null ){
System.out.println ( in.readLine() );
}
System.out.println ( "Closing connection ....");
//Terminates connection
clientSocket.close();
serverSocket.close();
System.out.println("Connecton successfully closed");
}
catch(IOException e){
System.out.println(e);
}
}
}
Could someone help me find out where the error might be in my code?
There is no error in your code that could cause this problem. Clearly you haven't configured the device to connect to this server correctly, or the device isn't running, or it isn't connecting, or there is a firewall in the way. Investigate that.
However:
InetAddress addr = InetAddress.getByName("192.168.2.2");
What is this for? It isn't used.
System.out.println("Server has connected");
This is simply not true. The server hasn't connected. At this point all it has done is create a listening socket.
while((in.readLine())!= null ){
Here you are reading a line and throwing it away.
System.out.println ( in.readLine() );
Here you are printing every second line, having thrown every odd line away. The correct way to write this loop is:
String line;
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
Note also that this server will service exactly one client and then exit. There should be a loop around everything from accept() to clientSocket.close(), and if there are multiple devices it should start a new thread per accepted socket to handle the I/O.
You specified timeout of 30 seconds, didn't you? :
serverSocket.setSoTimeout(30000);
So after 30 seconds, no matter whether stopped in debugger or running, this will timeout and throw exception.

Java Communication Server Client with Socket

i'm having trouble with my client/server program in java . I'm able to communicate from my client to my server but when i'm broadcasting from the server to the client it's not working.
There is the part of my program that is not working :
Server :
while (true) {
Socket socket = server.accept();
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
out.write("Welcome to the server !");
out.flush();
}
Client ( running as a thread):
while(true){
try {
//s is the socket I get from the connection to the server
in = new BufferedReader (new InputStreamReader (s.getInputStream()));
String msg = in.readLine();
System.out.println(msg);
} catch (IOException ex) {
}
}
When I use my client programm I don't receive the message sent by the server . However when i use netcat on my terminal to establish the connection on the server, I got the message . I don't get it. Thanks
The client expects a complete line to be sent:
String msg = in.readLine();
It can only be sure the line is complete if it finds a line terminator character, or if the stream is closed. But the server doesn't send any EOL character, and doesn't close the stream either. So the client keeps waiting for the line to complete.

Categories