Creating a TCP Client and Server Application
Creating TCP sockets is easy in Java. The client program creates a java.net.Socket. The server program does that as well, but more important, the server creates a java.net.ServerSocket.
Tip
You can use a ServerSocket in your program to your advantage, even if you are not doing networking. Because a ServerSocket is connected to a given port number, and only one program can use that port number, you can prevent a user from running multiple copies of your program by instantiating a ServerSocket on a specific port. If the user then tries to run another copy of your program, the program will throw an exception. This occurs because it is illegal to have two ServerSockets set to the same port on a given host. ...
Get PURE Java™ 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.