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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:05:34+00:00 2026-05-30T20:05:34+00:00

I’ve recently gotten into trying to make server-client connections. I was able to make

  • 0

I’ve recently gotten into trying to make server-client connections. I was able to make a 1 on 1 connection with no problems, but now i’m trying to make a server that accepts multiple clients and i’m running into a problem where i can’t make the server listen to connections while there is one established… I’m not sure if i made myself clear but here’s my code:

-The main loop that waits for connections:

public class ChatMultiServer {

public static void main(String []args){

    int socknum = 124;
    ServerSocket serverSocket;
    Socket clientSocket;

    while(true){

    ////opens socket
    try{
        System.out.println("Opening port...");
        new ServerSocket(124).close();
        serverSocket = new ServerSocket(socknum);
    }catch(Exception e){System.out.println("Error 101 = failed to bind to port "+socknum+"."); break;}
    //////accepts connection
    try{
        System.out.println("Waiting for connections...");
        clientSocket = serverSocket.accept();
    }catch(Exception e){System.out.println("Error 102 = failed to accept port "+socknum+"."); break;}
    /////
    try{
        System.out.println("Initializing thread...");
        new Thread(new CMSThread(clientSocket));
    }catch(Exception e){System.out.println("Error 103 = failed to create thread."); break;}
    try{
        serverSocket.close();
    }catch(Exception e){System.out.println("Error 105 = failed to close socket.");}
    }
}

}

-The thread that handles the connections:

public class CMSThread extends Thread{
Socket socket;
BufferedReader in;
PrintWriter out;
String username;
char EOF = (char)0x00;
public CMSThread(Socket s){
    socket = s;
    run();
}
public void run(){
    try{
    System.out.println("Setting up streams...");
    in = (new BufferedReader(new InputStreamReader(socket.getInputStream())));
    out = new PrintWriter(socket.getOutputStream());
    }catch(Exception e){System.out.println("Error 204 = failed to get streams");}
    try{
        out.print("Welcome! you can quit at any tyme by writing EXIT.\nLet me introduce myself, I'm 'testprogram 1', but that doesn't really matter since you'll do the talking.\nWhat's your name?"+EOF);
        out.flush();
        username = in.readLine();
        out.print("<b>"+username+"</b>, that's a nice name.\nWell, i'll shut up now. Have fun talking to yourself while whoever is running the server observes your conversation.\n"+EOF);
        out.flush();    
    }catch(Exception e){System.out.println("Are you effin kidding me!? -.-   whatever... Error 666 = failed to chat.");}
}

}

My problem, once again, is that when the server gets a connection with a client(I’m using actionscript for the clients just because it’s easier to make a GUI), it just waits until the thread is done running to start the loop again. I’m trying to make it loop at the same time as the thread handles the chat.

I was thinking maybe i needed to make a thread for the loop as well as the thread for handling the connection, but i’m not sure as to how i would go about doing that… Please let me know if my hypothesis was somewhat right, and if it was, some guidance towards the answer would be nice.

PS: I’m sorry if my code is a bit messy or if this is a stupid question, i haven’t made a java program in a while…

  • 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-30T20:05:35+00:00Added an answer on May 30, 2026 at 8:05 pm

    You aren’t actually starting your new Threads – you are just directly calling run(). As far as I can see, this means that you will be executing run() in the main thread that creates each CMSThread object.

    To start a Thread, you have to call thread.start().

    Also, I’m not sure why you are wrapping your CMSThread in another Thread – CMSThread extends Thread so it can be started in its own right. The wrapper Thread isn’t being started either.

    So you need:

            new CMSThread(clientSocket).start();
    

    and remove the run() call from the constructor of CMSThread

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.