Thursday, July 15, 2010

Socket Programming using TCP in Java

I am working out some basic network programming examples and assignments from Computer Networks:A Top-Down Approach (James F Kurose and Keith W Ross). This post is regarding the implementation of a simple Client Server application using TCP at the Transport layer. The program is coded in Java. I have made the source code available here.

Source Code for the TCP Client-Server model in Java


The program is extensively commented, and I hope you could find your way through this code. This is aimed at Beginners (Just like me) who are newly introduced to the concepts of socket programming.

Program
The program implements a client-server application. The client accepts a sentence from the user through the keyboard (standard input). It then sends this string to the server through the socket output stream. The server receives this and then capitalizes the sentence. This modified string is then send to the client through the socket output stream of the server. Upon receiving the capitalized string from the server, the client outputs it to the terminal (standard out).

Concepts Covered
This program covers a lot of basic concepts regarding network programming.

1) The Client-Server architecture.

2) The socket as the two end points of connection between client and server.

3) Input and Output stream associated with the standard I/O (keyboard/terminal) and
also associated with the network sockets.

4) The abstraction provided by treating the network architecture in layers. Our codes are at the Application layer. It uses the services provided by the Transport layer - here, TCP.

This program gives an introduction to the basics of network programming at the Application layer using TCP.

The Client

/*
* Coded by: Rahul Krishnan
* File Name: TCPClient.java
*
* Description:
* A simple client-server program using TCP
* at the Transport layer to initiate a reliable
* data transport connection. Client sends a sentence
* to the server. The server sends back capitalized
* sentence. This is output to stdout.
*
*/

import java.io.*;
import java.net.*;

class TCPClient {
public static void main(String argv[]) throws Exception{
String sentence; // Lowercase sentence from user.
String modifiedSentence; // Stores uppercase reply from server.

// Input stream - from user keyboard.
BufferedReader fromUser = new BufferedReader(
new InputStreamReader(System.in));

// Creates the Client socket and binds to the server
// at localhost, port 4444
Socket clientSocket = new Socket("localhost", 4444);

// Output stream to send the sentence through this.
DataOutputStream toServer = new DataOutputStream(clientSocket.getOutputStream());

// Input stream to read the capitalized sentence from server.
BufferedReader fromServer = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));

System.out.print("Client> ");

sentence = fromUser.readLine();
toServer.writeBytes(sentence + '\n');

modifiedSentence = fromServer.readLine();
System.out.println("Server>" + modifiedSentence);
clientSocket.close(); // TCP Client sends the close message to server.
}
}


The Server

/*
* Coded by: Rahul Krishnan
* File Name: TCPServer.java
*
* Description:
* A simple client-server program using TCP
* at the Transport layer to initiate a reliable
* data transport connection. The server takes in
* a sentence from client and sends back the
* capitalized version of it.
*
*/
import java.io.*;
import java.net.*;

class TCPServer {
public static void main(String argv[]) throws Exception {
String clientSentence; // Stores sentence sent by client.
String upperCaseSentence; // Uppercase sentence.

// Creates a server socket, which waits for clients
// to initiate connection.
ServerSocket welcomeConnection = new ServerSocket(4444);

while (true) {
System.out.println("Waiting for clients to connect....");

// Creates a new socket when a client contacts
// the server for the first time.
Socket connection = welcomeConnection.accept();

System.out.println("Client Connected.");

BufferedReader fromClient = new BufferedReader(
new InputStreamReader(connection.getInputStream()));

DataOutputStream toClient = new DataOutputStream(
connection.getOutputStream());


clientSentence = fromClient.readLine();
System.out.println("Client sent: " + clientSentence);
upperCaseSentence = clientSentence.toUpperCase() + '\n';

toClient.writeBytes(upperCaseSentence);
}
}
}

Post inspired by the simplicity of explanation provided in the text book that I had referred - Authors: Kurose and Ross

8 comments:

  1. its really nice and understandable

    ReplyDelete
  2. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  3. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  4. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  5. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  6. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  7. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  8. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete