Add: Initial TCP/Ip Implementation.
This commit is contained in:
@@ -5,7 +5,7 @@ import it.polimi.ingsw.gc14.View.IView;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
public class TCPClient {
|
public class TCPClient implements Serializable {
|
||||||
public TCPClient(IView view) {
|
public TCPClient(IView view) {
|
||||||
IView iView = view;
|
IView iView = view;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.Server.TCP;
|
package it.polimi.ingsw.gc14.Network.Server.TCP;
|
||||||
|
|
||||||
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -9,16 +11,17 @@ public class ClientHandler implements Runnable {
|
|||||||
BufferedReader in = null;
|
BufferedReader in = null;
|
||||||
PrintWriter out = null;
|
PrintWriter out = null;
|
||||||
List<ClientHandler> clientHandlers;
|
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.clientSocket = clientSocket;
|
||||||
this.clientHandlers = clientHandlers;
|
this.clientHandlers = clientHandlers;
|
||||||
|
this.gameController = gameController;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
clientLoop();
|
clientLoop();
|
||||||
}
|
}
|
||||||
private void clientLoop() {
|
private void clientLoop() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||||
synchronized (out) {
|
synchronized (out) {
|
||||||
@@ -33,7 +36,8 @@ public class ClientHandler implements Runnable {
|
|||||||
System.out.println(s);
|
System.out.println(s);
|
||||||
out.println(s.toUpperCase());
|
out.println(s.toUpperCase());
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
}
|
||||||
|
catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +1,51 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.Server.TCP;
|
package it.polimi.ingsw.gc14.Network.Server.TCP;
|
||||||
|
|
||||||
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class TCPServer {
|
public class TCPServer {
|
||||||
|
int port = -1;
|
||||||
|
ServerSocket serverTCP = null;
|
||||||
|
GameController gameController;
|
||||||
|
|
||||||
private List<ClientHandler> clientHandlers;
|
private List<ClientHandler> clientHandlers;
|
||||||
|
|
||||||
public void start(String args[]){
|
public void start(String args[]){
|
||||||
clientHandlers = new ArrayList<>();
|
clientHandlers = new ArrayList<>();
|
||||||
ServerSocket serverTCP=null ;
|
|
||||||
try{
|
try{
|
||||||
serverTCP=new ServerSocket(5200);
|
serverTCP = new ServerSocket(port);
|
||||||
}catch (IOException e){
|
}
|
||||||
System.out.println("Could not listen on port: 5200");
|
catch (IOException e){
|
||||||
|
System.out.println("Could not listen on port: " + port);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.out.println("Listening on port 5200" );
|
System.out.println("Listening on port " + port);
|
||||||
|
|
||||||
while(true){
|
while(true){
|
||||||
Socket clientSocket = null;
|
Socket clientSocket = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
clientSocket = serverTCP.accept();
|
clientSocket = serverTCP.accept();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Accepted");
|
System.out.println("Accepted");
|
||||||
ClientHandler clientHandler = new ClientHandler(clientSocket,clientHandlers);
|
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, this.gameController);
|
||||||
clientHandlers.add(clientHandler);
|
clientHandlers.add(clientHandler);
|
||||||
Thread t = new Thread(clientHandler);
|
Thread t = new Thread(clientHandler);
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TCPServer(GameController gameController, int port) {
|
||||||
|
this.port = port;
|
||||||
|
this.gameController = gameController;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user