• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

java socket output stream (change the way writing)

Hi! I write to a socket's outputstream like;

pw = new PrintWriter(s.getOutputStream());
pw.write("NEWCON");

after that i need to write some object, so i use a code like this;

ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                  oos.writeObject(new ClientListenData(1000,"127.0.0.1",id));

I get an StreamCorrupted Exception. If i donot use the printwriter first, it is working.
How to change the way i can write to a socket's outputstream?
0
btocakci
Asked:
btocakci
1 Solution
 
Thomas4019Commented:
You are mixing two types of outputs in the stream. I would only use the ObjectOutputStream. Here is how i would write the code. Then on the reading side call readObject() twice.
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject("NEWCON");
oos.writeObject(new ClientListenData(1000,"127.0.0.1",id));

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now