Eclipse 中的 Java 套接字编程
我试图使用 eclipse 在 JAVA 中创建一个套接字程序。我已经准备好服务器和客户端代码,但我不明白如何在 Eclipse 中为同一项目中的服务器和客户端运行代码。我应该吗 1] 在服务器和客户端都有 main 方法并开始单独运行它们,或者 2]我应该使用线程来单独运行它们,还是 3]如果我要在网络中添加另一个客户端,那么我该如何运行它。
请帮助我,让我知道我应该采用哪种方法。
感谢您的帮助。
I was trying to create a socket program in JAVA using eclipse. I have the server and client code ready but I am not understanding how to run the code in eclipse for the server and client in the same project. Should I
1] have main method in both the server and the client and start running them individually, or
2] Should I use threads to run each of them separately, or
3] If I were to add another client in the network then how do I run that as well.
Please do help me and let me know which method should I adopt.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行#1 或#2。 Eclipse 不存在并发运行/调试多个 Java 进程的问题,并且使用单个 Java 进程在单独的线程上启动服务器和客户端也不存在任何无效问题。
但是,我建议在现实世界中,您很可能不会总是同时在同一台计算机上启动服务器和客户端进程。所以我认为选项#1 最有意义。您的服务器和客户端应用程序应该能够彼此独立运行,无论您是否在 Eclipse 中。
因此,如果您执行#1,那么要将另一个客户端添加到网络,您只需启动另一个客户端进程,就像您对第一个客户端所做的那样(右键单击类 -> 运行方式 -> Java 应用程序)。您可以按照您喜欢的方式开始任意多个。不过,如果您想启动一堆客户端(例如,用于负载测试),请考虑使用自己的
main()
方法创建另一个类,该方法仅在单独的位置上启动一堆客户端线程。You can do either #1 or #2. Eclipse has no issue with running/debugging multiple Java processes concurrently, nor is there anything invalid about having a single Java process that starts up both the server and client on separate threads.
However, I would suggest that in the real world it is most likely that you will not always be starting the server and client processes on the same machine, at the same time. So I think option #1 makes the most sense. You server and client apps should be able to run independently of each other, whether you're inside of Eclipse or not.
So if you do #1, then to add another client to the network you simply spin up another client process, the same way you did with the first client (Right click on the class -> Run As -> Java Application). You can start as many as you like that way. Though if you want to start up a bunch of them (like, say, for load testing), then consider creating another class with its own
main()
method that just spins up a bunch of clients on separate threads.我认为如果将单独的主要方法放在客户端和服务器中会更好。这有助于调试,并帮助您确定当前正在运行的客户端(如果每个客户端单独运行)。
I think it would be better if you put individual main methods in the client and the server. This helps with the debugging and would help you determine which client is currently running if each client is run separately.
您可能可以拥有一个带有 main 方法的服务器 java 类文件,这是由 eclipse 启动的。您还可以有一个客户端 java 类,它具有多线程来启动一些客户端。要识别服务器和客户端之间的交互,可以在 eclipse 中启用调试透视图。这个链接应该是能够为您提供有关如何开始编码的想法。
You can probably have a server java class file with main method and this is started by the eclipse. You can also have a client java class which have multi threaded to start a few clients. To identify the interaction between the server and the client, you can enable debug perspective in eclipse. This link should be able to give you idea on how you can start coding.