Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading from a socket that requires an EOF.

843790Feb 18 2008 — edited Feb 18 2008
I'm trying to send a request and get a response from a socket.

The server only seems to send the response once it gets an EOF. The only way I can seem to get an EOF is to close my output stream. However, when I close my output stream before reading from the input stream I get a "java.net.SocketException: Socket closed" exception.

Is there a way to send an EOF signal in the output stream? Am I doing something wrong?
        Socket sock = new Socket(this.getHost(), this.getPort());

        DataOutputStream os = new DataOutputStream(sock.getOutputStream());        
        DataInputStream is = new DataInputStream(sock.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        String responseString = new String();
        if (sock != null && os != null && is != null) {
            os.writeBytes(request);
            String responseLine;
            while ((responseLine = reader.readLine()) != null) {
                responseString += responseLine;
            }
        } //endif
        os.close();
        is.close();
        sock.close();
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 17 2008
Added on Feb 18 2008
4 comments
299 views