I am trying to create a client server program where I'm sending an object from client and receiving the object at the server continuously in the while loop.
Client code:
ObjectOutputStream oos = null;
while(true){
WorkerMessageToMaster message = new WorkerMessageToMaster(WorkerTasksStatus.getTaskStatusMap(), WorkerTasksStatus.getTaskStatusReduce());
oos = new ObjectOutputStream(taskManagerSocket.getOutputStream());
oos.writeObject(message);
oos.flush();
Thread.sleep(1000);
}
Server code:
ObjectInputStream ois = null;
while (true ) {
ois = new ObjectInputStream(clientSocket.getInputStream());
WorkerMessageToMaster taskMapObject = (WorkerMessageToMaster)ois.readObject();
System.out.println("Connection from: "+clientSocket.getInetAddress().getHostAddress().toString());
}
When I try to run this code on my local system It runs normally, but when I try to run the client and server in different machines(different Ips) I get the following error.
java.io.StreamCorruptedException: invalid stream header: 74000432
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at master.MasterAnalyzer.heartBeat(MasterAnalyzer.java:58)
at master.MasterAnalyzer.run(MasterAnalyzer.java:80)
at java.lang.Thread.run(Thread.java:745)
I am confused at this erratic behavior as Im sending the stream through the client socket established with the server in a while loop and receiving it on the same socket connection accepted at the server and It seems to be working fine on local host.
Thank you for your help
You create a new stream for each object. Only create one output and one input stream. Object streams send header data which is maybe corrupted when you create a new stream.
Related
Getting Exception in thread "main" java.io.EOFException
at the last line of code, why? and how to fix... Thanks.
String ip = "XXX.XX.XX.XXX";
int port = XXXXX;
Socket socket = null;
System.out.println("in function");
socket = new Socket(ip, port);
System.out.println("in function - After Socket");
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
System.out.println("in function - After ObjectOutputStream");
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
The peer has already closed the socket. Possibly you sent it something it didn't understand. Unless it is a Java program that also uses object inout and output streams, it definitely won't understand.
I'm trying to send an object over the network to another computer (or the same computer) and then have said computer send an object back.
On the sending computer, I send the object and receive the returned object:
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(object);
Object returnedObject;
socket.setSoTimeout(timeout);
try (ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {
returnedObject = (Object) ois.readObject();
}
return returnedObject;
On the receiving computer, I receive the object:
Object object;
socket.setSoTimeout(timeout);
try (ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {
object = (Object) ois.readObject();
}
return object;
and then send an object back:
socket.setSoTimeout(timeout);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(object);
The error I get back is:
SEVERE: null java.net.SocketException: Socket is closed at
java.net.Socket.setSoTimeout(Socket.java:1137) at
and it occurs while attempting to send an object back on the receiving computer.
The socket on the sending computer is using the same address and port as the socket on the receiving computer.
This exception means that you closed the socket and then continued to use it. Specifically, you closed the ObjectInputStream at the end of the try-with-resources block where it is declared. That closes the other stream of the socket and the socket itself.
Don't use new object streams per transfer. Use the same ones for the life of the socket, at both ends.
You are using a very small-scoped try-with-resources:
try (ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {
returnedObject = (Object) ois.readObject();
}
This code is interpreted as:
Get the input stream and build an ObjectInputStream around it.
Read an object from the object stream
Close the ObjectInputStream.
When you close the ObjectInputStream it automatically closes the InputStream that backs it, which is the socket's input stream. And the documentation of getInputStream says:
Closing the returned InputStream will close the associated socket.
You should make sure the try-with-resources has a bigger scope that covers the entire lifetime of the socket, or avoid using try-with-resources and make sure you close the ObjecInputStream properly when you are done with it or when there is an error.
I've got a java tomcat servlet with a doPost method and a webpage running on that server. I set up a very basic test with a html button on my web page, that when pressed calls the doPost method. What i would like to know is how i would do this from the arduino?
I tried
client.println("POST /Project/index.html?acctID=1234567 HTTP/1.0");
as a means to call the doPost method and pass it an "acctID", this however doesn't call that doPost method. I don't have a very good understanding of how to send get/post from ethernetclient in arduino (i imagine it doesn't work quite how i'm thinking it does), so hopefully someone can help me understand, thanks!
In response to comment:
So i've continued to play with this and what i've done at this point is I've made a server on my java side that sends data (via sockets) to an arduino server that i've made. What i'm trying to do now is send a simple, small piece of data back to my java program/servlet. What i've tried so far that is the code posted above on the arduino side, and on the java side i've got a simple server:
private static void listenForArduino() throws IOException, ClassNotFoundException {
//static ServerSocket variable
ServerSocket server;
//socket server port on which it will listen
int port = 9876;
//create the socket server object
server = new ServerSocket(port);
//keep listens indefinitely until receives 'exit' call or program terminates
while(true){
System.out.println("Waiting for client request...");
//creating socket and waiting for client connection
Socket socket = server.accept();
//read from socket to ObjectInputStream object
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//convert ObjectInputStream object to String
String message = (String) ois.readObject();
System.out.println("Message Received: " + message);
//create ObjectOutputStream object
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
//close resources
ois.close();
oos.close();
socket.close();
//terminate the server if client sends exit request
if(message.equalsIgnoreCase("exit")) break;
}
System.out.println("Shutting down Socket server!!");
//close the ServerSocket object
server.close();
}
I never receive a response, it stalls on this line
server = new ServerSocket(port);
My arduino code (that pertains to this):
if (client.connect(ip, 9876)) {
Serial.println("connected");
client.println("POST /NFCPaymentProject/index.html?acctID=1234567 HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
I have the code below:
while (true)
{
lengthInput = instr.readByte();
// Other code
}
The thing is that I'm using a client to send information to the socket, but after it finishes I got EOF Exception and it brokes the thread, what I need is to manages this and dont stop the thread, because I need to send more information and be able to read it.
Thanks in advance for your help.
I guess the problem is related to your socket initialization. You probably need to check if your client socket indeed successfully create a socket and bind to a specified port. You may also check your client really send data to the outstream and flush to the server side. I have a small project on Android emulators with socket communication. Both my client and serve extends from Java Thread class. Maybe you can gain some idea seeing my code below.
The client side
try {
socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),
Integer.parseInt(remote_port));//note we must keep the addr#10.0.2.2
// write out
out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(out_msg);
out.flush();
// read in
in = new ObjectInputStream(socket.getInputStream());
socket.setSoTimeout(1000);
in_msg.set_message((Message)in.readObject());
// close all
out.close();
in.close();
socket.close();
return true;
}catch(InterruptedIOException E){}
The server side
while (true) {
try {
// read in message
Socket ClientSocket = serverSocket.accept();
Message out_msg = new Message();
// read in message
ObjectInputStream in = new ObjectInputStream(ClientSocket.getInputStream());
Message in_msg = (Message) in.readObject();
//Log.d(TAG, "recv" + " content:" + in_msg.msg2Str());
message_process(in_msg, out_msg);
// write out message
ObjectOutputStream out = new ObjectOutputStream(ClientSocket.getOutputStream());
out.writeObject(out_msg);
out.flush();
} catch(Exception E){}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java.net.SocketException: Software caused connection abort: recv failed
I'm trying to write a client server pair where the connection is live all day and the client waits for the server to send a message. The steps are:
Server opens port and listens for connection
Client connects in and waits for data
Some time later (maybe hours) the server sends data to the client
Client processes data and returns it to the server
Repeat steps 3 and 4
I can get steps 1-4 working, however if I try to repeat step 3 I get the error in the title.
This is my method on the client side:
private static void waitForInput(SSLSocket sslsocket) throws IOException {
do {
try {
ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
Object o = (Object) (ois.readObject());
// excluded code to process data
oos.flush();
oos.writeObject(o);
oos.reset();
}
catch(ClassNotFoundException e){
System.err.println(e.getMessage());
}
} while ( true );
}
The code fails on the 4th line, The first time around it blocks and waits until I get the next bit of data, but it doesn't work twice. What am I doing wrong?
Connection abort IOException is thrown when you are waiting to read from a socket that has been closed at the other end, check to see your server side code , if you are not accidentally closing the socket.
Also the server side code could be uploaded for deeper analysis, also try posting the stack trace, it will help analyzing.
You could move the declaration for ois and oos out of the do ... while loop, since there is no need to redeclare them everytime, might need a try ... catch around that.
private static void waitForInput(SSLSocket sslsocket) throws IOException {
ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
do {
try {
Object o = (Object) (ois.readObject());
// excluded code to process data
oos.writeObject(o);
oos.flush();
}
catch(ClassNotFoundException e){
System.err.println(e.getMessage());
}
} while ( true );
}
And I have removed the oos.reset(); and moved the oos.flush();
I believe that the problem is the oos.reset(); and i would never reset a connection that is supposed to be persistent for hours, or, at least part of it.
Besides there is already a ois for that connection and you don't need two of them.