Java Socket (server & client)

Prerequisites

Development environment

  1. JDK 1.8 Downloads
  2. Eclipse Mars Download

Required jar files

rt.jar (included by default)

In order to run the example; simply follow following steps:

Step 1 Create Java Project

Launch Eclipse IDE; Create new Java Project by going to File -> New -> Others… -> Java Project. Choose any project name you want then click Finish.

Step 2 Create Socket Server Class

Copy the code below to clipboard; Select src folder in your project; Press CTRL + V; Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.

Step 3 Create Socket Client Class

Copy the code below to clipboard; Select src folder in your project; Press CTRL + V; Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.

Step 4 Run the application

Right click to the classes; select Run As -> Java Application.  Remember to start the server  first then the client. The server could be available for multiple connections.

Server Output

Client Output

Tips

You can switch between application consoles in Eclipse IDE by toggling “Display Selected Console” button:

consoleScr