简体   繁体   English

Eclipse中的Java套接字编程

[英]Java Socket Programming in Eclipse

I was trying to create a socket program in JAVA using eclipse. 我试图使用eclipse在JAVA中创建一个套接字程序。 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. 我已准备好服务器和客户端代码,但我不了解如何在eclipse中为同一项目中的服务器和客户端运行代码。 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. 我应该1]在服务器和客户端都有主要方法,并开始单独运行它们,或者2]我应该使用线程分别运行它们,或者3]如果我要在网络中添加另一个客户端那么怎么做我也跑了。

Please do help me and let me know which method should I adopt. 请帮帮我,让我知道我应采用哪种方法。

Thanks for your help. 谢谢你的帮助。

You can do either #1 or #2. 你可以做#1或#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. Eclipse没有同时运行/调试多个Java进程的问题,也没有任何关于让单个Java进程在不同线程上启动服务器和客户端的任何问题。

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. 所以我认为选项#1最有意义。 You server and client apps should be able to run independently of each other, whether you're inside of Eclipse or not. 无论您是否在Eclipse内部,服务器和客户端应用程序都应该能够彼此独立运行。

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). 因此,如果您执行#1,然后将另一个客户端添加到网络中,您只需启动另一个客户端进程,就像使用第一个客户端一样(右键单击类 - >运行方式 - > Java应用程序)。 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. 虽然如果你想启动它们(例如,用于负载测试),那么考虑使用自己的main()方法创建另一个类,该方法只是在不同的线程上旋转一堆客户端。

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. 这有助于调试,如果每个客户端单独运行,将帮助您确定当前正在运行的客户端。

You can probably have a server java class file with main method and this is started by the eclipse. 你可能有一个带有main方法的服务器java类文件,这是由eclipse启动的。 You can also have a client java class which have multi threaded to start a few clients. 您还可以拥有一个具有多线程的客户端java类来启动一些客户端。 To identify the interaction between the server and the client, you can enable debug perspective in eclipse. 要识别服务器和客户端之间的交互,可以在eclipse中启用调试透视图。 This link should be able to give you idea on how you can start coding. 链接应该能够让您了解如何开始编码。

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

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