Dear all,
I've been struggeling with this issue for days now and I'm completely stuck on this.
The thing what I want to do is send an object over a socket.
I get the following error when I execute the send:
at java.io.ObjectInputStream$PeekInputStream.readFull y(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.rea dShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Agent.runAgent(Agent.java:125)
at Agent.<init>(Agent.java:53)
at Agent.main(Agent.java:193)
The code for the class that generates the error:
while (runAgent) { // At this point, we can read for input and reply with // appropriate output. try { serverSocket = agentSocket.accept(); in = new ObjectInputStream(serverSocket.getInputStream()); out = new ObjectOutputStream(serverSocket.getOutputStream()); InetAddress addr = agentSocket.getInetAddress(); // Print out details of this connection System.out.println("Accepted Client : Address - " + addr.getHostAddress().toString()); Object temp = null; //while ((performanceProfile = (PerformanceProfile) in.readObject()) != null) { while ((temp = in.readObject()) != null) { if(temp instanceof PerformanceProfile){ performanceProfile = (PerformanceProfile) temp; System.out.println("Profile received: " + performanceProfile.getName()); }else if(temp instanceof String){ String reply = ""; performanceProfile = null; if(temp.toString().equals("profile_status")){ if(performanceProfile == null ){ reply = "no_profile"; }else if(!performanceProfile.isRunning()){ reply = "not_running"; }else if(performanceProfile.isRunning()){ reply = "running"; } } else if(temp.toString().equals("stop_profile")){ stopPerformanceProfile(); System.out.println("Stopping performance profile"); } out.writeObject(reply); } break; } if(performanceProfile != null){ if(!performanceProfile.isRunning()){ startPerformanceProfile(); } } //serverSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
The code that send the object:
String command = "profile_status"; String reply = ""; // Create new output stream to send the data in bytes try { socket = new Socket(agent, 11111); // Instantiate the input and output variables for the sockets OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); ObjectInputStream ois = new ObjectInputStream(is); // Write the object oos.writeObject(command); Object temp = null; while ((temp = ois.readObject()) != null) { if (temp instanceof String) { reply = temp.toString(); } break; } // oos.writeObject(command); oos.flush(); oos.close(); os.flush(); os.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return reply;
TO make it complete I included the source of the Agent and the Server below.
src-agent.zip
src-server.zip
I can't figure out why this error occurs and maybe somebody can help me.