Я не очень хорошо разбираюсь в Java и хотел написать простую клиент-серверную программу (на сегодняшний день), но я не знаю, как разделить переменную termins
между потоками (или где я допустил ошибку).Любой совет будет принят.Я застрял на этом несколько часов.
Вот мой код:
Termin.java:
public class Termin {
public volatile int hour;
public volatile boolean reserved = false;
public volatile String user = null;
public Termin(int hour) {
this.hour = hour;
}
public void setHour(int value) {
this.hour = value;
}
...
Server.java:
public class Server {
public static final int PORT = 4444;
public volatile static Termin[] termins = new Termin[] {
new Termin(8),
new Termin(9),
new Termin(10),
new Termin(11),
new Termin(12),
new Termin(13),
new Termin(14),
new Termin(15),
new Termin(16),
new Termin(17),
};
public static void main(String[] args) throws IOException{
Server server = new Server();
server.runServer();
}
public void runServer() throws IOException{
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server up and ready for connections...");
while(true){
Socket socket = serverSocket.accept();
new ServerThread(socket).start();
}
}
}
ServerThread.java:
public class ServerThread extends Thread {
Socket socket;
ServerThread(Socket socket){
this.socket = socket;
}
public void run(){
try {
String message = null;
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("user '" + bufferedReader.readLine() + "' is now connected to the server");
while ((message = bufferedReader.readLine()) != null) {
System.out.println("incoming client message: " + message);
printWriter.println(message);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Как подключиться Clint.java:
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException{
String name = args[0];
Socket socket = new Socket("localhost", Server.PORT);
BufferedReader bufferedReaderFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.println(name);
BufferedReader bufferedReaderFromCmdPrompt = new java.io.BufferedReader(new InputStreamReader(System.in));
И фрагмент в Client.java , где я изменяю значение термов:
try {
String newReaderInput = bufferedReaderFromCmdPrompt.readLine();
printWriter.println(newReaderInput);
int var = Integer.parseInt(bufferedReaderFromClient.readLine());
if(var == 10 || var == 1 || var == 2 || var == 3 || var == 4 || var == 5 || var == 6 || var == 7 || var == 8 || var == 9 ) {
if(Server.termins[var].reserved == false) {
Server.termins[var-1].setReserved(true);
Server.termins[var-1].setUser(name);
System.out.println("\n\nTermin reserved.\n\n");
x=0;
} else {
System.out.println("\n\nError. Termin already booked\n\n");
}
} else {
System.out.println("\n\nError. No termin like this.!!!\n\n");
displayYourTermins(name);
}
} catch (NumberFormatException e) {
System.out.println("\n\nError. Write a number!!!\n\n");
displayYourTermins(name);
}