1
votes

so iv been programming for awhile and im trying for the first time to make a multiplayer game. The main problem im having right now is updating the data of multiple clients on the server side simultaneously.

heres the concept behind my code:

  1. Client establishes connection with server->
  2. client sends messages to the server based on what key is pressed on the client side->
  3. server updates itself 60 times a second based off of what key is currently being pressed by the client

my main problem is with step 3, and my problem is a syntax error in my main thread:

public static void updatethings() {
    for (ServerSocket i : ssList) {
        i.calculatePos();
    }
}

error message is:The method calculatePos() is undefined for the type ServerSocket.

what I tried to do was to make an array of ServerSockets, and update them all 60 times a second. each socket is its own thread of the server class.

Iv listed the entire server program below:

This is the main class for the server. This class listens for client connections and makes a new thread each time a client connects.

public class ServerMain {

    static ArrayList<ServerSocket> ssList = new ArrayList<ServerSocket>();
    static int oh = 0;

    public static void main(String[] args) throws IOException {

        int portNumber = 9000;
        boolean listening = true;
        UpdaterThread update = new UpdaterThread();
        update.start();

        try (ServerSocket serverSocket = new ServerSocket(portNumber)) {

            while (listening) {

                new ServerThread(serverSocket.accept()).start();
                ssList.add(serverSocket);

            }
        } catch (IOException e) {
            System.err.println("Could not listen on port " + portNumber);
            System.exit(-1);
        }
    }

    //does not work
    public static void updatethings() {
        for (ServerSocket i : ssList) {
            ServerSocket.calculatePos();
        }
    }
}

server thread class:

public class ServerThread
        extends Thread {
    private Socket socket = null;
    byte input;
    private float playery;
    private float playerx;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    public void calculatepos() {
        if (input == 0) {
        }
        if (input == 1) {
            playery = playery + 1;
        }
        if (input == 2) {
            playerx = playerx - 1;
        }
        if (input == 3) {
            playery = playery - 1;
        }
        if (input == 4) {
            playerx = playerx + 1;
        }
        if (input == 5) {
            playerx -= 0.7;
            playery += 0.7;
        }
        if (input == 6) {
            playerx -= 0.7;
            playery -= 0.7;
        }
        if (input == 7) {
            playerx += 0.7;
            playery -= 0.7;
        }
        if (input == 8) {
            playerx += 0.7;
            playery += 0.7;
        }
        System.out.println(playerx);
        System.out.println(playery);
    }

    public void run() {
        try (
                // PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                                socket.getInputStream()));
        ) {
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                input = Byte.parseByte(inputLine);
            }
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

updater thread class:

class UpdaterThread
        extends Thread {
    float time = 0;
    float lasttime = 0;

    public void run() {
        while (true) {
            time = Time.getTime();
            if (time > 1000 / 60 + lasttime) {
                lasttime = time;
                ServerMain.updatethings();
            }
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
1
did you write ServerSocket yourself? If so, write that method. If not go into the API for it to find out how you calculate the position. - La-comadreja
It's unlikely that you even have more than one ServerSocket object, so there would be no point in having a list of them, or iterating over it. Something wrong somewhere. - user207421

1 Answers

0
votes

Your error comes from Server. You are storing same server socket over and over again. And you do not store your ServerThread object

ServerSocket accept() - http://download.java.net/jdk7/archive/b123/docs/api/java/net/ServerSocket.html#accept()

Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. A new Socket s is created and, if there is a security manager, the security manager's checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.

Returns: the new Socket

So what you should do to handle clients is to save this ServerThread object. And if you want relevant returning socket too. !

public class ServerMain  {

static ArrayList <Socket> sSocketList = new ArrayList<Socket>();
 static ArrayList <ServerThread> sObjectList = new ArrayList<ServerThread>();
static int oh = 0;
public static void main(String[] args) throws IOException  {

    int portNumber =9000;
    boolean listening = true;
    UpdaterThread update =new UpdaterThread();
    update.start();

    try (ServerSocket serverSocket = new ServerSocket(portNumber)) { 

        while (listening) {

            Socket s=  serverSocket.accept();
            ServerThread st= new ServerThread(s);
            sSocketList.add(s);
            sObjectList.add(st);
            st.start();

        }
    } catch (IOException e) {
        System.err.println("Could not listen on port " + portNumber);
        System.exit(-1);
    }
}
//This might work now
public static void updatethings() {
    for (ServerThread  i : sObjectList){
     i.calculatePos();
    }
}
}