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:
- Client establishes connection with server->
- client sends messages to the server based on what key is pressed on the client side->
- 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();
}
}
}
}
ServerSocketobject, so there would be no point in having a list of them, or iterating over it. Something wrong somewhere. - user207421