Java Socket Programming (Client Server Program)

In this tutorial I have shared simple client server program example to explain java socket programming.

In this example I will use Socket and ServerSocket classes for connection oriented socket programming. Since data is shared between server and client over network in the form of streams so DataInputStream and DataOutputStream classes are used.

Java Socket Programming (Client Server Program)

Server

Above code is used to create server which is running on localhost on port number 5000.

Client

Since the server is running on local machine so I have used locahost while creating connection. Otherwise use the IP address of server.

How to Execute?

1. First open a command prompt and run server program. The server will wait for client to be connected.

2. Now open another command prompt and run client program. This will connect client with server. Enter a message at client side to send it to server.

See below screenshot as an example.

Java Socket Programming (Client Server Program)

Comment below if you have any queries regarding above client server program in java.

Leave a Reply

Your email address will not be published. Required fields are marked *