1

I'm working on a school project and the following codes are an example provided for building the project (should work without any problem but not...). There was no compiling error but when I use telnet to test it the following message shows:

java.io.StreamCorruptedException: invalid stream header: 56543130 
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
    at ThreadedDataObjectHandler.run(ThreadedDataObjectServer.java:41)

Line 41 is

ObjectInputStream in =new ObjectInputStream(incoming.getInputStream());

Here are my codes:

import java.io.*;
import java.net.*;
import java.util.*;
public class ThreadedDataObjectServer {  
    public static void main(String[] args ) {  

      try 
  {  ServerSocket s = new ServerSocket(3000);

     for (;;)
     {  Socket incoming = s.accept( );
        new ThreadedDataObjectHandler(incoming).start();

     }   
  }
  catch (Exception e) 
  {  e.printStackTrace();
      } 
   } 
}

class ThreadedDataObjectHandler extends Thread
{
DataObject myObject = null;
private Socket incoming;
private String greeting="Hello!";
public ThreadedDataObjectHandler(Socket incoming) 
   { 
        this.incoming = incoming;
   }

   public void run()
   {  try 
      {     

    ObjectInputStream in =new ObjectInputStream(incoming.getInputStream());

    ObjectOutputStream out =new ObjectOutputStream(incoming.getOutputStream());

    myObject = (DataObject)in.readObject();

    System.out.println("Message read: " + myObject.getMessage());

    myObject.setMessage("Got it!");

    System.out.println("Message written: " + myObject.getMessage());


    out.writeObject(myObject);

    in.close();

    out.close();

    incoming.close();    

  }
  catch (Exception e) 
  {  e.printStackTrace();
  } 
   } 
}
class DataObject implements Serializable{
    protected String message;
    public DataObject(){
        message="";
    }
public void setMessage(String m){
    message=m;
}
    public String getMessage(){
    return message;
    }
}

What I tried was to switch the order of statements ObjectInputStream in=... and ObjectOutputStream out=... but no luck. Please help...thanks.

HemChe
  • 2,249
  • 1
  • 21
  • 33
David
  • 11
  • 3
  • what u r doing in this code? no, what you wanna do? – Ankit Apr 10 '13 at 04:39
  • Are you sure the client is writing the object using ObjectOutputStream? – Bhushan Bhangale Apr 10 '13 at 04:43
  • You need to perform serialization. – HemChe Apr 10 '13 at 04:51
  • @ay89 this is only a small test to allow client and server to communicate with each other but has to use DataObject class to make it happen – David Apr 10 '13 at 04:52
  • is your client socket server also sending data using ObjectOutputStream.. may be a duplicate question http://stackoverflow.com/questions/5560939/java-io-streamcorruptedexception-invalid-stream-header – prashantsunkari Apr 10 '13 at 04:52
  • @BhushanBhangale I just tested it through telnet to connect myself using 127.0.0.1. I don't think the client is writing to the ObjectOutputStream...but don't know how to fix it... – David Apr 10 '13 at 04:55
  • @HemChe please be more specific... – David Apr 10 '13 at 04:56
  • @David telnet to what? The program which connect to the ServerSocket you have defined above should write the object using ObjectOutputStream. – Bhushan Bhangale Apr 10 '13 at 05:01
  • I see, so basically you wrote a server that will accept connections and you connected using telnet to that? :-D – Thihara Apr 10 '13 at 05:02
  • @BhushanBhangale I'm new to the java socket. can you provide me an example? the codes are from professor so i didn't write them – David Apr 10 '13 at 05:09
  • @David Check this link... http://stackoverflow.com/questions/2939073/java-io-streamcorruptedexception-invalid-stream-header-7371007e – HemChe Apr 10 '13 at 05:27

2 Answers2

2

From what I understood from the comments you are trying to read the objects from a telnet connection using ObjectInputStream.

You cannot do that. If you are going to use ObjectInputStream then you need the other connecting program to write using a ObjectOutputStream.

You telnet client don't really give a shit about the Java ObjectOutputStream, ObjectInputStream and Serialization.

So I'd try something like a InputStreamReader wrapped in a BufferedReader.

If you just want to test the connectivity just write a small java program that will connect to your program instead of using telnet.

Thihara
  • 7,031
  • 2
  • 29
  • 56
0

David as I mentioned in the comments you have to write a client which uses ObjectOutputStream to send the same DataObject to the server socket.

Since you are expecting DataObject a client needs to send the DataObject. When you use telnet it connects but from there you cannot send the DataObject in a way java Object stream understands.

Please see http://zerioh.tripod.com/ressources/sockets.html for server/client example.

Also since its some school exercise try to understand the concept and do not just copy.

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71