java.io.EOFException when trying to perform java socket operations - java

I'm getting the following exception for the code included below that. This works fine when the while() loop is excluded. Why is this?
Oct 6, 2011 1:19:31 AM com.mytunes.server.ServerHandler run
SEVERE: null
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at com.mytunes.server.ServerHandler.run(ServerHandler.java:68)
Class ServerHandler:
public class ServerHandler extends Thread {
.
.
.
public ServerHandler(...){
...
}
public void run(){
try {
os = s.getOutputStream();
oos = new ObjectOutputStream(os);
is = s.getInputStream();
ois = new ObjectInputStream(is);
while(true){
msg = (Messenger) ois.readObject();
String methodType = msg.getKey();
//validating various data passed from the serialized object
if(methodType.equals("validateCard")){
} else if(methodType.equals("validatePIN")){
}
}
} catch (SQLException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} finally{
try {
ois.close();
is.close();
oos.close();
os.close();
s.close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Class Server
public class Server{
...
ServerSocket ss;
...
public static void main(String args[]){
Server server = new Server();
server.init();
}
public void init(){
try {
System.out.println("Server started...");
ss = new ServerSocket(port);
System.out.println("Listening on " + ss.getInetAddress() + ":" + ss.getLocalPort());
System.out.println("Waiting for clients...");
while(true){
Socket incoming_socket = ss.accept(); // returns a Socket connection object if received
new ServerHandler(...).start();
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

You keep trying to read objects forever, never breaking out of the loop. When the client closes the connection, the stream will run out of data, and the ObjectInputStream.readObject method will throw the exception you're seeing.
How many objects did you expect to be in the stream, and why are you reading past the end of them?

Related

EOFException when trying to pass the object through sockets

I am trying to pass an object through sockets, from the server to the client, what happens is that in some PCs it works well, and in others it does not. The object is updated indefinitely, so obviously I use an infinite loop to send the object indefinitely. When trying to execute the client sends the following error:
java -jar PC-CheckClient.jar 192.168.1.71 -console
Successfully established connection with server PC-CHECK
Starting... It may take several seconds
jul 11, 2019 1:34:31 AM com.he.pc.view.Main <init>
GRAVE: null
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.he.pc.view.Main.<init>(Main.java:39)
at com.he.pc.view.Main.main(Main.java:93)
This is the code sonde command the object from the server
private void sendData() {
//Declarar, instanciar e inicializar
ObjectOutputStream oos = null;
ServerSocket serverSocket = null;
Socket socket = null;
try {
//Puerto 5000
int puerto = 5000;
serverSocket = new ServerSocket(puerto);
System.out.println("Establishing connection with the PC-CHECK client");
socket = serverSocket.accept();
System.out.println("Successfully established connection");
System.out.println("Starting... It may take several seconds");
oos = new ObjectOutputStream(socket.getOutputStream());
while (true) {
//Obtencion del hardware
this.cpuMain = this.loadCPU();
this.ram = this.loadRAM();
//Envio de datos al pc cliente
oos.writeObject(this.cpuMain);
oos.writeObject(this.ram);
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if(oos != null){
oos.close();
}
if(socket != null){
socket.close();
}
if(serverSocket != null){
serverSocket.close();
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And here the client receives it
public Main(String ip) {
try {
socket = new Socket(ip, 5000);
System.out.println("Successfully established connection with server PC-CHECK");
System.out.println("Starting... It may take several seconds");
ois = new ObjectInputStream(socket.getInputStream());
while(true){
this.cpu = (CPU)ois.readObject();
Utilities.clear();
System.out.println("CPU: ");
System.out.println(cpu.toString());
this.ram = (RAM) ois.readObject();
System.out.println("RAM: ");
System.out.println(this.ram.toString());
}
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}catch (Exception e){
}finally {
try {
if (socket != null) {
socket.close();
}
if (ois != null) {
ois.close();
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
My question is how can I solve this error, and make sure that all computers work properly?
Thank you

TCP client and server; cmd prompt using object streams issue [duplicate]

I have one client file clientRPC.java and server file serverRPC.java. Both communicate using TCP protocol and use objectinput and output stream to transfer data.
my client file:
public class clientRPC {
public static void main(String args[]) {
Socket s = null;
try {
int serverPort = 8888;
s = new Socket("localhost", serverPort);// server name is local host
//initializing input and output streams object and referencing them to get input and output
ObjectInputStream in = null;
ObjectOutputStream out = null;
out = new ObjectOutputStream(s.getOutputStream());
in = new ObjectInputStream(s.getInputStream());
MathsTutor mt = new MathsTutor();
out.writeObject(mt);
out.flush();
System.out.println("Welcome to Maths Tutor Service. The available maths exercises are:\n"
+ "Addition: Enter 'A' or 'a'\n"
+ "Subtraction: Enter 'S' or 's'\n"
+ "Multiplication: Enter 'M' or 'm'\n"
+ "Division: Enter 'D' or 'd'\n"
+ "Enter 'Q' or 'q' to quit");
//System.out.println();
MathsTutor mt1 = (MathsTutor) in.readObject();
String response = in.readUTF();
System.out.println(response);
} catch (UnknownHostException e) {
System.out.println("Socket:" + e.getMessage());
} catch (EOFException e) {
System.out.println("EOF:" + e.getMessage());
} catch (IOException e) {
System.out.println("readline:" + e.getMessage());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
System.out.println("close:" + e.getMessage());
}
}
}
}
}
and my server file :
public class serverRPC extends Thread {
String request;
String response;
public static void main(String args[]) {
try {
int serverPort = 8888;
ServerSocket listen_socket = new ServerSocket(serverPort);
while (true) {
Socket clientSocket = listen_socket.accept();
Connection c = new Connection(clientSocket);
}
} catch (IOException e) {
System.out.println("Listen socket:" + e.getMessage());
}
public serverRPC(String s) {
request = s;
}
}
class Connection extends Thread {
ObjectInputStream in;
ObjectOutputStream out;
Socket clientSocket;
public Connection(Socket aClientSocket) {
try {
clientSocket = aClientSocket;
in = new ObjectInputStream(clientSocket.getInputStream());
out = new ObjectOutputStream(clientSocket.getOutputStream());
this.start();
} catch (IOException e) {
System.out.println("Connection:" + e.getMessage());
}
}
public void run() {
try {
MathsTutor mt = (MathsTutor) in.readObject();
InetAddress ip = clientSocket.getInetAddress();
System.out.println("The Received Message from Client at address:/" + ip.getHostAddress());
System.out.println("====================================");
MathsTutor mt1 = new MathsTutor();
out.writeObject(mt1);
while(true) {
// Read from input
String command = in.readUTF();
System.out.println(command);
}
//System.out.println();
} catch (EOFException e) {
System.out.println("EOF:" + e.getMessage());
} catch (IOException e) {
System.out.println("readline:" + e.getMessage());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {/*close failed*/
}
}
}
}
The problem is when I run server and then client on cmd, the client side displays the welcome msg and puts cursor on another line for user input but, I can't type anything, the cursor just blinks... I know this might be simple but it has taken already 3 hours for me and I'm stuck in the same thing.
The cursor marked with red keeps blinking but doesn't let me type anything.
You're writing an object with writeObject() and trying to read it with readUTF(). Illogical.
objects written with writeObject() must be read with readObject().
strings written with writeUTF() must be read with readUTF().
primitives written with writeXXX() must be read with readXXX(), for most values of X.

ObjectInputStream$BlockDataInputStream.peekByte when using program on internet

I created an application with TCP, it works nice when I used it on a local network with 127.0.0.1 but the server refused to works when a client try to connect to him from an another network.
I don't know what this error means and how to resolve it and I can't anderstand that an application could works only on LAN.
public class Reception {
InputStream inObjet = null;
BufferedReader inString = null;
ObjectInputStream recVec2i = null;
public Reception(Socket socket) {
try {
this.inObjet = socket.getInputStream();
this.inString = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.recVec2i = new ObjectInputStream(inObjet);
} catch (IOException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Vecteur2i recevoir() {
Vecteur2i to = new Vecteur2i();
try {
to = (Vecteur2i) recVec2i.readObject();
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
to = new Vecteur2i(1000, 1000);
}
return to;
}
public String recevoirString() {
String chaine = "";
try {
chaine = inString.readLine();
} catch (IOException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
}
return chaine;
}
public void fermerReception() {
try {
inString.close();
} catch (IOException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
}
try {
inObjet.close();
} catch (IOException ex) {
Logger.getLogger(Emission.class.getName()).log(Level.SEVERE, null, ex);
}
try {
recVec2i.close();
} catch (IOException ex) {
Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And here the exception :
avr. 22, 2015 9:33:33 PM Snake.Reception recevoir Grave: null
java.io.EOFException at
java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2597)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1316)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at Snake.Reception.recevoir(Reception.java:41) at
Snake.Partie.cycleDeJeu(Partie.java:55)
Regards,
You can't mix different types of stream via the same socket when one or more of them is buffered, and both BufferedInputStream and ObjectInputStream are buffered. The buffers will 'steal' data from each other. In your case you should do all the I/O via the object stream. It has String-based methods.

Java sockets in threads - no data transfer

I'm trying to create data transfer between server and client. Connection is established, but there is no transfer of data between server and client.
Server side:
public static void CreateServerSocket() {
try {
server = new ServerSocket(9999);
t = new Thread(new SocketServer());
t.start();
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void closeServer() {
try {
server.close();
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void sentSide(String side) {
out.print(side);
}
#Override
public void run() {
try {
client = server.accept();
out = new PrintWriter(client.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out.print("test from server");
while (true){
strIn=in.readLine();
System.out.println(strIn);
}
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
Client side:
public class SocketClient implements Runnable {
static Socket client;
static Thread t;
static PrintWriter out;
static BufferedReader in;
static String strIn;
public static boolean CreateClientSocket(InetAddress ip) {
try {
client = new Socket(ip, 9999);
t = new Thread(new SocketClient());
t.start();
return true;
} catch (UnknownHostException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
public static void closeClient() {
try {
client.close();
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void run() {
try {
out = new PrintWriter(client.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out.print("test from client");
while (true) {
strIn = in.readLine();
System.out.println(strIn);
DialogMultiplayerStart.setSide(strIn);
}
} catch (IOException ex) {
Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
The problem is that out.print is not sending data. Connection is established, because CreateClientSocket returns true, but neither out.print("test from client/server") is working, nor setSide method. Thanks in advance for your help.
The client starts by doing
out.print("test from client");
and then waits for a whole line from the server:
strIn = in.readLine();
The server starts by doing
out.print("test from server");
and then waits for a whole line from the client:
strIn = in.readLine();
So, both sides are waiting for the other side to send an end of line: deadlock.
Change it to
out.println("test from server");
and
out.println("test from client");
to go a bit further. But you'll then have another problem: both sides read in an infinite loop, and don't send anything more.

Maintain server after socket killing

So I have this simple server. What I want to do is keep the server running and waiting for another client, when I kill the clients socket (telnet -> end process).
private ServerSocket serv;
public Server() throws IOException {
try {
serv = new ServerSocket(port);
serv.setReuseAddress(true);
while(true) {
Socket sock = serv.accept();
try {
BufferedReader netIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter netOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())), true);
while(true) {
//do stuff
}
} finally {
sock.close();
}
}
} catch (SocketException e) {
recreateSocket();
} catch (IOException e) {
e.printStackTrace();
}
}
private void recreateSocket() {
try {
ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port);
serv = socket;
} catch (IOException e) {
e.printStackTrace();
}
}
Atm it throws bindException, how to deal with it.
Add catch statement(s) to before the finally block (but don't call recreateSocket() there )
Update to clarify, something like this:
while(true) {
//do stuff
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
sock.close();
Start a new thread to handle each accepted connection.
The reason is that you are creating a server socket again. You don't need to do this (the previous one is still working which is why you get a bind exception). This is what you want to do:
private ServerSocket serv;
public Server(int port) throws IOException
{
try {
serv = new ServerSocket(port);
serv.setReuseAddress(true);
while(true) {
Socket sock = serv.accept();
try {
BufferedReader netIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter netOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())), true);
// do stuff
} catch(SocketException e) {
e.printStackTrace();
} finally {
sock.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

Categories