0

Hi guys i'm trying to connect two clients via the server, atm each client cconnects to the server and creates a new thread so each client can communicate with the server, but how would I get two clients communicating via the server ?

Server:

public void allowConnections(){
    int i = 0;
    try{
        while(true){                                                      
            new Thread(new TestT(draughtsSS.accept(),i )).start();         //listen for incoming connections    
            i++;
        }
    }
    catch(IOException e){
        System.out.println("Error :"+e.getMessage());
    }   
}


class TestT implements Runnable{

    Socket s;
    int gameNo;
    Scanner in;
    ObjectInputStream objectIn;  // for retrieving new color array (down s socket)
    ObjectOutputStream out;
    PrintWriter outPW;
    //String Player1;
    //String Player2;
    String playerIP;
    String opponentIP;
    int port = 50000;

    TestT(Socket s, int gameNo){
        this.s = s;
        this.gameNo = gameNo;
    }
    public void run(){

        try{
            outPW = new PrintWriter(s.getOutputStream(), true);
            out = new ObjectOutputStream(s.getOutputStream());
            out.writeObject(list);
            in = new Scanner(s.getInputStream());
            outPW.println("returnIP");                            ///get the IP of the client
            clientIPArray.add(in.nextLine());                     /// add it to the clientIPArray
        }
        catch(IOException e){
            e.getStackTrace();  
        }
        JOptionPane.showMessageDialog(new JFrame(), s.getInetAddress().toString() +" has connected.");              
        list[gameNo] = "Game "+Integer.toString(gameNo);
        gamesList.updateUI();   

        }
    }

}

    private void setupConnection(String serverIP, String port){
    int portInt = Integer.parseInt(port);
    serverIp = serverIP;
    try{
        IP = InetAddress.getByName(serverIP);
        clientSocket = new Socket(IP,portInt);
        JOptionPane.showMessageDialog(new JFrame(), "Successfully connected to Server");
        in = new ObjectInputStream(clientSocket.getInputStream());
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        scanIn = new Scanner(clientSocket.getInputStream());
        a = (String[]) in.readObject();
        if(scanIn.nextLine().equals("returnIP"))out.println(clientIp);
        getGamesList(a);
        Thread tss = new Thread(new ClientThread());
        tss.start();
        System.out.println("w");
    }
    catch(Exception e){
        System.out.println("ErrorC :" +e.getMessage());
    }
}
John Collins
  • 121
  • 2
  • 14

1 Answers1

0

Suggestions:

  • Keep couple of buffers on the server side, refresh them on clients' input and send data when another client is connected

  • Let server to wait until two clients will be connected and then redirect InputStream of each Socket to OutputStream of another.

  • Let server to wait until two clients will be connected and then transmit to both clients address of another, let them to communicate p2p (without server)

  • Any combination of the above

The best way depends on your goals.

Sergey Fedorov
  • 2,169
  • 1
  • 15
  • 26
  • Talking about suggestions, I'd use [Thrift](http://thrift.apache.org/) or other mature framework to do the dirty work for you. – Gilberto Torrezan Nov 28 '13 at 19:14
  • Thanks for sharing an useful link! I don't think it is easier than writing on java, but I will keep in mind) – Sergey Fedorov Nov 29 '13 at 15:14
  • thanks for the suggestions, now i'm trying to recieve an array is this legitimate ? else if(Color[])objectIn.readObject() instanceof Color[]){ //do something} – John Collins Nov 29 '13 at 15:51
  • `(Color[])` will throw ClassCastException if it is not Color[]. Better split to Object o=objectIn.readObject();if(o instanceof ...) Does it really relate to question? – Sergey Fedorov Nov 29 '13 at 15:57
  • but then it will be an object not a color[], it seems the above if statement oes not work.. i'd better ask another question – John Collins Nov 29 '13 at 16:10