问题
When creating a client server application (serversocket), would I create 2 seperate projects or can I do this in a single project?
I'm a little confused as to what type of project this will be, and more confused as to how I can launch both the client and server.
Or would I have to manually fire up terminal and execute the jars?
This will be a serversock and a client that connects to it and sends simple messages.
回答1:
Assuming that your client and server are started by invoking a Java main()
method in a client and server class respectively, the simplest path is to have a single IntelliJ project. To launch the server, right-click on the class containing the main()
method and select "run". Likewise for the client. When you do this, each program is added to IntelliJ's run configurations (a dropdown list to the left of the green arrow on IntelliJ's button bar). You can choose "Edit Configurations" from this dropdown to change how your main methods are run, for example, to pass in command line arguments.
回答2:
There isn't really a good or bad answer to this question, it all depend on your requirements. In case you have both the client and server in the same jar, you can provide a parameter through which you will figure out what to start (client or server)
example: java myapp client
and in the main method
public static void main (String[] args) {
// Make sure args is not empty
// In which case you can display a help message on how to use this application
if (args[0]== "client"){
// This is a client
} else {
// This is a server
}
}
来源:https://stackoverflow.com/questions/8247568/if-i-am-creating-a-simple-client-server-application-in-intellij-how-should-this