0

I have a Server and two clients.

I have finished i/o with the client A and client B .

Now i need to reconnect to Client A and do some input/output with that client.

I store connectedClientsHistory in a list of String with their ip and ports.

But the problem is that i got Exception java.net.ConnectException: Connection refused: connect

Here is my code:

public class Server extends Thread {

private static int serverPort = 9090;
private static ServerSocket listener;
private static int numberOfConnectedClients;
private static int numberOfPlayers;
List<String> orderOfConnectedClients = new LinkedList<String>();

public Server(int port) throws IOException {
    listener = new ServerSocket(port);
    this.serverPort = port;
}


public static void main(String[] args) throws IOException {
    Thread thread = new Server(9090);
    thread.start();
}

public void run() {

    while (numberOfConnectedClients < numberOfPlayers) {

        try {
            System.out.println("Waiting for clients on server Port " + listener.getLocalPort() + " ...\n");

            Socket connectedClient = listener.accept();
            numberOfConnectedClients++;

            System.out.println("Connected Client ip/port:   " + connectedClient.getRemoteSocketAddress().toString());
            orderOfConnectedClients.add(connectedClient.getRemoteSocketAddress().toString());
            System.out.println("History Connected Clients: " + orderOfConnectedClients);

            DataOutputStream outToClient = new DataOutputStream(connectedClient.getOutputStream());
            InputStream inFromClient = connectedClient.getInputStream();
            DataInputStream in = new DataInputStream(inFromClient);


            if (numberOfConnectedClients == numberOfPlayers) {
                // notify to the first client

                String[] firstClient = orderOfConnectedClients.get(0).split(":");
                System.out.println(firstClient);

                Socket reconnectSocket = new Socket(firstIp, firstPort);
                DataOutputStream outToClient2 = new DataOutputStream(reconnectSocket.getOutputStream());
                outToClient2.writeUTF("Reconnect?");

         // how access to the first client?

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
CVV
  • 451
  • 2
  • 7
  • 21
  • You have to edit your question a bit: *But the problem is that i got Exception* is a bit cryptic: What exception? – Norbert Nov 23 '15 at 20:02
  • @NorbertvanNobelen Sorry, see that again. – CVV Nov 23 '15 at 20:07
  • 'Now i need to reconnect to Client A and do some input/output with that client.' The problem is here. You are attempting the impossible. You can't do that in TCP. Clients connect to servers. Not the other way round. You state that you're 'finished I/O with client A' and then that you 'need to reconnect to client A'. You're contradicting yourself.Your question doesn't make sense. – user207421 Nov 23 '15 at 23:02
  • @EJP I should correct that , two clients are connected now, now how send some data to the first client? – CVV Nov 24 '15 at 05:25
  • Err, via the Socket that's connected to it? What's the question now? – user207421 Nov 24 '15 at 17:38
  • @EJP Two clients are now connected to the server, how But the last client is `client B` , Now How can i write something to the `client A` ? – CVV Nov 28 '15 at 07:51

0 Answers0