Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 402799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:09:30+00:00 2026-05-12T17:09:30+00:00

MyWriter.java (execute this before MyReader.java) import java.io.*; import java.net.*; import java.util.* ; public class

  • 0

MyWriter.java (execute this before MyReader.java)

import java.io.*;
import java.net.*;
import java.util.* ;

public class MyWriter {

    public static void main(String[] args) throws Exception {

        ServerSocket ss = new ServerSocket(1234) ;
        System.out.println("Server started...\n\n") ;
        Socket s = ss.accept() ;

        OutputStream out = s.getOutputStream() ;

        Scanner scan = new Scanner(System.in) ;

        while(true){
            System.out.print("Enter integer (or something else to quit) : ");

            try{
                int i = scan.nextInt() ;
                out.write(i) ;
            }catch(RuntimeException rte){
                System.out.println("\n\n") ;
                rte.printStackTrace() ;
                System.out.println("\n\n") ;
                break ;
            }
        }
    }
}

MyReader.java (only executed, when done with MyWriter.java)

import java.io.*;
import java.net.*;
import java.util.* ;

public class MyReader {

    public static void main(String[] args) throws Exception {

        Socket s = new Socket("localhost", 1234) ;

        InputStream is = s.getInputStream() ;

        int i ;
        while( (i = is.read()) != -1 ){
            System.out.println( "character form : " + (char)i + ", int form : " + i);
        }
    }
}

Problem : MyReader is not getting terminated, even if I am passing -1 in MyWriter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:09:30+00:00Added an answer on May 12, 2026 at 5:09 pm

    Read returns an int so that it can also indicate the end of the stream, as -1. If the signature were

    byte read()
    

    then you’d never know when the stream had ended (unless it threw an exception, I suppose).

    I suspect there’s no particular reason for write to take an int – it seems a slightly strange choice to me. The Javadoc specifically says that the higher 24 bits are ignored. Maybe this is just an accident of history.

    (In .NET, Stream.ReadByte returns an int, but Stream.WriteByte takes a byte. This makes more sense IMO.)

    Now, with that background, when you write “-1” to your stream, you’re actually writing the value “255” – and that’s what you read back. “-1” is represented in binary as “all 1s” so the lower 8 bits of that are “11111111” which is 255 when considered as an unsigned value.

    Your reader will only terminate when you close the output stream in the “writing” application – because that’s when read() will return -1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this code public override void Serialize(BaseContentObject obj) { string file = ObjectDataStoreFolder +
class MyWriter: def __init__(self, stdout): self.stdout = stdout self.dumps = [] def write(self, text):
Objective: Serialize all the public properties of the ClassMain: public class ClassMain { ClassA
pthread_t single_thread ; pthread_create (&single_thread , NULL , &mywriter , 0) ; void *
Below is my code (ASP.net web project), but when i debug this asp page
import subprocess subprocess.Popen(['C:\Program Files\Nuke6.1v3\\Nuke6.1.exe', '-t', 'C:\Users\user\Desktop\\server.py' I am currently using this to call server.py,
If I have a line like this ContentRepository.Update(existing); that goes into datastore repository to
I want to be able to send a XmlSerializer class (which is generated obvious
This is a combination of my two recent questions: [1] Python instance method in
I have created a clone of an object in my code. Class<?> oc =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.