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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:39:16+00:00 2026-05-27T16:39:16+00:00

I’m making a server/client application in Java but it doesn’t really work the way

  • 0

I’m making a server/client application in Java but it doesn’t really work the way I want it to.

I make a connection and everything goes fine but then it does nothing. I’m thinking it doesn’t come to my thread for accepting the client.

Here’s my code.

Networking Thread:

package libgdx.server;


import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.net.*;


public class NetworkingThread extends Thread {
private Socket sock= null;
 private int ID;

public NetworkingThread(Socket sock){
    super("Multiple connection thread!");

    this.sock = sock;

 }

@Override
public void run()  {
   try{
   System.out.println("In the Method run() in the thread!");

       PrintWriter out = new PrintWriter(sock.getOutputStream(),true);

    BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    String inputLine,outputLine;
    AlphaProtocol alpha = new AlphaProtocol();
    outputLine = alpha.ProcessInput(null);
    out.println(outputLine);

    out.println("Welcome to the Server! Hope you enjoy your stay.");
    while ((inputLine  = in.readLine()) != null){
        outputLine = alpha.ProcessInput(inputLine);
        out.println(outputLine);
        if (outputLine.equals("Bye!")){
            break;
        }
    }
    out.close();
    in.close();
   sock.close();
   }catch(IOException ioe){
       ioe.printStackTrace();
       System.err.println("Error in the Tread of Connecting and Method Run()");
   }

}

}

This is the tread on the server for accepting clients and handling them 🙂

Main class on server:

package libgdx.server;

import java.io.IOException;
import java.net.ServerSocket;

/**
 *
 * @author Saturn
 */
 public class MainServer {
 private static ServerSocket Server = null;
 private static boolean networking = true;


public static void main(String[] args) throws IOException{
    try{
        System.out.println("Server listening!");
        Server = new ServerSocket(4444);


    }catch (IOException io){
     System.err.println("Error while making ServerSocket!");
     System.exit(-1);
    }
    while (networking)
    System.out.println("Networking!");

        new NetworkingThread(Server.accept()).start();     

}

}

Protocol of server:

  /*
 * To change this template, choose Tools | Templates
  * and open the template in the editor.
*/
package libgdx.server;

import java.util.Calendar;
import java.util.Date;

/**
*
* @author Saturn
*/
public class AlphaProtocol {
    private static final int CONNECTING = -1;

    private static final int MOVE = 0;
    private static final int NEW_PLAYER = 1;
    private int state  =  CONNECTING;
    private Calendar Date;
    private Date Time = Date.getTime();

public String ProcessInput(String input){
    String output = null;
    String name = null;
    String X= null,Y = null;

    if (input.equals("connect")){
      System.out.println("Connection granted!");

        output =  Time + ":" + "Got a Connection over here!";
    }
    if (input.equals("Bye!")){
        System.err.println("Client said Bye!");
    }
    return output;

}

}

Now these are the client files:

Desktop:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package libgdx.test;

import com.badlogic.gdx.backends.jogl.JoglApplication;

 /**
  *
  * @author Saturn
  */
public class Desktop {
   public static void main(String[] args) {
    // TODO code application logic here
    new JoglApplication(new LibGDXTest(),"Test #1",640,480,false);

}

}

LibGDXTest:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
   package libgdx.test;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.jogl.JoglApplication;
import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import java.util.Scanner;

 /**
 *
 * @author Saturn
 */
  public class LibGDXTest implements ApplicationListener {
    SpriteBatch spriteBatch;
    Texture texture;
    BitmapFont font;
    Vector2 textPosition = new Vector2(100, 100);
    Vector2 textDirection = new Vector2(1, 1);
    String ip;
    int port;
    Networking net = new Networking();

    /**
    * @param args the command line arguments
    */


      @Override
    public void create() {
     Scanner in = new Scanner(System.in);
    System.out.println("Hello, welcome to LibGDX Network test: #1 ");
    System.out.println("Type the server ip in:");
   ip = in.next();
   System.out.println("Type the server port in:");
   port = in.nextInt();
   System.out.println("IP:" + ip + "Port:" + port);
   net.Connect(ip,port);

   }

    @Override
    public void resize(int i, int i1) {

  }

   @Override
   public void render() {

  }

    @Override
    public void pause() {

   }

   @Override
   public void resume() {

   }

   @Override
  public void dispose() {

   }

}

Networking: is the class that handeles all the networking on the client side 🙂

   /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package libgdx.test;

   /**
    *
   * @author Saturn
   */
      import java.io.BufferedReader;
      import java.io.IOException;
       import java.io.InputStreamReader;
       import java.io.PrintWriter;
       import java.net.*;
     import java.util.logging.Level;
     import java.util.logging.Logger;

   public class Networking {

   private Socket Client;
    private  PrintWriter out;
   private BufferedReader in;


   public void Connect(String arg1, int arg2){
       try{
           Client = new Socket(arg1,arg2);
                out = new PrintWriter(Client.getOutputStream(),true);
             in = new BufferedReader(new InputStreamReader(Client.getInputStream()));

        }catch(UnknownHostException uhk) {
           System.err.println("Cannot find host:" + arg1);
           System.exit(-1);

      }catch (IOException ioe) {
           System.err.println("Cannot get I/O for the connection:" + arg1);
          System.exit(-1);

       }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromUser;
        String fromServer;
        try {
            while ((fromServer = in.readLine()) != null) {
              System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye!"))
                  break;

              fromUser = stdIn.readLine();
          if (fromUser != null) {
                  System.out.println("Client: " + fromUser);
                  out.println(fromUser);
           }
          }
       } catch (IOException ex) {
           Logger.getLogger(Networking.class.getName()).log(Level.SEVERE, null, ex);
      }



   }
  }

When I run the application,

I get this:

   Client:    Hello, welcome to LibGDX Network test: #1 
   Type the server ip in:
   127.0.0.1 // My INPUT!
   Type the server port in:
   4444  // My INPUT!
   IP:127.0.0.1Port:4444

And the server:

Server listening!
Networking!
Networking!
Networking!
Networking!
Networking!
Networking!
Networking!

It just prints “Networking!”.

Any help would be appreciated!

  • 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-27T16:39:17+00:00Added an answer on May 27, 2026 at 4:39 pm

    Please do note that in your NetworkingThread class of package libgdx.server; you are calling outputLine = alpha.ProcessInput(null); of Class AlphaProtocol, but since you are providing null as an argument, so that function returns null as defined in your method. Since Variable input is null for which no condition satisfies to true, hence the output always remains null, which is returned from this method.

    public String ProcessInput(String input){
      String output = null;
      String name = null;
      String X= null,Y = null;
    
      if (input.equals("connect")){
        System.out.println("Connection granted!");
    
          output =  Time + ":" + "Got a Connection over here!";
      }
      if (input.equals("Bye!")){
          System.err.println("Client said Bye!");
      }
      return output;
    }
    

    so in the Networking Class of Client side on receiving that null value, in this code of yours while ((fromServer = in.readLine()) != null) , the Client shuts itself down.
    So please try to provide some String value in place of null here,
    outputLine = alpha.ProcessInput(“Hello Client”);`

    I guess that’s the issue you should be aiming at to make things work.

    Regards`

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
I have thousands of HTML files to process using Groovy/Java and I need 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.