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

  • Home
  • SEARCH
  • 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 3753142
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:20:49+00:00 2026-05-19T09:20:49+00:00

Hii all, I have already created some programs related to client and server. Today

  • 0

Hii all, I have already created some programs related to client and server. Today it was my sessional[practical exam] of Client-server technology.

Problem was: i was to add two no’s at server sent from client and result back to client.

I tried this solution but some strange solution was there:
Server.java

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

public class Server{
    public static void main(String args[]) throws Exception{
        ServerSocket s = new ServerSocket(7896);
        Socket cs = s.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(cs.getOutputStream()));
        bw.write(br.read() + br.read());
    }
}

Client.java

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

public class Client{
public static void main(String args[]) throws Exception{
    Socket s = new Socket("localhost", 7896);
    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    bw.write(3);
    bw.write(4);
    System.out.println("Output is: " + br.read());
}
}

when i ran it on dos prompt i got two blank screens; one at client site and one at server site(which was a bit surprising). then i closed server then suddenly i got error msg at client something like connection closed.

Then i ran same program on my ubuntu linux with same jdk 1.6 as it was in windows, and here also blank screens but when i closed server i got:

Output is: -1

Although in exam i done this using DataOutputStream and DataInputStream.

But why the above code is not working.

Can Anyone explain what is happening in the code there.

Thanks

  • 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-19T09:20:50+00:00Added an answer on May 19, 2026 at 9:20 am

    A few comments which might help you reach the correct answer:

    • The server code as it is can accept
      only a single client request since
      the accept() call isn’t present in
      an infinite loop.
    • When using buffered
      streams, make sure you always flush
      the OuputStream/Writer to ensure
      that the data is actually written
      to the client instead of just lying
      around in the buffer.
    • DataInput/OutputStream is absolutely required (or any other approach which logically reads the “number” string and converts it to an appropriate representation which can be added) since just reading from the BufferedReader doesn’t fetch you the “numbers”.

    But the question is, why flushing is required? To answer this question, one has to understand why “buffering” is used in the first place. Why bother wrapping your streams in another stream when you can directly write to your stream? The reason is that I/O operations are really expensive (at least when compared to accessing your RAM or CPU cache/registers). Frequent writes/reads accessing/writing small chunks of data to the HDD can choke up your HDD and bring down the overall performance of your application.

    So what’s the solution? Write data to the HDD less frequently. Two ways this can be achieved:

    1. Manually allocate a large byte array
      and fill up the entire array in a
      single read. This is way better than
      single byte reads. The byte array
      size does require tuning as per your
      application needs but for general
      purposes 8K should be good to go.
    2. Use a buffered stream. This has the advantage of not exposing the clients to the low level details. Your application can use the “stream” as it sees fit (continuously writing single bytes to it) and the stream would assume the complete responsibility of flushing the buffered data when it sees fit (this depends on the buffer size which you set when creating a buffered stream).

    Though the explanation was HDD specific, the same applies to other types of stream like in your case socket.

    TL;DR: Buffering improves IO throughput/performance. 🙂

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

Sidebar

Related Questions

Hi I have to send am mail(like an notification) to all the persons who
Hi I have created a user message page with this plugin. The grid will
Hi I have a div box that opens on my page and then loads
Hi I have a listview and, I am trying to start an activity from
Hi i have the following: bash_script parm1 a b c d ..n I want
Hi I have question regarding cin and buffer. I want to make a simple
Hi I have basically this mapping: @Entity @Table(schema=xas,name=billing) public class Billing implements Serializable{ private
Hi i have this javascript function function test(){ var count = 0; var date1
Hi I have a simple ajax search that returns the results in a table.
Hi i want to add to my app multi-friend-selector. I found a method in

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.