Add: Network structure, View structure
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package it.polimi.ingsw.gc14.Network.Client;
|
||||
|
||||
import it.polimi.ingsw.gc14.View.IView;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class TCPClient {
|
||||
public TCPClient(IView view) {
|
||||
IView iView = view;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
String hostName = "127.0.0.1";
|
||||
int portNumber = 5200;
|
||||
Socket communicationSocket = null;
|
||||
try {
|
||||
communicationSocket = new Socket(hostName, portNumber);
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println(e.toString() + " " + hostName);
|
||||
System.exit(1);
|
||||
}
|
||||
PrintWriter out = null;
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
out = new PrintWriter(communicationSocket.getOutputStream(), true);
|
||||
in = new BufferedReader(new InputStreamReader(communicationSocket.getInputStream()));
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.toString() + " " + hostName);
|
||||
System.exit(1);
|
||||
}
|
||||
String userInput = "";
|
||||
while (true) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package it.polimi.ingsw.gc14.Network.Server;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.List;
|
||||
|
||||
public class ClientHandler implements Runnable {
|
||||
private Socket clientSocket;
|
||||
BufferedReader in = null;
|
||||
PrintWriter out = null;
|
||||
List<ClientHandler> clientHandlers ;
|
||||
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers) {
|
||||
this.clientSocket = clientSocket;
|
||||
this.clientHandlers = clientHandlers;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
clientLoop();
|
||||
}
|
||||
private void clientLoop() {
|
||||
|
||||
try {
|
||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
synchronized (out) {
|
||||
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String s = "";
|
||||
try {
|
||||
while ((s = in.readLine()) != null) {
|
||||
System.out.println(s);
|
||||
out.println(s.toUpperCase());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
private void notifyEvent(Object event) {
|
||||
synchronized (out) {
|
||||
out.println(event.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package it.polimi.ingsw.gc14.Network.Server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TCPServer {
|
||||
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");
|
||||
e.printStackTrace();
|
||||
return ;
|
||||
}
|
||||
System.out.println("Listening on port 5200" );
|
||||
while (true) {
|
||||
Socket clientSocket = null;
|
||||
try {
|
||||
clientSocket = serverTCP.accept();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Accepted");
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket,clientHandlers);
|
||||
clientHandlers.add(clientHandler);
|
||||
Thread t = new Thread(clientHandler);
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package it.polimi.ingsw.gc14.View;
|
||||
|
||||
public interface IView {
|
||||
}
|
||||
Reference in New Issue
Block a user