Hi guys!!!
I'm having a major problem with this essay and the deadline is today!!!!
i've created a super peer to manage the connections and a client handler! and a simple client!! once the client has rewquested 2 play it should give him his opponent and then the first client create a new connection peer to peer!!!
the first client is ok!! but the socond for no obvious reason(at least to me) just desnt handle the streams right in the client handler!!!
plzzz help!!!
package anaptyksi; import java.net.*; import java.io.*; import java.util.*; public class MultiThreadServer { public static void main(String[] args) throws IOException{ /* Properties pro = System.getProperties(); pro.setProperty("java.net.preferIPv4Stack","true"); System.setProperties(pro); */ ServerSocket serverSocket = null; try{ serverSocket = new ServerSocket(4444); System.out.println("Organizer "+Inet4Address.getLocalHost().getHostAddress()+" initialized and waits for connections"); /* Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) interfaces.nextElement(); System.out.println(ni); }*/ } catch(IOException e){ System.err.println("Could not listen on port: 4444."); System.exit(1); } Socket clientSocket = null; int counter = 0 , game = 1; String[] player_ip = new String[2]; int[] player_id = {0,0}; Vector<ClientHandler> ThreadV = new Vector(); while(true){ try{ clientSocket = serverSocket.accept(); counter++; System.out.println("Received connection from peer "+clientSocket.getInetAddress().getHostAddress()+" Attributed id "+counter); ThreadV.add(new ClientHandler(clientSocket,counter)); ThreadV.elementAt(counter-1).start(); if(player_id[0] == 0){ player_id[0] = counter; player_ip[0] = clientSocket.getInetAddress().getHostAddress(); } else{ player_id[1] = counter; player_ip[1] = clientSocket.getInetAddress().getHostAddress(); ThreadV.elementAt(player_id[0] - 1).opponent(player_id[1],player_ip[1],1); ThreadV.elementAt(player_id[1] - 1).opponent(player_id[0],player_ip[0],2); System.out.println("Game "+game+" will be initialized from peer "+player_id[0]+" with symbol x"); game++; } } catch(IOException e){ System.err.println("Accept failed."); serverSocket.close(); System.exit(1); } } } }
package anaptyksi; import java.net.*; import java.io.*; class ClientHandler extends Thread{ Socket clientSocket = null; int cid; int flag = 0; public ClientHandler(Socket clientSocket,int counter){ this.clientSocket = clientSocket; cid = counter; } @Override public void run(){ try{ InputStream is = clientSocket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader in = new BufferedReader(isr); OutputStream os = clientSocket.getOutputStream(); PrintWriter out = new PrintWriter(os, true); String inputLine = in.readLine(); if(inputLine.equals("play")){ System.out.println("Peer " +cid+ " requests to play"); out.println(cid); } else if(inputLine.equals("statistics")){ System.out.println("THA TO FTIAXO KAI AFTO..."); } else{ System.out.println("LATHOS ENTOLI!!!"); } if(cid%2!=0){ this.sleep(10000); } out.close(); in.close(); clientSocket.close(); } catch(IOException e){ System.err.println("Ouups"); System.exit(1); } catch (InterruptedException e) { System.err.println("Den perimenei o peer!"); System.exit(1); } } public void opponent(int oppid,String oppip,int turn){ try{ OutputStream osi = clientSocket.getOutputStream(); PrintWriter outi = new PrintWriter(osi, true); if(turn == 1){ outi.println(oppip+" x "+turn); } else{ outi.println(oppip+" o "+turn); } outi.close(); } catch(IOException e){ System.err.println("Den paei to minimataki!!!"); System.exit(1); } } }
package anaptyksi; import java.net.*; import java.io.*; public class SimpleClient { public static void main(String[] args) throws IOException{ Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try{ echoSocket = new Socket("192.168.178.23", 4444); System.out.println("Connect to: "+Inet4Address.getLocalHost().getHostAddress()); System.out.println("Connected to organizer, waiting for id:..."); OutputStream os = echoSocket.getOutputStream(); out = new PrintWriter(os, true); InputStream is = echoSocket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); in = new BufferedReader(isr); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String cmd = br.readLine(); out.println(cmd); System.out.println("Attributed id "+ in.readLine()); System.out.println("Waiting opponent..."); /* String oppflag = null; while(oppflag == null){ oppflag = in.readLine(); System.out.println("Received "+oppflag); } */ String info; info = in.readLine(); System.out.println("Received "+info); } catch(UnknownHostException e){ System.err.println("Unknown host"); System.exit(1); } catch(IOException e){ System.err.println("Couldn't get the I/O"); System.exit(1); } out.close(); in.close(); echoSocket.close(); } }