Skip to content
Snippets Groups Projects
Commit 5eb63490 authored by Elrabu's avatar Elrabu
Browse files

Update: Test server can now store integer variables.

parent d3eda617
No related branches found
No related tags found
2 merge requests!35Update: Added more Stuff to be transported over the server.,!2Merge of Multiplayer Feature into development branch
......@@ -20,9 +20,14 @@ public class Client {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public String sendMessage(String msg) throws IOException {
out.println(msg);
return in.readLine();
public int sendMessage(int number) throws IOException {
out.println(number);
return number;
}
public int receiveInt() throws IOException {
String response = in.readLine();
return Integer.parseInt(response);
}
public void stopConnection() throws IOException {
......
package de.hdm_stuttgart.battlearena.Model.Multiplayer.TestServer;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class ConnectionHandling {
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException, InterruptedException {
Client client = new Client();
client.startConnection("localhost", 4444);
String resp1 = client.sendMessage("hello");
System.out.println(resp1);
for (int i = 0; i < 10; i++){
int resp1 = client.sendMessage(1);
System.out.println(resp1);
TimeUnit.SECONDS.sleep(1);
}
}
}
......@@ -10,6 +10,8 @@ import java.io.*;
public class Server {
private ServerSocket serverSocket;
private static BufferedReader px;
public static void main(String[] args) throws IOException {
Server server = new Server();
server.start(4444);
......@@ -41,27 +43,26 @@ public class Server {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
BufferedReader inputLine = in;
px = in;
while (clientSocket.isConnected() && (inputLine = in.readLine()) != null) {
while (true) {
System.out.println("Received message from client: " + inputLine);
if (".".equals(inputLine)) {
if (inputLine.equals(0)) {
out.println("bye");
out.flush();
System.out.println("Sent response to client: bye");
break;
}
out.println(inputLine);
out.println(px);
out.flush();
System.out.println("Sent response to client: " + inputLine);
System.out.println("Sent response to client: " + px);
}
} catch (SocketException e) {
// Connection reset by client; no need to print a stack trace
System.out.println("Client connection reset.");
} catch (IOException e) {
// Other IOExceptions - log or handle as needed
e.printStackTrace();
} finally {
try {
......@@ -69,7 +70,6 @@ public class Server {
out.close();
clientSocket.close();
} catch (IOException e) {
// Log the exception or handle as appropriate for your application
e.printStackTrace();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment