Skip to content
Snippets Groups Projects
Commit 0ebac858 authored by Busch Elias's avatar Busch Elias
Browse files

Added: x and y cord as array send to server.

parent 7b615339
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
......@@ -6,6 +6,7 @@ import org.apache.logging.log4j.Logger;
import java.net.*;
import java.io.*;
import java.util.Arrays;
public class Client {
private Socket clientSocket;
......@@ -20,14 +21,11 @@ public class Client {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public int sendX(int number) throws IOException {
out.println("x:" + number);
return number;
}
public int sendY(int number) throws IOException {
out.println("y:" + number);
return number;
public int[] sendcords(int[] cords) throws IOException {
String message = String.join(",", Arrays.stream(cords).mapToObj(String::valueOf).toArray(String[]::new));
out.println(message);
log.info("Sent coordinates: " + message);
return cords;
}
......
package de.hdm_stuttgart.battlearena.Model.Multiplayer.TestServer;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.concurrent.TimeUnit;
public class ConnectionHandling {
private int px;
private int py;
private static int[] coordinates = {5,10};
public static void main(String[] args) throws IOException, InterruptedException {
Client client = new Client();
client.startConnection("localhost", 4444);
for (int i = 1; i < 10; i++){
int x = client.sendX(1+i);
System.out.println(x);
TimeUnit.MILLISECONDS.sleep(100);
int[] cords = client.sendcords(coordinates);
System.out.println(cords);
}
int y = client.sendY(i);
System.out.println(y);
public int getPx() {
return px;
}
TimeUnit.SECONDS.sleep(1);
}
public int getPy() {
return py;
}
}
......@@ -6,6 +6,7 @@ import org.apache.logging.log4j.Logger;
import java.net.*;
import java.io.*;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
......@@ -17,6 +18,8 @@ public class Server {
private static int px;
private static int py;
private static int[] cords;
public static void main(String[] args) throws IOException {
Server server = new Server();
log.info("server starting...");
......@@ -55,33 +58,24 @@ public class Server {
String inputLine = localIn.readLine();
log.info(inputLine);
if (inputLine != null) {
if (inputLine.startsWith("x:")) {
int receivedX = Integer.parseInt(inputLine.substring(2));
px = receivedX;
localOut.println(inputLine);
localOut.flush();
log.info("Current X and Y: " + px + " / " + py);
log.info("Sent response to client: " + receivedX);
} else if (inputLine.startsWith("y:")) {
int receivedY = Integer.parseInt(inputLine.substring(2));
py = receivedY;
localOut.println(inputLine);
localOut.flush();
log.info("Current X and Y: " + px + " / " + py);
log.info("Sent response to client: " + receivedY);
// Assuming that the input line is a comma-separated list of integers
String[] coordinates = inputLine.split(",");
cords = new int[coordinates.length];
for (int i = 0; i < coordinates.length; i++) {
cords[i] = Integer.parseInt(coordinates[i]);
}
}
log.info(cords);
} catch (IOException e) {
throw new RuntimeException(e);
}
}, 0, 250, TimeUnit.MILLISECONDS);
}
}
}
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