java socket  output stream (change the way writing)

Posted on 2009-05-02
Last Modified: 2013-12-29
Hi! I write to a socket's outputstream like;

pw = new PrintWriter(s.getOutputStream());

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,"",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?
Question by:btocakci
    1 Comment
    LVL 17

    Accepted Solution

    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(new ClientListenData(1000,"",id));

    Open in new window


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    wordsWithout 49 64
    sumDigits  challenge 7 41
    @SBGen Method 3 18
    Impossible to extract MSI from new JAVA releases 2 7
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    This video teaches viewers about errors in exception handling.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now