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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:20:41+00:00 2026-06-01T06:20:41+00:00

I’m programming a client server app. I’m wondering to end the connection, which side

  • 0

I’m programming a client server app. I’m wondering to end the connection, which side should decide to end it, client or server?

Then if for example the client did it, should the server still keep open or not?

Suppose we have the below codes for each client and server:

           socket = new Socket("127.0.0.1",3000);
........

            socket.close();

or

            serverSocket = new ServerSocket(3000);
            socket = serverSocket.accept();
......

            serverSocket.close();

EDIT:
according to the matt’s answer, i pot my code here and let see why my code doesnt work:

generally, i have a Jframe program as client which will connect to a server and while its open, i want the connection being alive, since it send info to the server and server should response for the result. but if i dont use closing the socket from server it gives an error and if i use, after once calculation, it closes the connection:

Server

        private static PrintWriter toClient;
        private static BufferedReader fromClient;


  public static void run() throws IOException, SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException, SAXException, XPathExpressionException
        {
  
        
        System.out.println("Server is waiting for connections...");
   
        while(true)
        {
                 openStreams();
                  processClient();
                  closeStreams();
 }
     
    }

OpenStream:

  public static void openStreams() throws IOException, SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException, SAXException, XPathExpressionException
            {
            serverSocket = new ServerSocket(3000);
            socket = serverSocket.accept();
                System.out.println("Connected");

             toClient = new PrintWriter(socket.getOutputStream(),true);
            
            fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            

            }

Closestearms:

   public static void closeStreams() throws IOException
            {
            fromClient.close();
            toClient.close();
            socket.close();
            serverSocket.close();
                System.out.println("Disconnected");
        }
          

the error i receive if i remove the closestream();

at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
    at java.net.ServerSocket.bind(ServerSocket.java:319)
    at java.net.ServerSocket.<init>(ServerSocket.java:185)
    at java.net.ServerSocket.<init>(ServerSocket.java:97)
  • 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-06-01T06:20:42+00:00Added an answer on June 1, 2026 at 6:20 am

    Depends on your application’s logic. Both are valid, and you must make sure your code is correct in both cases, since if the connection is broken “involuntarily” (like a network drop), it will appear to both sides as if the other side had closed the connection.

    So your code must handle both cases gracefully.

    Your closing code is suspicious: you’re closing the listening socket, which is unusual. You should only close the listening socket when you don’t want any more incoming connections (i.e. usually for a server, when you’re shutting down the server).
    Only close the socket you got from accept to end that “conversation”, and don’t re-open the server listening socket. (The exception you’re getting might be an “Address already in use” in this case. See How to set reuse address option for a datagram socket in java code? for an example of how to avoid that specific problem.)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string
I am writing an app with both english and french support. The app requests

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.