1

我正在尝试为我编写的服务器/客户端编写 JUnit 测试,因此我创建了充当服务器的 Runnable,而我当前的线程充当客户端。在尝试写入客户端套接字的输出流之前,我在服务器 Runnable 上调用 start()。但是,我的程序总是在之后终止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");
            }
        }
    }
4

1 回答 1

7

new ServerSocket(0)将创建一个侦听任何空闲端口的服务器,该端口在每次运行测试时都会有所不同。 new Socket("hostname", 0)虽然正在尝试专门连接到端口 0,但这将失败。

由于您首先初始化服务器,因此您可以调用getLocalPort()ServerSocket获取服务器正在侦听的端口,然后在创建客户端时使用此端口号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();
    }
}
于 2013-04-16T03:10:58.027 回答