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 501509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:11:49+00:00 2026-05-13T06:11:49+00:00

I have Client class and Server class but when i run both main methods

  • 0

I have Client class and Server class but when i run both main methods and then nothing will happen and when i stop running ,this exception will be occurred. why?? please help me,how can I fix it???

my Client class:

public class Client {

static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    try {
        Socket c = new Socket("localhost", 5001);
        BufferedReader read = new BufferedReader(new InputStreamReader(c.getInputStream()));
        BufferedWriter write = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
        String string = reader.readLine();
        write.write(string, 0, string.length());
        write.newLine();
        write.flush();
        System.out.println(read.readLine());



    } catch (Exception e) {
        System.err.println(e);
    }
}}

my Server class:

public class Server{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    ServerSocket s = null;
    try {
        s = new ServerSocket(5001);
        System.out.println("listening...");
        Socket so = s.accept();
        BufferedReader read = new BufferedReader(new InputStreamReader(so.getInputStream()));
        BufferedWriter write = new BufferedWriter(new OutputStreamWriter(so.getOutputStream()));
        while (true) {
            String string = read.readLine();
            System.out.println(string);
            String answer = "I got" + string + "from you!";
            write.write(answer, 0, answer.length());
            write.newLine();
            write.flush();
        }
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

}}

stacktrace in server cpnsole:

run:


listening...
system connected
Hello

Dec 19, 2009 12:58:15 PM server.Main main
SEVERE: null
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
        at java.io.InputStreamReader.read(InputStreamReader.java:167)
        at java.io.BufferedReader.fill(BufferedReader.java:136)
        at java.io.BufferedReader.readLine(BufferedReader.java:299)
        at java.io.BufferedReader.readLine(BufferedReader.java:362)
        at server.Main.main(Main.java:37)
BUILD SUCCESSFUL (total time: 9 seconds)

in Client console:

run:
Hello
I gotHellofrom you!
BUILD SUCCESSFUL (total time: 4 seconds)
  • 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-13T06:11:49+00:00Added an answer on May 13, 2026 at 6:11 am

    Your client connects to the server, sends some data, reads the response and terminates. That’s ok.

    But your server waits for a client, reads its data, writes a response and then tries to read some data from the client again. But the client has closed the connection. So the server gets the exception you described.

    To fix this (on server side), you have to do the Socket so = s.accept(); within your while loop. And don’t forget to close the socket at the end of the loop.

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

Sidebar

Ask A Question

Stats

  • Questions 248k
  • Answers 248k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should be able to simply grab the pop-up at… May 13, 2026 at 8:52 am
  • Editorial Team
    Editorial Team added an answer I'm not sure of your goals, so let me answer… May 13, 2026 at 8:52 am
  • Editorial Team
    Editorial Team added an answer Note: the word/line wrapping in eclipse is a "sore" topic,… May 13, 2026 at 8:52 am

Related Questions

I am adapting a little rmi client-server application. I have written several things :
hello i have a basic client-server system running using java sockets. my problem is,
I am trying to setup a simple UDP client and server using Ruby. The
I have simple chat program using WCF service. One service use for server and
Ok, I'm programming in objective-C and using Xcode. I have read through the documentation

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.