简体   繁体   English

如何在Eclipse中运行和调试客户端-服务器项目?

[英]How to run and debug client-server project in Eclipse?

There are three classes: Server, Client, Test. 共有三类:服务器,客户端,测试。 I need to run server which waits for client and process it's commands. 我需要运行等待客户端并处理其命令的服务器。 While trying to run two classes within Test it ends up in server's while (wait cycle for client connection on a socket): 尝试在Test中运行两个类时,它最终在服务器的while(等待套接字上的客户端连接的周期)中:

class Test{
    new Server();
    new Client();
}

How to run and debug this kind of application? 如何运行和调试这种应用程序?

You can run two processes in Eclipse simultaneously. 您可以在Eclipse中同时运行两个进程。 Run the Server in "debug" and run Test in "debug", the processes will each stop in the designated breakpoints. 在“调试”中运行服务器,在“调试”中运行测试,每个进程将在指定的断点处停止。 As far as I understand Test invokes Client commands, right? 据我了解,Test调用客户端命令,对吗?
You don't need to run both in the same process. 您无需在同一过程中同时运行两者。

You can just create two Threads like this: 您可以像这样创建两个Threads

Thread serverThread = new Thread(new Runnable() {
  @Override
  public void run() {
    new Server();
  }
});
Thread clientThread = new Thread(new Runnable() {
  @Override
  public void run() {
    new Client();
  }
});
serverThread.start();
clientThread.start();

You need to start another thread for your server loop, this is usually done in the setup phase of a test. 您需要为服务器循环启动另一个线程,这通常是在测试的设置阶段完成的。

Assuming you're using JUnit 4. 假设您正在使用JUnit 4。

class Test {
    @BeforeClass
    public static void setup() {
        new Thread() {
            public void run() {
                new Server();
            }
        }.start();
    }
    @AfterClass
    public static void teardown() {
        // stop the server (somehow)
    }

    @Test
    public void test() {
        new Client();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM