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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:08:06+00:00 2026-05-26T06:08:06+00:00

I’m trying to make a simple chat server. I had a siisue where the

  • 0

I’m trying to make a simple chat server. I had a siisue where the program would run on one thread end a endless loop accepting a socket connection on port 80. Now if I re ran the program, it would give me a address error, ie port 80 was not freed.
Now the server runs on a separate thread, and the main thread just waits for some one to press the enter key by using a system.in.read. It seems like in java when a program exits, it does not automcly close all the connections.
I was thinking about having the main thread send a message that the server thread would read. It would tell the server thread to shut down.
Is there a better way ro do this?

code
public class cServerThread implements Runnable {

Thread runner;
public cServerThread() {
}

public cServerThread(String threadName) {
    runner = new Thread(this, threadName); // (1) Create a new thread.
    System.out.println(runner.getName());
    runner.start(); // (2) Start the thread.
}

public void run() {
    //Display info about this particular thread
    System.out.println(Thread.currentThread());
     cServer mServer = new cServer();
      mServer.run();

}

}

public class cServer {

boolean StopFlag;
 String header;
Socket connection;
ServerSocket  server;
StringBuffer request;
OutputStream out;



 public boolean ReadSize(InputStream in)
 {
    // read in one line
     try{
         request = new StringBuffer(1000);
        boolean f=true;
        int s=43;
        while( s-->0 )
        {
            int c=in.read();
            char a=(char)c;
            request.append(a);
                } // end while

     } catch(IOException ec)
        {
            System.out.println(ec.getMessage());    
        }

     System.out.println("last line");
        System.out.println(request);
        int p=request.lastIndexOf("stop");
        if (p!=-1)
            StopFlag=true;

    return true; 
 }


 public boolean ReadLine(InputStream in)
 {
    // read in one line
     try{
         request = new StringBuffer(1000);
        boolean f=true;
        while(true)
        {
            int c=in.read();
            if (c=='\r') 
                {
                // next should be a \n
                 c=in.read();
                if (f==true)
                    return false;
                 break;
                }
            f=false;
            request.append((char)c);
                } // end while

     } catch(IOException ec)
        {
            System.out.println(ec.getMessage());    
        }

        System.out.println(request);
        int p=request.lastIndexOf("stop");
        if (p!=-1)
            StopFlag=true;

    return true; 
 }


 public void ReadHeader()
 {
    // read in one line
     try{
        request = new StringBuffer(1000);
        System.out.println("get connection reading in data \r");
        InputStream in = new BufferedInputStream( connection.getInputStream() );
        StopFlag=false;
        for(int i=0;i<11;i++)
            ReadLine(in);
        ReadSize(in);


     } catch(IOException ec)
        {
            System.out.println(ec.getMessage());    
        }

 }

 public void SendReply()
 {
        // set up header
     String content="test html ted\r\n cat2";

      String header="HTTP/1.0 200 Ok\r\n"+
      "Server: OneFile 1.0\r\n"+
      "Content-length: "+content.length() +"\r\n"+
      "Content=type: "+"text/html " +"\r\n\r\n";
    //  header=header.getBytes("ASCII");
      byte[] bHeader;

      try{
      bHeader=header.getBytes("ASCII");

        out.write( header.getBytes("ASCII") );
        out.write(content.getBytes("ASCII"));
        out.flush();



      } catch(IOException ec)
        {
          System.out.println(ec.getMessage());
        }

 }

 public int GetPort()
 {
  return 90;     
 }


 public void run()
  {
     String content="test html ted2 \r\n fred";

    // set up header
      header="HTTP/1.0 200 Ok\r\n"+
      "Server: OneFile 1.0\r\n"+
      "Content-length: "+content.length() +"\r\n"+
      "Content=type: "+"text/html " +"\r\n\r\n";
    //  header=header.getBytes("ASCII");


      try{
        connection = null;
        server = new ServerSocket(87); // port 62

        // Loop forever
        while(true)
        {
            ///////////////////////////////////////////////////
            // get a new connection
            ///////////////////////////////////////////////////
            System.out.println("Aceepting connections on port 86 \r");

            try{
                // Get New Connection
                connection=server.accept();

                // Create a buffer stream to connection
                out=new BufferedOutputStream( connection.getOutputStream() ); 

                // Read in the header
                ReadHeader();

                // Read in the message id
                // Send the reply back
                SendReply();

                if (StopFlag)
                {
                    System.out.println("shutting down \r");
                    connection.close();
                    break;
                }
                /*
                byte[] bHeader;
                bHeader=header.getBytes("ASCII");

                out.write( header.getBytes("ASCII") );
                out.write(content.getBytes("ASCII"));
                out.flush();

                int p=request.lastIndexOf("stop");
                if (p!=-1)
                {
                    System.out.println("shutting down \r");
                    connection.close();
                    break;
                }
                */
            } catch(IOException ec)
            {
                System.out.println(ec.getMessage());    
            }
            finally{
                if (connection != null )
                    connection.close();
            }

            } // end while

    } catch(IOException ec)
    {
        System.out.println(ec.getMessage());            
    }

  } // end run


} // end class
  • 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-26T06:08:06+00:00Added an answer on May 26, 2026 at 6:08 am

    ServerSocket.setReuseAddress(true). It isn’t Java but the operating system, specifically the TCP/IP stack, that keeps the port open, for two minutes in TIME_WAIT state, for technical reasons.

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

Sidebar

Related Questions

I am trying to make a simple client-server chat program. On the client side
I'm trying to make a simple chat server in Java. Now I have a
I'm trying to make a simple chat program between to programs on a lan.
I'm trying to make a simple text chat for example. Server start and work.
I am trying to make a simple TCP client and host chat program in
im trying to make a simple program that has one BITMAP that is the
I am trying to make a simple HTTP server that would be able to
I'm trying to make a simple blackjack program. Sadly, I'm having problems right off
I'm trying to make a simple C# web server that, at this stage, you
I am trying to make a simple Client-Server application but when I execute the

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.