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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:01:59+00:00 2026-05-31T13:01:59+00:00

I have an java application using Socket TCP/IP and GUI. Server always listens connection

  • 0

I have an java application using Socket TCP/IP and GUI. Server always listens connection and receives message from client. When server received message, it will show a swing form.
My trouble is when I click on close button, the application will stop although I set server socket ALWAYS listens connection (by put method serverSocket.accept() in loop while(true)).
How can I solve that problem ?

Here is my code on Server:
public class TCPServer {

ServerSocket server = null;
BufferedReader in;
PrintWriter out;
Socket client = null;

//open serverSocket
public void openServer() {
    try {
        server = new ServerSocket(1234);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//accept connection and read data
public void listening() {
    try {

        while (true) {
            client = server.accept();
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);
            //read data from stream
            String s = in.readLine();
            System.out.println("String receive:  " + s);

            new NewJFrame().setVisible(true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void closeServer() {
    try {
        if (out != null) {
            out.close();
        }

        if (in != null) {
            in.close();
        }
        if (client != null) {
            client.close();
        }
        if (server != null) {
            server.close();
        }
    } catch (Exception e) {
    }
}

public static void main(String arg[]) {
    TCPServer server = new TCPServer();
    server.openServer();
    server.listening();
    server.closeServer();
}

}

  • 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-31T13:02:00+00:00Added an answer on May 31, 2026 at 1:02 pm

    From Javadoc:

          EXIT_ON_CLOSE 
          The exit application default window close operation.
    

    In the NewJFrame class, remove this: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    This is making the whole application shutdown when the close button is hit!

    Replace it by:
    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    This way you are sure only the window is disposed, not the whole application

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

Sidebar

Related Questions

I have a Java based web-application using Java Server Faces and Facelets. I am
Using Restlet I have created a router for my Java application. From using curl,
I have a project to develop a client server application in java . This
I have a client Server application which communicate using objects. when I send only
I have a java application where on thread is the socket connection which captures
I have a Python server script and a Java client application and the goal
I would like to create a server using .NET and Java client (Android application).
I currently have an application that sends XML over TCP sockets from windows client
I have a client/server application that remotely connects to a server via Java's SSLSocket.
I have a java server which is using TCP and sockets to connect to

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.