Basics of client server communication and drawing of gamestates finished

This commit is contained in:
2021-02-18 11:42:11 +01:00
parent 8ed0e5196b
commit b7113b7ee8
15 changed files with 238 additions and 47 deletions

View File

@@ -2,6 +2,8 @@ package networking;
import logging.ClientLogger;
import logging.LogType;
import javax.swing.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@@ -37,8 +39,9 @@ public class Client {
out.writeInt(165313125);
out.flush();
success = in.readInt() == 200;
if (success){
if (success) {
out.writeUTF(name);
out.flush();
serverName = in.readUTF();
clientLogger.printLog("You successfully connected to me", serverName, success, LogType.Log);
} else {
@@ -55,18 +58,37 @@ public class Client {
out.writeUTF(message);
out.flush();
success = in.readInt() == 200;
clientLogger.printLog(String.format("Sent the message: %s", message), serverName, success, LogType.Log);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getGameState(){
public String getResponse() {
try {
out.writeUTF("gamestate");
return in.readUTF();
String message = in.readUTF();
clientLogger.printLog(String.format("Message recieved: %s", message), serverName, success, LogType.Log);
return message;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public String getGameState() {
this.sendToServer("gameState");
String gameState = this.getResponse();
return gameState;
}
public void exitProcess(){
try {
out.writeUTF("exit()");
out.flush();
success = in.readInt()==200;
clientLogger.printLog("Closing connection to server", serverName, success, LogType.Log);
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
@@ -74,6 +96,8 @@ public class Client {
return success;
}
public String getName(){return name;}
public String getName() {
return name;
}
}