Making a Client Server application (Socket programming) in java

The programming languages become handier when the user can create a link between two pieces of compiled codes operating from two different locations.

In this example, a simple server that takes a text string from the client and returned in upper case is discussed. Hope this will give an overall picture about how socket programming work in JAVA.

You do not have to be an expert in Java and all you need is a Java compiler.

Let us get started;

  1. Create an empty folder in your C: drive with the name “Tutebox”
  2. Create 2 empty JAVA files inside the folder and rename them as:
    3. Then copy/paste the following codes into those files and save them.
    4. Compile the java files.
      • Open command prompt
      • Type “C:” {enter}
      • Type “cd C:Tutebox” {enter}
      • Type “javac *.java” {enter}
      • If you have returned to the command prompt, you are ready to run your client server application. (If you do have difficulties please write to us, we would be happy to help you)

or you can simply download and from here.

Server side code



class TCPServer {

public static void main(String argv[]) throws Exception {

String clientSentence;

String capitalizedSentence;

ServerSocket welcomeSocket = new ServerSocket(6789);

while(true) {

Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream  outToClient = new DataOutputStream(connectionSocket.getOutputStream());

System.out.println(“Server waiting for data…”);

clientSentence = inFromClient.readLine();

System.out.println(“Recived from client : ” + clientSentence);

capitalizedSentence = clientSentence.toUpperCase() + ‘n’;





Client Side code



class TCPClient {

public static void main(String argv[]) throws Exception {

String sentence;

String modifiedSentence;

BufferedReader inFromUser =  new BufferedReader(new InputStreamReader(;

Socket clientSocket = new Socket(“localhost”, 6789);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

System.out.print(“SEND TO SERVER: “);

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + ‘n’);

modifiedSentence = inFromServer.readLine();

System.out.println(“FROM SERVER: ” + modifiedSentence);




You have just created a client server application. Now let us see how it works in real time.

  1. Open two command prompts;
  2. First command prompt
    • Type “C:”
    • Type “cd C:Tutebox”
    • Type “java TCPServer”
  3. Second command prompt
    • Type “C:”
    • Type “cd C:Tutebox”
    • Type “java TCPClient”

You are ready to go.
Write some text in any case on the client program and press enter.
Your server, which is running on the other command prompt, will convert it to uppercase and send it back to the clients’ command prompt.

TCP Client Server demo

“Knowledge belongs to world and Be proud to pass it on” – Tutebox

Share this post

Related Posts

11 thoughts on “Making a Client Server application (Socket programming) in java”

  1. Buster Doceti says:

    Nice to have you back. And again with the interesting posting.

  2. Pingback: PowerSaver
  3. Cute Girl says:

    hey, good job as always genius. 🙂 # * **** * #

    1. hanxlk says:

      Thanks alot CuteGirl 😉

  4. steven_scream says:

    it was very interesting to read
    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

  5. drug123 says:

    Nice post… keep writing…

  6. yaz and yasmin says:

    I really love the topic that you shared. Thank you for posting.

  7. EverBloomer says:

    I have doubt ..
    scenario -> we have 2 clients and server.

    Client A has connected with Server… but message is not sent yet!
    Client B is connecting and sending message..

    if i close the A what will happen to Client B’s Message ?

    How to manage these kind a problems???

    1. hanxlk says:

      Easiest solution for these kind of scenario is to choose different ports to communicate..

  8. charu says:

    Nice……keep it ON!!1

  9. charu says:

    I want to play my Chess application in LAN,which developed in java…………
    will u plz help??

Comments are closed.