多个客户端 - 一个服务器w /嵌套尝试不连接到第二个客户端[java]

时间:2017-10-23 14:50:38

标签: java sockets server

我正在尝试使用嵌套的try / catch块在单个服务器上运行两个客户端。在运行Server类然后运行客户端时,我得到“客户端1已连接”的输出,但随后它停止并且似乎在等待客户端2时挂起。有人可以解释为什么它们不同时连接吗? / p>

//SERVER args-> 8 8 3000

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ReversiServer {
    public static void main(String[] args) throws IOException {
        if (args.length != 3) {
            System.err.println("Usage: java ReversiServer #_rows #_cols port");
            System.exit(1);
        }
        int portNumber = Integer.parseInt(args[2]);
        System.out.println("ReversiServer listening on port " + portNumber);

        try (
                ServerSocket serverSocket =
                        new ServerSocket(portNumber);
                Socket clientOneSocket = serverSocket.accept();
                BufferedReader clientOneIn = new BufferedReader(
                        new InputStreamReader(clientOneSocket.getInputStream()));
                PrintWriter clientOneOut =
                        new PrintWriter(clientOneSocket.getOutputStream(), true);
        ) {
            System.out.println("Client 1 is connected");
            try (
                    Socket clientTwoSocket = serverSocket.accept();
                    BufferedReader clientTwoIn = new BufferedReader(
                            new InputStreamReader(clientTwoSocket.getInputStream()));
                    PrintWriter clientTwoOut =
                            new PrintWriter(clientTwoSocket.getOutputStream(), true);
            ) {
                System.out.println("Client 2 is connected");
            } catch (IOException e) {
                System.out.println("Exception caught when trying to listen on port "
                        + portNumber + " or listening for a connection");
                System.out.println(e.getMessage());
            }
        } catch (IOException e) {
            System.out.println("Exception caught when trying to listen on port "
            + portNumber + " or listening for a connection");
            System.out.println(e.getMessage());
        }
    }
}

//CLIENT args-> 127.0.0.1 3000

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ReversiClient {
    public static void main(String[] args) {
        if (args.length != 2){
            System.out.println("Usage: java ReversiClient <host name> <port>");
            System.exit(1);
        }

        String hostName = args[0];
        int portNumber = Integer.parseInt(args[1]);

        System.out.println("Echo client connnecting to " + hostName + ":" + portNumber);

        try (
                Socket reversiSocket = new Socket(hostName, portNumber);
                PrintWriter outOne =
                        new PrintWriter(reversiSocket.getOutputStream(), true);
                BufferedReader inOne =
                        new BufferedReader(
                                new InputStreamReader(reversiSocket.getInputStream()));
                BufferedReader stdInOne =
                        new BufferedReader(
                                new InputStreamReader(System.in));
        ) {
            try (
                    PrintWriter outTwo =
                            new PrintWriter(reversiSocket.getOutputStream(), true);
                    BufferedReader inTwo =
                            new BufferedReader(
                                    new InputStreamReader(reversiSocket.getInputStream()));
                    BufferedReader stdInTwo =
                            new BufferedReader(
                                    new InputStreamReader(System.in));
            ) {
            } catch (IOException e) {
                System.err.println("Don't know about host " + hostName);
                System.exit(1);
            }
        } catch (IOException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

Socket reversiSocket = new Socket(hostName, portNumber);初始化连接,您不为第二次连接创建新的Socket

Socket reversiSocketTwo = new Socket(hostName, portNumber);添加到您客户端的第二个try块中,从中获取流,然后再次测试