Add: Initial TCP/Ip Implementation.

This commit is contained in:
GabrieleRadice
2026-04-18 15:45:11 +02:00
parent c4fe810776
commit ffdc8f6d88
3 changed files with 37 additions and 17 deletions
@@ -5,7 +5,7 @@ import it.polimi.ingsw.gc14.View.IView;
import java.io.*;
import java.net.*;
public class TCPClient {
public class TCPClient implements Serializable {
public TCPClient(IView view) {
IView iView = view;
}
@@ -1,5 +1,7 @@
package it.polimi.ingsw.gc14.Network.Server.TCP;
import it.polimi.ingsw.gc14.Controller.GameController;
import java.io.*;
import java.net.*;
import java.util.List;
@@ -9,16 +11,17 @@ public class ClientHandler implements Runnable {
BufferedReader in = null;
PrintWriter out = null;
List<ClientHandler> clientHandlers;
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers) {
GameController gameController;
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, GameController gameController) {
this.clientSocket = clientSocket;
this.clientHandlers = clientHandlers;
this.gameController = gameController;
}
@Override
public void run() {
clientLoop();
}
private void clientLoop() {
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
synchronized (out) {
@@ -33,7 +36,8 @@ public class ClientHandler implements Runnable {
System.out.println(s);
out.println(s.toUpperCase());
}
} catch (IOException e) {
}
catch (IOException e) {
e.printStackTrace();
}
}
@@ -1,35 +1,51 @@
package it.polimi.ingsw.gc14.Network.Server.TCP;
import it.polimi.ingsw.gc14.Controller.GameController;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
public class TCPServer {
int port = -1;
ServerSocket serverTCP = null;
GameController gameController;
private List<ClientHandler> clientHandlers;
public void start(String args[]){
clientHandlers = new ArrayList<>();
ServerSocket serverTCP=null ;
try{
serverTCP=new ServerSocket(5200);
}catch (IOException e){
System.out.println("Could not listen on port: 5200");
serverTCP = new ServerSocket(port);
}
catch (IOException e){
System.out.println("Could not listen on port: " + port);
e.printStackTrace();
return;
}
System.out.println("Listening on port 5200" );
System.out.println("Listening on port " + port);
while(true){
Socket clientSocket = null;
try {
clientSocket = serverTCP.accept();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Accepted");
ClientHandler clientHandler = new ClientHandler(clientSocket,clientHandlers);
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, this.gameController);
clientHandlers.add(clientHandler);
Thread t = new Thread(clientHandler);
t.start();
}
}
private TCPServer(GameController gameController, int port) {
this.port = port;
this.gameController = gameController;
}
}