Hallo. Ich habe ein Problem mit meinem Server und Client. Der Server soll Punktezahlen an alle Clients senden. Bei einem Client wird nur dem Client die Punktezahl zugeschrieben, bei zwei Clients gibt der Server beide Punktezahlen an jeden Client, bei drei Clients drei Punktezahlen, die der Server an alle Clients versendet, usw...
Jedoch ist es momentan so, dass wenn sich ein weiterer Client anmeldet, dass nur der neue und nicht der/die vorherige/n die Punktezahl bekommt.
[CODE lang="java" title="Server.java"]package application;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static application.Client.readReceivedScore;
public class Server implements Serializable {
public static int PORT = 10024;
public static ServerSocket serverSocket;
public static boolean checkScore = true;
public static HashMap<String, Integer> clientsScoreMap = new HashMap<>();
static List<Client> connections = new ArrayList<Client>(); // Array List of all connections
public static Socket verbindung;
public static void main(String[] args) throws IOException {
serverSocket = new ServerSocket(PORT);
System.out.println("Server running on " + InetAddress.getLocalHost() + " and Port " + PORT);
Thread wait = new Thread(() -> {
while (true) try {
waitForClient(serverSocket.accept());
} catch (Exception e) {
e.printStackTrace();
}
});
wait.start();
}
public static void waitForClient(Socket connection) throws IOException {
Client client = new Client(connection);
connections.add(client);
client.start();
}
}
[/CODE]
[CODE lang="java" title="Client.java"]package application;
import java.io.*;
import java.net.Socket;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static application.Server.*;
public class Client extends Thread implements Serializable{
static Socket connection;
static ObjectInputStream in;
static ObjectOutputStream out;
public Client(Socket connection) throws IOException {
Client.connection = connection;
in = new ObjectInputStream(connection.getInputStream());
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
}
public static void readReceivedScore() {
Runnable helloRunnable = () -> {
try {
if (in.readUTF().equals("Unknown Player,0")) return;
//writeToAll(in.readUTF());
clientsScoreMap.put(in.readUTF().split(",")[0], Integer.parseInt(in.readUTF().split(",")[1]));
clientsScoreMap.forEach((key, value) -> {
if (key.equals("Unknown Player")) return;
try {
writeToAll(key + "," + value);
//out.writeUTF(key + "," + value + "," + connections.size());
//out.reset();
} catch (IOException e) {
e.printStackTrace();
}
});
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
};
ScheduledExecutorService executor2 = Executors.newScheduledThreadPool(1);
executor2.scheduleAtFixedRate(helloRunnable, 0, 5, TimeUnit.SECONDS);
}
@Override
public void run() {
System.out.println("Connected to " + connection.getRemoteSocketAddress());
readReceivedScore();
}
public static void write(String obj) throws IOException {
out.writeUTF(obj);
out.flush();
}
public static void writeToAll(String obj) throws IOException {
for (Client c : connections) {
c.write(obj);
}
}
}[/CODE]
[CODE lang="java" title="main.java"] public static void sendScore(GraphicsContext gc) {
Runnable helloRunnable = new Runnable() {
public void run() {
try {
out.writeUTF(validUsername + "," + score);
out.flush();
System.out.println("SENT");
} catch (IOException e) {
e.printStackTrace();
}
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 1, TimeUnit.SECONDS);
}
public static void readAllScores() {
Runnable helloRunnable = () -> {
try {
System.out.println(in.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
};
ScheduledExecutorService executor2 = Executors.newScheduledThreadPool(1);
executor2.scheduleAtFixedRate(helloRunnable, 0, 5, TimeUnit.SECONDS);
}
public static long startingTime;
public static void main(String[] args) {
try {
verbindung = new Socket("localhost", 10024);
System.out.println("Verbunden");
out = new ObjectOutputStream(verbindung.getOutputStream());
in = new ObjectInputStream(verbindung.getInputStream());
launch(args);
} catch (Exception e) {
System.out.println("Verbindung fehlgeschlagen");
}
//sounds.playBackgroundMusic();
// TODO: Music
// Sets Background Music
// sounds.playBackgroundMusic();
//Application.launch(args);
//myLaunch(main.class);
}[/CODE]
Jedoch ist es momentan so, dass wenn sich ein weiterer Client anmeldet, dass nur der neue und nicht der/die vorherige/n die Punktezahl bekommt.
[CODE lang="java" title="Server.java"]package application;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static application.Client.readReceivedScore;
public class Server implements Serializable {
public static int PORT = 10024;
public static ServerSocket serverSocket;
public static boolean checkScore = true;
public static HashMap<String, Integer> clientsScoreMap = new HashMap<>();
static List<Client> connections = new ArrayList<Client>(); // Array List of all connections
public static Socket verbindung;
public static void main(String[] args) throws IOException {
serverSocket = new ServerSocket(PORT);
System.out.println("Server running on " + InetAddress.getLocalHost() + " and Port " + PORT);
Thread wait = new Thread(() -> {
while (true) try {
waitForClient(serverSocket.accept());
} catch (Exception e) {
e.printStackTrace();
}
});
wait.start();
}
public static void waitForClient(Socket connection) throws IOException {
Client client = new Client(connection);
connections.add(client);
client.start();
}
}
[/CODE]
[CODE lang="java" title="Client.java"]package application;
import java.io.*;
import java.net.Socket;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static application.Server.*;
public class Client extends Thread implements Serializable{
static Socket connection;
static ObjectInputStream in;
static ObjectOutputStream out;
public Client(Socket connection) throws IOException {
Client.connection = connection;
in = new ObjectInputStream(connection.getInputStream());
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
}
public static void readReceivedScore() {
Runnable helloRunnable = () -> {
try {
if (in.readUTF().equals("Unknown Player,0")) return;
//writeToAll(in.readUTF());
clientsScoreMap.put(in.readUTF().split(",")[0], Integer.parseInt(in.readUTF().split(",")[1]));
clientsScoreMap.forEach((key, value) -> {
if (key.equals("Unknown Player")) return;
try {
writeToAll(key + "," + value);
//out.writeUTF(key + "," + value + "," + connections.size());
//out.reset();
} catch (IOException e) {
e.printStackTrace();
}
});
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
};
ScheduledExecutorService executor2 = Executors.newScheduledThreadPool(1);
executor2.scheduleAtFixedRate(helloRunnable, 0, 5, TimeUnit.SECONDS);
}
@Override
public void run() {
System.out.println("Connected to " + connection.getRemoteSocketAddress());
readReceivedScore();
}
public static void write(String obj) throws IOException {
out.writeUTF(obj);
out.flush();
}
public static void writeToAll(String obj) throws IOException {
for (Client c : connections) {
c.write(obj);
}
}
}[/CODE]
[CODE lang="java" title="main.java"] public static void sendScore(GraphicsContext gc) {
Runnable helloRunnable = new Runnable() {
public void run() {
try {
out.writeUTF(validUsername + "," + score);
out.flush();
System.out.println("SENT");
} catch (IOException e) {
e.printStackTrace();
}
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 1, TimeUnit.SECONDS);
}
public static void readAllScores() {
Runnable helloRunnable = () -> {
try {
System.out.println(in.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
};
ScheduledExecutorService executor2 = Executors.newScheduledThreadPool(1);
executor2.scheduleAtFixedRate(helloRunnable, 0, 5, TimeUnit.SECONDS);
}
public static long startingTime;
public static void main(String[] args) {
try {
verbindung = new Socket("localhost", 10024);
System.out.println("Verbunden");
out = new ObjectOutputStream(verbindung.getOutputStream());
in = new ObjectInputStream(verbindung.getInputStream());
launch(args);
} catch (Exception e) {
System.out.println("Verbindung fehlgeschlagen");
}
//sounds.playBackgroundMusic();
// TODO: Music
// Sets Background Music
// sounds.playBackgroundMusic();
//Application.launch(args);
//myLaunch(main.class);
}[/CODE]