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

  • Home
  • SEARCH
  • 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 4024524
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:43:56+00:00 2026-05-20T10:43:56+00:00

I am currently developing a prototype for a game and I need a simple

  • 0

I am currently developing a prototype for a game and I need a simple server to run it.
At this stage, I don’t want to invest the time learning about all the different full-featured multiplayer game servers already there (smartfox, etc…)

I know how to develop a basic Server with Threads listening on Sockets but I have ran into a roadblock. Here’s the run() function of the Thread

public void run() {
        try {
            out = new PrintWriter(mSocket1.getOutputStream(), true);
            in = new BufferedReader( new InputStreamReader( mSocket1.getInputStream() ) );
            String inputLine1 = null, outputLine;

            out.println("hello");
            out.flush();

            while( (inputLine1 = in.readLine()) != null) {
                outputLine = mGameControl.processInput(mPlayerNum, inputLine1);
                out.println(outputLine);
                out.flush();
                if(outputLine.contentEquals("bye"))
                    break;
            }

            Terminate();
        }
        catch(IOException e) { e.printStackTrace(); }
    }

Now my problem is that the thread is blocked waiting for input. I do have other similar Threads connected to other client which may result in information being dispatched to all clients…

How can I modify it so that a different Thread can interact with it and push info to the client?

  • 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-20T10:43:57+00:00Added an answer on May 20, 2026 at 10:43 am

    Just write a synchronised public method which writes to your PrintWriter, and allow other threads to use it to send messages to your client. Call the same method from your read loop to avoid two threads writing at the same time.

    Here’s a tested example:

    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    
    public class SocketTest {
        public static class Client implements Runnable {
    
            private final BufferedReader in;
            private final PrintWriter out;
    
            public Client(Socket clientSocket) throws IOException {
                out = new PrintWriter(clientSocket.getOutputStream(), true);
                in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream() ) );
            }
    
            public void run() {
                send("Hello");
                String inputLine1 = null, outputLine;
                try {
                    while( (inputLine1 = in.readLine()) != null) {
                        outputLine = inputLine1.toLowerCase();
                        System.out.println(inputLine1);
                        send(outputLine);
                        if(outputLine.contentEquals("bye"))
                            break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            public synchronized void send(String message) {
                out.println(message);
                out.flush();
            }
        }
        public static void main(String[] args) throws IOException, InterruptedException {
            ServerSocket s = new ServerSocket(5050);
            Socket clientSocket = s.accept();
            Client client = new Client(clientSocket);
            Thread clientThread = new Thread(client);
            clientThread.start();
            int i = 1;
            while (true) {
                Thread.sleep(1000);
                client.send("Tick " + (i++));
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are currently developing a server whereby a client requests interest in changes to
I'm currently developing a web application in ASP.Net with SQL Server and I would
I am developing a prototype for a game, and certain gameplay rules are to
Currently developing an application using the newest version of symfony, obtained through PEAR. This
Currently developing a PHP framework and have ran into my first problem. I need
I'm currently developing a prototype using AS3 and trying out some touchscreen gestures. I'm
Currently developing this site and I can't pin point as to why there is
Im currently developing this site so none of the content on there is actually
Im currently developing this site so none of the content on there is actually
I'm currently developing a web application for one of my clients. This client requested

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.