sshah
sshah

Reputation: 1

Java Socket Programming in Eclipse

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.

Upvotes: 0

Views: 5178

Answers (3)

Jasonw
Jasonw

Reputation: 5064

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.

Upvotes: 0

aroth
aroth

Reputation: 54816

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.

Upvotes: 2

JZares
JZares

Reputation: 307

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.

Upvotes: 0

Related Questions