我试图为我编写的服务器/客户端编写JUnit测试,所以我创建了充当服务器的Runnable,而我当前的线程充当了客户端。 在尝试写入客户端套接字的输出流之前,我在服务器Runnable上调用了start()。 但是,我的程序总是在Socket client = new Socket("hostname", 0);之后终止Socket client = new Socket("hostname", 0); 我不知道为什么。 我的猜测是因为我试图在同一测试中同时创建套接字和客户端? 因此,将一个ip绑定为客户端并同时监听该ip会导致异常行为吗? 这个对吗? 我该如何解决这个难题?

public void test() {
        int result;
        String strMsg = "dasda";
        try {
            Thread serverThread = new Thread(new ServerRunnable());
            serverThread.start();
            Socket client = new Socket("hostname", 0);
            OutputStream os = client.getOutputStream();
            os.write(strMsg.getBytes());
            InputStream is = client.getInputStream();
            while (true){
                result = is.read();
                ByteBuffer bb = ByteBuffer.allocate(4);
                bb.putInt(result);
                String input = new String(bb.array());
                if (input=="Success") return;
            }
        } catch (IOException e1){
            fail("IOException on client");
        }

    }

    class ServerRunnable implements Runnable {
        ServerSocket server;
        public ServerRunnable(){
            server = new ServerSocket(0);
        }
        public void run(){
            try {
                active = true;
                    while (active) {
                     Socket sock = server.accept();
                    }
            } catch (IOException e1){
                fail("IOException in Server");
            }
        }
    }

===============>>#1 票数:6 已采纳

new ServerSocket(0)将创建一个服务器,该服务器侦听每次运行测试时都会变化的任何可用端口。 尽管new Socket("hostname", 0)试图专门连接到端口0,但这将失败。

由于首先要初始化服务器,因此可以在ServerSocket上调用getLocalPort()以获取服务器正在侦听的端口,然后在创建客户端Socket时使用此端口号。

您可能还需要将主机从"hostname"更改为"localhost"以便它可以连接到本地计算机上的端口。

以下是根据您的代码改编而成的示例。 要看的主要项目是Socket client = new Socket("localhost", sr.getPort());

public void test() throws Exception {
    // start up the server
    ServerRunnable sr = new ServerRunnable();
    Thread serverThread = new Thread(sr);
    serverThread.start();

    // connect the client to the port the server is listening on
    Socket client = new Socket("localhost", sr.getPort());

    // client socket interactions go here

    client.close();
}

class ServerRunnable implements Runnable {
    private ServerSocket server;

    public ServerRunnable() throws IOException {
        // listen on any free port
        server = new ServerSocket(0);
    }

    public void run() {
        try {
            while (true) {
                Socket sock = server.accept();

                // normally you will need to start a thread to handle
                // the new socket so that the server will be able to accept
                // new connections but this may not be necessary for
                // unit testing where only a single connection occurs.

                sock.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int getPort() {
        return server.getLocalPort();
    }
}

  ask by user1431282 translate from so

未解决问题?本站智能推荐: