Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: tick tack toe hard times!!

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default tick tack toe hard times!!

    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();
        }
    }
    Last edited by JavaPF; November 12th, 2010 at 08:56 AM. Reason: Please use [highlight=Java] code [/highlight] tags


Similar Threads

  1. [SOLVED] Very complex project. It's hard to explain.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 55
    Last Post: November 4th, 2010, 12:30 PM
  2. Counting How Many Times Program Was Excecuted
    By Override in forum Loops & Control Statements
    Replies: 2
    Last Post: October 30th, 2010, 06:11 PM
  3. Replies: 1
    Last Post: October 16th, 2010, 04:32 PM
  4. way too hard to explain this in short. please help guys!!
    By humdinger in forum Collections and Generics
    Replies: 4
    Last Post: March 15th, 2010, 06:57 AM
  5. GuessWhat- same errors repeated 4 times?
    By iank in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2009, 08:32 PM