From 768bd2859bb09baedb09cbacf025b81cb1ad5d4b Mon Sep 17 00:00:00 2001 From: LeeJooHwan Date: Sat, 13 Jun 2020 19:42:17 +0900 Subject: [PATCH 1/3] RMISSL --- RMISSLClientSocketFactory.java | 27 +++++++++++++++ RMISSLServerSocketFactory.java | 62 ++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 RMISSLClientSocketFactory.java create mode 100644 RMISSLServerSocketFactory.java diff --git a/RMISSLClientSocketFactory.java b/RMISSLClientSocketFactory.java new file mode 100644 index 0000000..a7a634f --- /dev/null +++ b/RMISSLClientSocketFactory.java @@ -0,0 +1,27 @@ +import java.io.*; +import java.net.*; +import java.rmi.server.*; +import javax.net.ssl.*; + +public class RMISSLClientSocketFactory + implements RMIClientSocketFactory, Serializable { + + public Socket createSocket(String host, int port) throws IOException { + SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault(); + SSLSocket socket = (SSLSocket)factory.createSocket(host, port); + return socket; + } + + public int hashCode() { + return getClass().hashCode(); + } + + public boolean equals(Object obj) { + if (obj == this) { + return true; + } else if (obj == null || getClass() != obj.getClass()) { + return false; + } + return true; + } +} \ No newline at end of file diff --git a/RMISSLServerSocketFactory.java b/RMISSLServerSocketFactory.java new file mode 100644 index 0000000..695fe6b --- /dev/null +++ b/RMISSLServerSocketFactory.java @@ -0,0 +1,62 @@ +import java.io.FileInputStream; +import java.io.IOException; +import java.net.ServerSocket; +import java.rmi.server.RMIServerSocketFactory; +import java.security.KeyStore; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; + +public class RMISSLServerSocketFactory implements RMIServerSocketFactory{ + private static SSLServerSocketFactory ssf = null; + + final String runRoot = "C:/eclipse/java/WarGame/bin/"; + String ksName = runRoot + ".keystore/SSLSocketServerKey"; + + public RMISSLServerSocketFactory() throws Exception{ + try { + KeyStore ks; + KeyManagerFactory kmf; + SSLContext sc ; + + + char[] keyStorePass = "123456".toCharArray(); + char[] keyPass = "123456".toCharArray(); + + ks = KeyStore.getInstance("JKS"); + ks.load(new FileInputStream(ksName), keyStorePass); + kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, keyPass); + + sc = SSLContext.getInstance("TLS"); + sc.init(kmf.getKeyManagers(), null, null); + ssf = sc.getServerSocketFactory(); + + } catch(Exception e) { + e.printStackTrace(); + throw e; + } + } + + + public ServerSocket createServerSocket(int port) throws IOException { + return ssf.createServerSocket(port); + } + + public int hashCode() { + return getClass().hashCode(); + } + + public boolean equals(Object obj) { + if (obj == this) { + return true; + } else if (obj == null || getClass() != obj.getClass()) { + return false; + } + return true; + } + + +} From 93a8a8e3dd25aa5c2f0ab0da58753580e8939759 Mon Sep 17 00:00:00 2001 From: LeeJooHwan Date: Thu, 18 Jun 2020 15:28:26 +0900 Subject: [PATCH 2/3] =?UTF-8?q?SSL=EB=A1=9C=20=EC=8B=9C=EC=9E=91=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B2=83=EB=93=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bin에 .keystore였나 여기다가 저장 --- SSLSocketServerKey | Bin 0 -> 2221 bytes SSLSocketServerKey.cert | Bin 0 -> 859 bytes WarGameClient.java | 172 ++++++++++++++++++++++----- WarGameGUI.java | 14 ++- WarGameImpl.java | 254 +++++++++++++++++++++++++++++++++------- WarGameServer.java | 150 +++++------------------- 6 files changed, 392 insertions(+), 198 deletions(-) create mode 100644 SSLSocketServerKey create mode 100644 SSLSocketServerKey.cert diff --git a/SSLSocketServerKey b/SSLSocketServerKey new file mode 100644 index 0000000000000000000000000000000000000000..ee6c305a5cd899c139936b93ad0fe2f077dff57e GIT binary patch literal 2221 zcmcJQ`8U*y8^>oemdU;inlTJnNSK#(JWe~C9Bz$bw0 z>g^U7;N~Cf=6@9TuYjjcfg~*auIcZLBr=MhN;g zRis1`b{OVdE)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)}? literal 0 HcmV?d00001 diff --git a/WarGameClient.java b/WarGameClient.java index 02a53f4..c9b431b 100644 --- a/WarGameClient.java +++ b/WarGameClient.java @@ -1,21 +1,31 @@ import java.rmi.AccessException; import java.rmi.Naming; import java.rmi.RemoteException; -import java.rmi.server.UnicastRemoteObject; -import java.util.Scanner; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.net.InetAddress; import java.net.MalformedURLException; +import java.net.UnknownHostException; import java.rmi.NotBoundException; -import java.net.MalformedURLException; +import java.rmi.server.UnicastRemoteObject; +import java.util.ArrayList; +import java.util.Scanner; import java.rmi.NotBoundException; + + + public class WarGameClient implements Runnable {//handle connection and use rmi function here from client side // private static final long serialVersionUID = 1L; + private static final int PORT = 2019; + private String id; boolean chkLog=false; private WarGame server; boolean boolean_ReadyBtnStatus; boolean boolean_HitBtnStatus; boolean boolean_DropBtnStatus; - + ArrayList client; + WarGameGUI gui; protected WarGameClient(WarGame wg,String id,String pw){ boolean_ReadyBtnStatus=false; boolean_HitBtnStatus=false; @@ -28,72 +38,176 @@ protected WarGameClient(WarGame wg,String id,String pw){ System.out.println("wrong id or wrong passworld"); System.exit(0); } - - + + }catch(RemoteException re) { System.out.println("RemoteException: "+re); - + }catch(java.lang.ArithmeticException ae) { System.out.println("java.lang.ArithmeticException: "+ae); } + + } + void drop() { + boolean_DropBtnStatus=gui.returnDropBtnStatus(); + try { + server.doDrop(id);//id가 필요한지 모르겠음... + }catch(Exception e) { + e.printStackTrace(); + } + } + void hit() { + boolean_HitBtnStatus=gui.returnHitBtnStatus(); + try { + server.doHit(id); + }catch(Exception e) { + e.printStackTrace(); + } } - public void run() { if(chkLog) { System.out.println(id); - WarGameGUI gui=new WarGameGUI(id); + gui=new WarGameGUI(id); + gui.deactivatedHitDropBtn(); try { while(server.checkAllReady()==false) { boolean_ReadyBtnStatus=gui.returnReadyBtnStatus(); - server.setReadyStatus(id, boolean_ReadyBtnStatus); + server.setReadyStatus(id, boolean_ReadyBtnStatus); } - - }catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯.. 예외처리 사이즈가 + + }catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯.. 예외처리 사이즈가 e.printStackTrace(); } + gui.activatedHitDropBtn(); System.out.println("game start");//this will be deleted in the end gui.setGameInfo("game start"); gui.deactivateReadyBtn(); - -// try { -// do{//게임중... do while인 이유는 마지막 카드가 공격이어서 기회가 남아 있을수도 있다.. -// //////////////////// -// }while(server.checkStatus()); -// }catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯..예외처리 사이즈가 커 + try {//최종 참가자 리스트 추가 + client=server.getClientList(); + String tmp=client.get(0); + String tmp2=client.get(1); + gui.setPlayerList("Player1: "+tmp); + gui.setPlayerList("Player2: "+tmp2); + }catch(Exception e) { + e.printStackTrace(); + } +// try {//턴 돌아갈때 마다 올라가는 것으로 바꿔야 할것 같다. 1번에 국한되지 말고... +// if(id.contentEquals(server.whosTurn())) { +// String tmp=server.whosTurn(); +// sendMsgToAll(tmp+"'s turn\n"); +// +// } +// +//// System.out.println(tmp+"'s turn. good luck~!"); +//// gui.setGameInfo(tmp+"'s turn. good luck~!"); +//// String msg=receiveMsg(); +//// gui.setMsgView(msg); +// +// +// }catch(Exception e) { // e.printStackTrace(); // } + try { + while(server.checkEndingStatus()){//게임중... + while(id.equals(server.whosTurn())){//자신의 턴 동안... + boolean_DropBtnStatus=gui.returnDropBtnStatus(); + boolean_HitBtnStatus=gui.returnHitBtnStatus(); + if(boolean_DropBtnStatus) { + drop(); + gui.setMsgView("플레이어 "+ id+" 가 drop을 했습니다."); +// server.doDrop(id);//id는 안쓰고, serverImpl에서 turn보고 알아서 판단하고 있음. 필요 없는거 확실해지면 그때 지우기. + gui.setDropStatusFalse();//버튼 눌리면 true로 변하기 때문에 변경 안하면 항상 true인 효과 한번 버튼 누르면 다시 false로 변환 + //server.changeTurn(); + gui.repaint(); + }else if(boolean_HitBtnStatus) { + hit(); +// server.doHit(id); + gui.setHitStatusFalse(); + + }else { + + } + } + if(!id.equals(server.whosTurn())){//player가 잘못누르는 경우... + boolean_DropBtnStatus=gui.returnDropBtnStatus(); + boolean_HitBtnStatus=gui.returnHitBtnStatus(); + if(boolean_DropBtnStatus){ + System.out.println("it's not your turn..please wait"); + gui.setGameInfo("it's not your turn..please wait"); + gui.setDropStatusFalse(); + } + if(boolean_HitBtnStatus){ + System.out.println("it's not your turn..please wait"); + gui.setGameInfo("it's not your turn..please wait"); + gui.setHitStatusFalse(); + } + } + } + +// gui.deactivatedHitDropBtn(); + + }catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯..예외처리 사이즈가 커 + e.printStackTrace(); + } + } + } + public void sendMsgToAll(String str) {//행동 취하고 나면 반드시 해당함수를 써서 메시지를 보내야한다. 그리고 바로 receiveMSG도 작동해야한다. + try { + server.sendMsg(str); + + }catch(Exception e) { + e.printStackTrace(); } } +// public String receiveMsg() { +// String msg=""; +// try { +// msg=server.receiveMsg(); +//// gui.setMsgView(msg+"\n"); +// +// }catch(Exception e) { +// e.printStackTrace(); +// +// } +// return msg; +// } public static void main(String[] args) { String mServer="127.0.0.1"; -// String mServer=args[0]; String mServName="WarGame"; - + System.out.println("Remote Method Invocate to "+mServer+", service name: "+mServName); - + try { + System.setProperty("javax.net.ssl.trustStore", "trustedcerts"); + System.setProperty("javax.net.ssl.trustStorePasssword", "123456"); String id; String pw; Scanner sc=new Scanner(System.in);//this is for temporary - System.out.println("enter id: "); + System.out.print("enter id: "); id=sc.nextLine(); - System.out.println("enter pw: "); + System.out.print("enter pw: "); pw=sc.nextLine(); + + + Registry registry = LocateRegistry.getRegistry( + InetAddress.getLocalHost().getHostName(), PORT, + new RMISSLClientSocketFactory()); - WarGame wg=(WarGame)Naming.lookup("rmi://"+mServer+":1099/"+mServName); - new Thread(new WarGameClient(wg,id,pw)).start(); - - }catch(MalformedURLException mue) { - System.out.println("MalformedURLException: "+mue); + WarGame c=(WarGame)registry.lookup("WarGame"/*"rmi://"+mServer+":1099/"+mServName*/); + new Thread(new WarGameClient(c,id,pw)).start(); + }catch(RemoteException re) { System.out.println("RemoteException: "+re); }catch(NotBoundException nbe) { System.out.println("NotBoundException: "+nbe); }catch(java.lang.ArithmeticException ae) { System.out.println("java.lang.ArithmeticException: "+ae); + } catch (UnknownHostException e) { + System.out.println("UnknownHostException: "+e); } } - + } + diff --git a/WarGameGUI.java b/WarGameGUI.java index a695d46..96a37e8 100644 --- a/WarGameGUI.java +++ b/WarGameGUI.java @@ -234,16 +234,24 @@ public void setPlayerList(String str) {//참가자 리스트를 보여줌 public void deactivateReadyBtn() { readyBtn.setEnabled(false); } + public void deactivatedHitDropBtn() { + hitBtn.setEnabled(false); + dropBtn.setEnabled(false); + } + public void activatedHitDropBtn() { + hitBtn.setEnabled(true); + dropBtn.setEnabled(true); + } + public void actionPerformed(ActionEvent ae) { try { if(ae.getSource()==dropBtn) { // System.out.println("drop"); boolean_DropBtn=true; - - } else if(ae.getSource()==hitBtn) { - System.out.println("hit"); + boolean_HitBtn=true; +// System.out.println("hit"); } // else if(ae.getSource()==tf_Send) { // String msg=tf_Send.getText(); diff --git a/WarGameImpl.java b/WarGameImpl.java index a3908fe..0544dd9 100644 --- a/WarGameImpl.java +++ b/WarGameImpl.java @@ -8,37 +8,48 @@ import java.util.Set; public class WarGameImpl extends UnicastRemoteObject implements WarGame { + + private static final int PORT = 2019; private static final long serializedversion=1L; private String clientID[]= {"aaa","bbb"}; private String clientPassword[]= {"1234","1234"}; - private ArrayList clientList; +// private ArrayList clientList; private Game game; private boolean turn=true; + private String msg; private ArrayList deck0,deck1,deck2; private int clientCnt=0; + //private HashMap clientStatus;//어떤 클라이언트의 순서인지 확인할 수 있다(이름,0)/(이름,1) -> 전자는 비활성화, 후자는 활성화 + private ArrayList client; private HashMap readyStatus; private boolean boolean_readyStatus[]; + private int attackCount=-1; + + private boolean isAttack = false; + private int dropCount = 0; + protected WarGameImpl() throws RemoteException{ super(); //clientList=new ArrayList(); + msg=""; game=new Game(); deck1=game.returnDeck1(); deck2=game.returnDeck2(); - deck0=null; + deck0=game.returnDeck0(); readyStatus=new HashMap(); - + client=new ArrayList(); + //clientStatus=new HashMap(); // boolean_readyStatus=new boolean[2]; // boolean_readyStatus[0]=false; // boolean_readyStatus[1]=false; //System.out.println(deck1);//test } - public synchronized boolean checkClientCredentials(WarGame wg,String id,String password) throws RemoteException{ + public synchronized boolean checkClientCredentials(WarGame wg,String id,String password) throws RemoteException{ //id, pw 확인 boolean checkLog=false; for(int i=0;i getClientList() throws RemoteException{ + return client; + } + @Override + public synchronized void sendMsg(String msg) throws RemoteException{ + this.msg=msg; + } + @Override + public synchronized String receiveMsg() throws RemoteException{ + return msg; + } + @Override + public int checkGameStatus() throws RemoteException {//drop을 해야할지 hit를 해야할지, 공격중인 상황인지 평시 상황인지 판단한담에 거기에 맞춰 int리턴 + // TODO Auto-generated method st + int loc = deck0.size(); + String value=deck0.get(loc-1).returnValue(); + System.out.println("Value: "+value); + if(value=="K")//공격카드의 값만큼 리턴 + return 3; + else if(value.equals("Q")) + return 2; + else if(value.equals("J")) + return 1; + else if(value.equals("A")) + return 3; + else if(value.equals("2")) { + System.out.println("2임"); + return 2; + } + else { + System.out.println("Value????: "+value); + return -1;//평시 상태 + } + } + public boolean checkEndingStatus() throws RemoteException{ //deck이 비면 끝남 if(deck1.isEmpty()==true || deck2.isEmpty()==true) return false;//game over else return true; } - public synchronized void setReadyStatus(String id,boolean status) throws RemoteException{ + public String whosTurn() throws RemoteException{ //누구의 턴인지 반환 + if(turn==true) + return client.get(0); + else + return client.get(1); + } + public void changeTurn() throws RemoteException{ + turn=!turn; + } + public synchronized void setReadyStatus(String id,boolean status) throws RemoteException{//플레이어 각각이 레디했는지 확인 int tmp=0; if(status==true) tmp=1; @@ -71,61 +125,173 @@ public synchronized void setReadyStatus(String id,boolean status) throws RemoteE readyStatus.put(id, tmp); else { readyStatus.put(id,tmp); + } } - public synchronized boolean checkAllReady() throws RemoteException{ + public synchronized boolean checkAllReady() throws RemoteException{//player 2명 전부 레디 했는지 확인 int cnt=0; Iterator> itr=readyStatus.entrySet().iterator(); Set>set=readyStatus.entrySet(); while(itr.hasNext()) { Map.Entrye=(Map.Entry)itr.next(); - if(e.getValue()==1) + if(e.getValue()==1) { cnt++; + } } - if(cnt==2) + if(cnt==2) { + client.addAll(readyStatus.keySet()); +// clientStatus.put(client.get(0),1); +// clientStatus.put(client.get(1),0); return true; + } + else return false; } - public synchronized void doDrop() throws RemoteException{ + public synchronized void doDrop(String id) throws RemoteException{ + Card c=null; if(turn==true) {//player 1's drop - c=deck1.get(0); - if(c==null) { - System.out.println("Player2 win!!"); - } - deck0.add(c); - + if(isAttack == false) { + System.out.println("player 1 drop!"); + if(deck1.size() == 0) { + System.out.println("Player2 win!!"); + return; + } + c=deck1.get(0); + pop(); //가져온 카드 1개 제거 + + deck0.add(c); + attackCount = checkGameStatus(); + if(attackCount == -1) { + turn = !turn; + } + else { + isAttack = true; + turn = !turn; + } + + System.out.println("뽑힌카드 : " +c); + } + else if(isAttack == true) { //공격 상태일 때 + System.out.println("player 1 drop! : Attack"); + if(deck1.size() == 0) { + System.out.println("Player2 win!!"); + return; + } + c=deck1.get(0); + pop(); //가져온 카드 1개 제거 + + deck0.add(c); + if(checkGameStatus() == -1) { //공격카드가 아닐 경우 + dropCount++; + System.out.println("AttackCount: "+attackCount+ ", DropCount: "+dropCount); + + if(dropCount == attackCount) { //낸 카드의 수가 조건을 넘어갔을 떄 + dropCount = 0; + isAttack = false; + for(int i = 0; i < deck0.size(); i++) { + deck2.add(deck0.get(i)); + } + deck0 = new ArrayList(); + System.out.println("aaa의 카드 수 : "+deck1.size()); + System.out.println("bbb의 카드 수 : "+deck2.size()); + System.out.println("깔려 있는 카드 수 :"+deck0.size()); + + } + } + else { + isAttack = true; + dropCount = 0; + attackCount = checkGameStatus(); + turn = !turn; + } + System.out.println("뽑힌카드 : " +c); + } + }else {//player 2's drop - c=deck2.get(0); - if(c==null) { - System.out.println("Player1 win!!"); + if(isAttack == false) { + System.out.println("player 2 drop!"); + if(deck2.size() == 0) { + System.out.println("Player1 win!!"); + return; + } + c=deck2.get(0); + pop(); //가져온 카드 1개 제거 + + deck0.add(c); + attackCount = checkGameStatus(); + if(attackCount == -1) { + turn = !turn; + } + else { + isAttack = true; + turn = !turn; + } + + System.out.println("뽑힌카드 : " +c); + } + else if(isAttack == true) { //공격 상태일 때 + System.out.println("player 2 drop! : Attack"); + if(deck2.size() == 0) { + System.out.println("Player1 win!!"); + return; + } + c=deck2.get(0); + pop(); + + deck0.add(c); + + if(checkGameStatus() == -1) { //공격카드가 아닐 경우 + dropCount++; + System.out.println("AttackCount: "+attackCount+ ", DropCount: "+dropCount); + if(dropCount == attackCount) { //낸 카드의 수가 조건을 넘어갔을 떄 + dropCount = 0; + isAttack = false; + for(int i = 0; i < deck0.size(); i++) { + deck1.add(deck0.get(i)); + } + deck0 = new ArrayList(); + System.out.println("aaa의 카드 수 : "+deck1.size()); + System.out.println("bbb의 카드 수 : "+deck2.size()); + System.out.println("깔려 있는 카드 수 :"+deck0.size()); + } + } + else { + isAttack = true; + dropCount = 0; + attackCount = checkGameStatus(); + turn = !turn; + } + System.out.println("뽑힌카드 : " +c); } - deck0.add(c); } } - public synchronized void doHit() throws RemoteException{ + public synchronized void doHit(String id) throws RemoteException{ + System.out.println("hit이 실행됨"); //플레이어 구분해서 해야하는데 아직 못함. Card []c=new Card[2]; - c[0]=deck0.get(0); - c[1]=deck0.get(1); - if(c[0].returnType()==c[1].returnType()) { - System.out.println("Hit Success!!"); - } - else + if(deck0.size() < 2) { System.out.println("Hit Fail..."); - - - } - public boolean changeTurn() { - return !turn; + } + else { + c[0]=deck0.get(0); + c[1]=deck0.get(1); + if(c[0].returnType()==c[1].returnType()) { + System.out.println("Hit Success!!"); + } + else + System.out.println("Hit Fail..."); + + } } + public void pop() { if(turn==true) { - + deck1.remove(0); }else { - + deck2.remove(0); } } public void push() { diff --git a/WarGameServer.java b/WarGameServer.java index f41b75e..0e3cd45 100644 --- a/WarGameServer.java +++ b/WarGameServer.java @@ -4,137 +4,43 @@ import java.io.*; import java.rmi.Naming; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; -public class WarGameServer { +public class WarGameServer extends UnicastRemoteObject { public int clientCnt=0; -// private WarGameServerRunnable client[]=new WarGameServerRunnable[2]; - public WarGameServer(String server,String servName) { - try { - WarGame wg=new WarGameImpl(); - Naming.rebind("rmi://"+server+":1099/"+servName, wg); - }catch(Exception e) { - System.out.println("Trouble: "+e); - } + private static final int PORT = 2019; + + public WarGameServer(String server,String servName) throws Exception{ + super(PORT, + new RMISSLClientSocketFactory(), + new RMISSLServerSocketFactory()); + } public static void main(String[] args) throws UnknownHostException { -// String mServer=InetAddress.getLocalHost().getHostName(); -// String mServer=args[0]; String mServer="127.0.0.1"; String mServName="WarGame"; System.out.println("started at"+mServer+"and use default port(1099). service name: "+mServName); - new WarGameServer(mServer,mServName); + + try { + + //SSL-based registry 생성 + Registry registry = LocateRegistry.createRegistry(PORT, new RMISSLClientSocketFactory(), new RMISSLServerSocketFactory()); + + //Create new impl object + WarGameImpl warGameImpl = new WarGameImpl(); + + registry.bind("WarGame", warGameImpl); + + System.out.println("WarGame bound in registry"); + + } catch (Exception e) { + System.out.println("WarGame Error : " + e.getMessage()); + e.printStackTrace(); + } } } -// public int whoClient(int clientID) { -// for (int i = 0; i < clientCnt; i++) -// if (client[i].getClientID() == clientID) -// return i; -// return -1; -// } -// public void putClient(int clientID, String inputLine) { -// for (int i = 0; i < clientCnt; i++) -// if (client[i].getClientID() == clientID) { -// System.out.println("writer: "+clientID); -// } else { -// System.out.println("write: "+client[i].getClientID()); -// client[i].out.println(inputLine); -// } -// } - //this is about chatting. so.. -// public void addClient(ServerSocket serverSocket) { -// Socket clientSocket = null; -// -// if (clientCnt < client.length) { -// try { -// clientSocket = serverSocket.accept(); -// clientSocket.setSoTimeout(40000); // 1000/sec -// } catch (IOException i) { -// System.out.println ("Accept() fail: "+i); -// } -// client[clientCnt] = new WarGameServerRunnable(this, clientSocket); -// new Thread(client[clientCnt]).start(); -// clientCnt++; -// System.out.println ("Client connected: " + clientSocket.getPort() -// +", CurrentClient: " + clientCnt); -// } else { -// try { -// Socket dummySocket = serverSocket.accept(); -// WarGameServerRunnable dummyRunnable = new WarGameServerRunnable(this, dummySocket); -// new Thread(dummyRunnable); -// dummyRunnable.out.println(dummySocket.getPort() -// + " < Sorry maximum user connected now"); -// System.out.println("Client refused: maximum connection " -// + client.length + " reached."); -// dummyRunnable.close(); -// } catch (IOException i) { -// System.out.println(i); -// } -// } -// } -// public synchronized void delClient(int clientID) { -// int pos = whoClient(clientID); -// WarGameServerRunnable endClient = null; -// if (pos >= 0) { -// endClient = client[pos]; -// if (pos < clientCnt-1) -// for (int i = pos+1; i < clientCnt; i++) -// client[i-1] = client[i]; -// clientCnt--; -// System.out.println("Client removed: " + clientID -// + " at client[" + pos +"], CurrentClient: " + clientCnt); -// endClient.close(); -// } -// } -//} -//class WarGameServerRunnable implements Runnable{ -// protected WarGameServer warGameServer=null; -// protected Socket clientSocket=null; -// protected PrintWriter out=null; -// protected BufferedReader in=null; -// public int clientID=-1; -// -// Game game=new Game(); -// ArrayList deck0=null;//내려놓는 곳 -// ArrayList deck1;//p1 deck -// ArrayList deck2;//p2 deck -// int playerCardCnt[]=new int[2];//player의 카드 갯 -// int deck0Cnt;//내려놓는 곳 카드 수 -// int turn=0; -// public WarGameServerRunnable(WarGameServer server,Socket socket){ -// this.warGameServer=server; -// this.clientSocket=socket; -// clientID=clientSocket.getPort(); -// try{ -// out=new PrintWriter(clientSocket.getOutputStream(),true); -//// out=new BufferedWriter(clientSocket.getOutputStream()); -// in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); -// }catch(IOException ioe){} -// } -// public void run() { -// gameInit(); -// } -// public void close() { -// try { -// if(in !=null) in.close(); -// if(out !=null) out.close(); -// if(clientSocket!=null)clientSocket.close(); -// -// }catch(IOException i) { -// i.printStackTrace(); -// } -// } -// public int getClientID() { -// return clientID; -// } -// public void gameInit() { -// -// deck1=game.returnDeck1();//이미 셔플된 상태로 나온거라 따로 안해줘도 -// deck2=game.returnDeck2(); -// playerCardCnt[0]=deck1.size(); -// playerCardCnt[1]=deck2.size(); -// deck0Cnt=0; -// -// } From f7216aac3aa9ec39631ba51fbf7d441bbd3293cf Mon Sep 17 00:00:00 2001 From: LeeJooHwan Date: Thu, 18 Jun 2020 15:30:36 +0900 Subject: [PATCH 3/3] Add files via upload --- WarGameServer.java | 111 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/WarGameServer.java b/WarGameServer.java index 0e3cd45..ef777f3 100644 --- a/WarGameServer.java +++ b/WarGameServer.java @@ -43,4 +43,113 @@ public static void main(String[] args) throws UnknownHostException { } } - +//public int whoClient(int clientID) { +// for (int i = 0; i < clientCnt; i++) +// if (client[i].getClientID() == clientID) +// return i; +// return -1; +//} +//public void putClient(int clientID, String inputLine) { +// for (int i = 0; i < clientCnt; i++) +// if (client[i].getClientID() == clientID) { +// System.out.println("writer: "+clientID); +// } else { +// System.out.println("write: "+client[i].getClientID()); +// client[i].out.println(inputLine); +// } +//} +//this is about chatting. so.. +//public void addClient(ServerSocket serverSocket) { +// Socket clientSocket = null; +// +// if (clientCnt < client.length) { +// try { +// clientSocket = serverSocket.accept(); +// clientSocket.setSoTimeout(40000); // 1000/sec +// } catch (IOException i) { +// System.out.println ("Accept() fail: "+i); +// } +// client[clientCnt] = new WarGameServerRunnable(this, clientSocket); +// new Thread(client[clientCnt]).start(); +// clientCnt++; +// System.out.println ("Client connected: " + clientSocket.getPort() +// +", CurrentClient: " + clientCnt); +// } else { +// try { +// Socket dummySocket = serverSocket.accept(); +// WarGameServerRunnable dummyRunnable = new WarGameServerRunnable(this, dummySocket); +// new Thread(dummyRunnable); +// dummyRunnable.out.println(dummySocket.getPort() +// + " < Sorry maximum user connected now"); +// System.out.println("Client refused: maximum connection " +// + client.length + " reached."); +// dummyRunnable.close(); +// } catch (IOException i) { +// System.out.println(i); +// } +// } +//} +//public synchronized void delClient(int clientID) { +// int pos = whoClient(clientID); +// WarGameServerRunnable endClient = null; +// if (pos >= 0) { +// endClient = client[pos]; +// if (pos < clientCnt-1) +// for (int i = pos+1; i < clientCnt; i++) +// client[i-1] = client[i]; +// clientCnt--; +// System.out.println("Client removed: " + clientID +// + " at client[" + pos +"], CurrentClient: " + clientCnt); +// endClient.close(); +// } +//} +//} +//class WarGameServerRunnable implements Runnable{ +// protected WarGameServer warGameServer=null; +// protected Socket clientSocket=null; +// protected PrintWriter out=null; +// protected BufferedReader in=null; +// public int clientID=-1; +// +// Game game=new Game(); +// ArrayList deck0=null;//내려놓는 곳 +// ArrayList deck1;//p1 deck +// ArrayList deck2;//p2 deck +// int playerCardCnt[]=new int[2];//player의 카드 갯 +// int deck0Cnt;//내려놓는 곳 카드 수 +// int turn=0; +// public WarGameServerRunnable(WarGameServer server,Socket socket){ +// this.warGameServer=server; +// this.clientSocket=socket; +// clientID=clientSocket.getPort(); +// try{ +// out=new PrintWriter(clientSocket.getOutputStream(),true); +// // out=new BufferedWriter(clientSocket.getOutputStream()); +// in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); +// }catch(IOException ioe){} +// } +// public void run() { +// gameInit(); +// } +// public void close() { +// try { +// if(in !=null) in.close(); +// if(out !=null) out.close(); +// if(clientSocket!=null)clientSocket.close(); +// +// }catch(IOException i) { +// i.printStackTrace(); +// } +// } +// public int getClientID() { +// return clientID; +// } +// public void gameInit() { +// +// deck1=game.returnDeck1();//이미 셔플된 상태로 나온거라 따로 안해줘도 +// deck2=game.returnDeck2(); +// playerCardCnt[0]=deck1.size(); +// playerCardCnt[1]=deck2.size(); +// deck0Cnt=0; +// +// }