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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:37:59+00:00 2026-05-27T21:37:59+00:00

gameServer.java: import java.util.ArrayList; public class gameServer{ public static server server; public static gameRunner gameRunner;

  • 0

gameServer.java:

import java.util.ArrayList;

public class gameServer{

    public static server server;
    public static gameRunner gameRunner;
    public static ArrayList<packet> packets = new ArrayList<packet>();

    public static void main(String[] args) throws Exception {
        server = new server();
        server.startServer(5050);
        Runnable listenServer = new Runnable() {
            public void run() {
                while(true) {
                    packets.add(server.getPacket());
                }
            }
        };
        new Thread(listenServer).start();
        gameRunner = new gameRunner();
        while(true) {
            if(packets.size() <= 0)
                continue;
            gameRunner.executeMessage(packets.get(0));
            packets.remove(0);
        }
    }
}

server.js:

import java.net.*;
import java.util.Date;

public class server {

    public static DatagramSocket socketServer;
    private Date date = new Date();

    public void startServer (int port) throws SocketException {
        socketServer = new DatagramSocket(port);
        System.out.println("Started Server on port: " + port);
    }
public void sendPacket (packet packet) {
        String message = null;
        for(int i = 0; i < packet.header.length; i++) {
            message += packet.header[i] + "\r\n";
        }
        message += "\r\n" + packet.message;
        String[] info = packet.address.split(":");
        try {
            sendPacket(message, (Inet4Address)InetAddress.getByName(info[0]), Integer.parseInt(info[1]));
        } catch (Exception e) {
            System.out.println("failed to determine host");
        }
    }

}

gameRunner.js:

import java.util.ArrayList;
import java.util.Date;


public class gameRunner {

    public static ArrayList<object> objects = new ArrayList<object>();
    public static player[] players = new player[1000];

    public Date date = new Date();

    Runnable updatePlayers = new Runnable() {
        public void run () {
            while(true) {
                long StartTime = date.getTime();
                for(int i = 0; i < players.length; i++) {
                    if(players[i] == null)
                        continue;
                    players[i].updatePos();
                }
                while(StartTime + 100 < date.getTime()) { }
            }
        }
    };

    Runnable sendPlayerPackets = new Runnable () {
        public void run () {
            while(true) {
                parent.gameServer.sendPacket(player[0].packet); //<<<----
                }
        }
    };

}

I am new to classes, abstract, inherent and such. I need to be able to access socketServer object in gameServer from gameRunner to send a message. I am not sure how to do this, thanks in advance.

  • 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-27T21:38:00+00:00Added an answer on May 27, 2026 at 9:38 pm

    EDIT1 guessed right 🙂

    Some words on design:

    • Most of these statics are horrible, you should only make properties static when they are part of the class such as default values, Singleton business and when you are really drunk and lazy – but that’s a different topic (you will get the hang of that if you keep coding)

    • Actually you should not make properties public, they should always be private or protected, with a public getter/setter method – that way you can restrict access to the properties and can control what happens on a get or set. (but this tends to be very tedious in java)

    Now, keeping with the statics you could easily send a message like this (in sendPlayerPackets.run()): server.sendPacket(player[0].packet);

    To pass the server along to the gameRunner you would do:

    (in gameRunner.java)

    public class gameRunner {
      public static ArrayList<object> objects = new ArrayList<object>();
      public static player[] players = new player[1000];
    
      public server gameServer;
    
      /* ... */
      Runnable sendPlayerPackets = new Runnable () {
        public void run () {
            while(true) {
                gameServer.sendPacket(gameRunner.players[0].packet); //<<<----
                }
        }
    };
    

    and in gameServer.java:

    public class gameServer{
      /* ... */
      public static void main(String[] args) throws Exception {
        /* ... */
        gameRunner = new gameRunner();
        gameRunner.gameServer = server;
        /* ... */
      }
    }
    

    old part

    Purely on instinct I guess you want this:

    public class A {
        public B foo;
        public C bar;
    
        public A() {
          // java does not like exposing this, but screw that
          foo = new B(this, 10);
          C = new C(this);
        }
    }
    
    public class B {
        private A parent;
        public int numB;
    
        public B(A parent, int num) {
          this.parent = parent;
    
          numB = num;
        }
    }
    
    public class C {
        private A parent;
        public int numC;
    
        public C(A parent) {
          this.parent = parent;
    
          if(parent != null && parent.B != null)
            numC = parent.B.numB;
        }
    }
    

    In short: you have to pass the parent to the child for it to be able to access it.

    Note that in this example B has to be initialized before C, to get around this you would need events i guess. Anyways, the idea does not seem well defined, you should think about what you want to accomplish again (and tell us).

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

Sidebar

Related Questions

For my Java game server I send the Action ID of the packet which
I've written a simple multi-threaded game server in python that creates a new thread
Context : Master server (Java, TCP) monitoring a list of hosted games (a different
I am writing a game server for a turn-based game in Java. These are
I have a game server which is made in Java. I want to make
I've made a Perl script to start a Java game server, java -jar somejar.jar
Recently I published my open source java game server code to github and faced
I'm beginning to work on a game server written in Java. Granted, Java is
I'm making a multyplayer game and I'm using java sockets for the server, the
I already have implemented JOOQ with Union Platform as a java based game server

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.