Reputation: 9611
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.
Upvotes: 4
Views: 7656
Reputation: 1
Yes you can do it in a single project. The easiest way is to create a separate thread for your serverSocket, and start it in the main method of your app wich will initiate and start the server socket first.
public static void main (String[] args) {
// CREATE AN OBJECT OF SERVER CLASS WHICH EXTENDS THREAD AND START IT.
Server serv = New Server ();
Serv.start ();
Socket soc = Socket("localhost", 2000);
}
Upvotes: 0
Reputation: 19131
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.
Upvotes: 5
Reputation: 21429
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
}
}
Upvotes: 0