0

I am trying to send some data over the sockets. The first 3 data items are sent successfully but when I try to send the double array then an exception is thrown. Lets come to the code: Client Side:

    Socket clisock=new Socket("127.0.0.1",1341);
    Scanner sc1=new Scanner(clisock.getInputStream());
    PrintStream p=new PrintStream(clisock.getOutputStream());
    p.println(num_doc);
    p.flush();

    p.println(TD);      
    p.flush();

    p.println(num_Decimal);     
    p.flush();
    ObjectOutputStream os=new ObjectOutputStream(clisock.getOutputStream());
    os.writeObject(server_index);

Server Side:

    int number;
    long keyword, keywords;
    double[][] server_ind;
    ServerSocket s1=new ServerSocket(1341);
    Socket ss=s1.accept();
    Scanner sc=new Scanner(ss.getInputStream());
    number=sc.nextInt();

    keyword=sc.nextLong();

    keywords=sc.nextLong();

    ObjectInputStream is = new ObjectInputStream(ss.getInputStream());
    server_ind=(double[][])is.readObject();

Exception: java.io.StreamCorruptedException: invalid stream header: FAD08000

Note: Before posting this question, I have already searched for similar exceptions but havent been successful in removing the exception.

1 Answer 1

1

The Scanner is buffered so it will read as much memory as is available (up to it's buffer size)

This means if you didn't mean the Scanner to read all the data and read instead some of it as a binary object you have a problem.

A simpler solution is to stick to one stream type. As you need Object stream, just use it end to end and you won't get this confusion.

java.io.StreamCorruptedException: invalid stream header: FAD080

This means the Scanner has consumed the object stream header so when the object stream attempts to read some data it is reading some piece of data much further down the stream.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.