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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:35:22+00:00 2026-06-18T15:35:22+00:00

I have a client/server java game im working on. There’s a bunch of code

  • 0

I have a client/server java game im working on. There’s a bunch of code and im trying to paste as little as possible while still giving enough.

When I first start the server, it takes no CPU. When my first game client connects, it jumps to 25%(which seems quite high for what it does, but thats not my main concern yet). The problem is, even when the client disconnects, the CPU usage of the server app remains at 25%.

The server takes a name from the client and constantly receives x, y coordinates from the player.

Here is the code for my actual server that is run once the server is started: (I apologize beforehand for the excessive indentation)

 TCPServer(int port) {
                try
                {
                        tcpSock = new ServerSocket(port);
                        int z = 0;
                        while(true)
                        {
                                Socket sock = tcpSock.accept();
                                sock.setKeepAlive(true);  
                                clientList.addElement(new TcpClient(sock));
                                clientList.get(z).cr.start();
                                clientList.get(z).cw.start();
                                clientList.get(z).packs.addElement("?");
                                while(!clientList.get(z).cr.nameRecieved)
                                {
                                //do nothing untill client has provided it's name
                                }
                                playerList.addElement(new player(sock.getInetAddress(), clientList.get(z).cr.playerName));                                     
                                playerList.get(z).id=z;
                                playerList.get(z).name=clientList.get(z).cr.playerName;
                                clientList.get(z).packs.addElement("2 " + z);
                                for(int j =0; j<playerList.size();j++)
                                    {
                                   String status;
                                   if(playerList.get(j).Connected)
                                       status = "1";
                                   else
                                       status = "0";

                                        addToQueue("3 " + playerList.get(j).id + " "
                                                + playerList.get(j).name + " " + playerList.get(j).x + " " + playerList.get(j).y + " " + 
                                                playerList.get(j).area + " " + status + " " );                     
                                    }
                                z++;
                                addToQueue("4 " + z);
                        }    
                }

                catch (IOException e) {   
                        System.out.print(e);   
                }   
        }

And here is code that is run for each client. I have commented out a large portion of it for readability, the code just calculates and updates various location information. I think the problem is occurring somewhere here in this class.

  public class TcpClient {
                Socket sock;
                ObjectInputStream in;
                ObjectOutputStream out;
                ClientRead cr;
                ClientWrite cw;
                public Vector<String> packs = new Vector<String>();
                 TcpClient(Socket s) {
                        this.sock = s;
                        try
                        {
                            sock.setKeepAlive(true);
                        }
                        catch(Exception e)
                        {
                            System.out.print(e);
                        }
                        cr = new ClientRead();
                        cw = new ClientWrite();
                }
                public class ClientRead extends Thread {
                     public boolean nameRecieved = false;
                     public boolean reading = true;
                String playerName;
                    public void run(){
                        try{
                            in  = new ObjectInputStream(sock.getInputStream());
                            while(sock.isConnected() && reading)
                            {
                               //code to retrieve and update user location
                            }
                        }
                        catch(Exception e){
                                System.out.print(e);
                    }
                }
        }

And finally, a short class to write information to the client:

public class ClientWrite extends Thread {
                 public boolean writing = true;
                    public void run() {
                        try{

                            out = new ObjectOutputStream(sock.getOutputStream());
                            out.flush();
                            while(sock.isConnected() && writing)
                            {
                                out.flush();
                                while(!packs.isEmpty())
                                {
                                    out.writeObject(packs.firstElement());
                                    System.out.print(packs.firstElement());
                                    packs.remove(0);
                                    out.flush();
                                }
                            }

                        }
                        catch(Exception e)
                        {
                            System.out.print(e);
                        }
                    }
                }

I know there’s a ton of code but if somebody sees anyting that immediately jumps out (especially with the threads) that can explain the behavior im getting. To recap, the server stays at 0% cpu until a client connects. After the first client connects, no matter how many more connect, it stays from 25%-30% CPU. However, when all of the client have disconnected, the CPU STAYS at that 25%-30% instead of going back down to 0.

  • 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-18T15:35:23+00:00Added an answer on June 18, 2026 at 3:35 pm

    The problem is here:

    while(!clientList.get(z).cr.nameRecieved)
    {
        //do nothing untill client has provided it's name
    }
    

    It is a spin loop that smokes the CPU. You’re doing this all wrong. As soon as you have accepted the socket, you must start a new thread to handle it. That thread should block until the client has provided its name as part of your protocol handshake, and then update your data structures. The accept loop shouldn’t do anything except accept connections and start threads.

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

Sidebar

Related Questions

Well, I'm working on an online game in Java. I have a client and
I'm writing a small game in java. There's a server and a client module.
I have a very simple client server code written java(server listens on some port
I have client-server app. Client on C++, server on Java. I am sending byte-stream
I have a client and server communicating via Spring remoting (using Java Serialization) over
I have developed a java swing client-server application. The server has many services like
Background: I have written a java swing based client server application. The server is
I have created a server in java which accepts client connections. But I am
I'm developing a multiplayer turn based strategy game in Java (both client and server).
I have a client/server program that attempts to send and receive an object. There

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.