1

In the following program, client has to enter numbers and send them to server. Server has to compute the numbers which client is sent and send that result to client.

For the following code, I'm getting exception inside my server class:

java.io.StreamCorruptedException: invalid stream header: 75720002
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)

And inside my Client class, exception:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)

How can I solve this problem ?

1 Answer 1

3

You can't wrap multiple streams/writers around the same underlying stream (your client is wrapping the socket output stream twice and your server is wrapping the socket input stream twice). if you want to send Objects, you need to just use the Object based streams.

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

6 Comments

Ok so, it's better to use object streams then scanner, for all kind of communications ?
@Xerath - no, object streams aren't better (or worse) than Scanner, just different. pick the one that works for you and stick with it, but you can't use both on the same stream.
Can you create any code example on how should I implement my scenario ?
@Xerath - your code is already a good starting point, just choose one type of communication stream and stick with it (hint: ObjectOutputStream can send Strings).
Hey again @jtahlbonr, besides sending objects, how I'm also going to communicate, pass normal string messages ? I don't understand that logic.
|

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.