0% found this document useful (0 votes)
4 views12 pages

05 Exp

This document outlines the implementation of a client-server model using TCP in Java, detailing protocols, algorithms, and example code for various applications including a simple data transfer, chat server, time server, and echo server. It includes pre-lab information on reliable connection protocols, syntax explanations for key classes, and step-by-step algorithms for both server and client programs. The document also provides sample outputs for each program to demonstrate functionality.

Uploaded by

srinivaas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

05 Exp

This document outlines the implementation of a client-server model using TCP in Java, detailing protocols, algorithms, and example code for various applications including a simple data transfer, chat server, time server, and echo server. It includes pre-lab information on reliable connection protocols, syntax explanations for key classes, and step-by-step algorithms for both server and client programs. The document also provides sample outputs for each program to demonstrate functionality.

Uploaded by

srinivaas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Ex.

No:5
Client-Server Model Using TCP
Date:

Aim:
To implement a server-client model using Transmission Control Protocol.
(TCP)

Pre-Lab:

1. List out the application protocols which require reliable connection – TCP and also
give their default port numbers

Default
Protocol Description
Port

Hypertext Transfer Protocol (web


HTTP 80
browsing)

HTTP Secure (encrypted web


HTTPS 443
browsing)

File Transfer Protocol (control


FTP 21
connection)

SSH 22 Secure Shell (secure remote login)

Telnet 23 Telnet Protocol (remote login)

Simple Mail Transfer Protocol (email


SMTP 25
sending)

Post Office Protocol v3 (email


POP3 110
retrieval)

Internet Message Access Protocol


IMAP 143
(email management)

Domain Name System (TCP used for


DNS 53
zone transfers)

MySQL 3306 MySQL Database System

PostgreSQL 5432 PostgreSQL Database System


2. Write the syntax and its explanation for the following:

a. OutputStream
OutputStream outputStream = socket.getOutputStream();
• OutputStream is an abstract class that represents an output stream of bytes.
• In socket programming, we get the output stream from a socket
using getOutputStream().
• This stream is used to send data to the connected client/server.
• We typically wrap it with other classes like PrintWriter or DataOutputStream for
easier writing of data.

b. InputStream
InputStream inputStream = socket.getInputStream();
• InputStream is an abstract class that represents an input stream of bytes.
• In socket programming, we get the input stream from a socket
using getInputStream().
• This stream is used to receive data from the connected client/server.
• We typically wrap it with other classes like InputStreamReader, BufferedReader,
or DataInputStream for easier reading of data.

c. InputStreamReader
InputStreamReader isr = new InputStreamReader(inputStream);
• InputStreamReader is a bridge from byte streams to character streams.
• It reads bytes from an InputStream and decodes them into characters using a
specified charset.
• If no charset is specified, it uses the platform's default charset.
• It's often used with BufferedReader for efficient reading of text data.

d. BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
• BufferedReader reads text from a character-input stream, buffering characters for
efficient reading.
• It provides methods like readLine() to read an entire line of text at once.
• Wrapping an InputStreamReader with BufferedReader is a common practice in
socket programming for reading text data line by line.
• It improves performance by reducing the number of I/O operations through
buffering.
These classes and methods are fundamental for implementing TCP-based client-server
communication in Java, where OutputStream is used to send data and InputStream with its
wrappers is used to receive data.
In-Lab Programs:

1. Establish reliable connection (TCP) between client and server, send message from server
to client and print message in client.

Algorithm:

Server:

i. Start the program.


ii. Create a ServerSocket on a specified port (e.g., 2050).
iii. Wait for a client to connect using accept().
iv. Get the output stream of the connected socket.
v. Prompt the user to enter data from the keyboard.
vi. Read each character using System.in.read().
vii. Send each character to the client using OutputStream.write().
viii. Continue until the terminating character '#' is encountered.
ix. Close the output stream, socket, and server socket.

Client:

i. Start the program.


ii. Create a Socket to connect to the server on the specified port.
iii. Get the input stream of the socket.
iv. Display a message indicating that the client is ready to receive data.
v. Read each byte from the input stream using InputStream.read().
vi. Print each character to the screen as it is received.
vii. Continue until the terminating character '#' is encountered.
viii. Close the input stream and socket.

Server Program:

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

public class DataTransfer {


public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(2050);
System.out.println("Server is waiting...");

Socket s = ss.accept();
System.out.println("Enter the data to send (end with '#'):");
OutputStream os = s.getOutputStream();
int c = 0;
while (c != '#') {
c = System.in.read(); // Read from keyboard
os.write((byte) c); // Send to client
}

os.close();
s.close();
ss.close();
}
}

Client Program:

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

public class DataReceive {


public static void main(String[] args) throws Exception {
Socket s = new Socket(InetAddress.getLocalHost(), 2050);
InputStream is = s.getInputStream();

System.out.println("Client is ready to receive data:");


int d = 0;
while (d != '#') {
d = is.read(); // Read from socket
System.out.print((char) d); // Print to screen
}

is.close();
s.close();
}
}

Output:

Server is waiting...
Enter the data to send (end with '#'):
hey from tce#

Client is ready to receive data:


hey from tce#
2. Implement Chat server using TCP

Algorithm:

Server:

i. Start the program.


ii. Create a ServerSocket on a specified port (e.g., 2050).
iii. Wait for the client to connect using accept().
iv. Get the OutputStream of the socket.
v. Prompt the user to type a message.
vi. Read each character from the keyboard using System.in.read().
vii. Send each character to the client using OutputStream.write().
viii. Stop sending when the terminating character '#' is encountered.
ix. Get the InputStream of the socket.
x. Read the response from the client character by character.
xi. Display the received message until '#' is encountered.
xii. Close the streams, socket, and server socket.
.

Client:

i. Start the program.


ii. Connect to the server using Socket and the specified port.
iii. Get the InputStream of the socket.
iv. Read and display the message from the server character by character until '#'
is received.
v. Get the OutputStream of the socket.
vi. Prompt the user to type a message to the server.
vii. Read each character from the keyboard using System.in.read().
viii. Send each character to the server using OutputStream.write().
ix. Stop sending when the character '#' is encountered.
x. Close the streams and socket.

Server Program:

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

public class ChatServer {


public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(2050);
System.out.println("Server is waiting for client...");
Socket soc = ss.accept();
System.out.println("Client connected.");

// Sending message to client


OutputStream os = soc.getOutputStream();
System.out.println("Message to client (end with '#'):");
int c = 0;
while (c != '#') {
c = System.in.read();
os.write((byte) c);
}

// Receiving message from client


InputStream is = soc.getInputStream();
System.out.println("Message from client:");
int d = 0;
while (d != '#') {
d = is.read();
System.out.print((char) d);
}

os.close();
is.close();
soc.close();
ss.close();
}
}

Client Program:

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

public class ChatClient {


public static void main(String args[]) throws Exception {
Socket s = new Socket(InetAddress.getLocalHost(), 2050);
System.out.println("Connected to server.");

// Receiving message from server


InputStream is = s.getInputStream();
System.out.println("Message from server:");
int d = 0;
while (d != '#') {
d = is.read();
System.out.print((char) d);
}

// Sending message to server


OutputStream os = s.getOutputStream();
System.out.println("\nMessage to server (end with '#'):");
int c = 0;
while (c != '#') {
c = System.in.read();
os.write((byte) c);
}

is.close();
os.close();
s.close();
}
}

Output:

Server is waiting for client...


Client connected.
Message to client (end with '#'):
hi from tce#
Message from client:
hello server#

Connected to server.
Message from server:
hi from tce#
Message to server (end with '#'):
hello server#
Post-lab:

1. Implement Time server using TCP

Algorithm:

Server:

i. Create a ServerSocket on a specified port (e.g., 3000).


ii. Wait for a client connection using accept().
iii. Get the output stream of the socket.
iv. Fetch the current system time using Date class.
v. Send the time as a string to the client.
vi. Close the socket and server.

Client:

i. Connect to the server using a Socket on the correct port.


ii. Get the input stream of the socket.
iii. Read the time sent by the server.
iv. Display the received time on the client console.
v. Close the socket.

Server Program:

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

public class TimeServer {


public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(3000);
System.out.println("Time Server is running...");

Socket s = ss.accept();
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);

Date date = new Date();


pw.println("Current Server Time: " + date);

pw.close();
s.close();
ss.close();
}
}

Client Program:

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

public class TimeClient {


public static void main(String[] args) throws Exception {
Socket s = new Socket(InetAddress.getLocalHost(), 3000);
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));

String serverTime = br.readLine();


System.out.println("Time received from server: " + serverTime);

br.close();
s.close();
}
}

Output:

Time Server is running...

Time received from server: Current Server Time: Mon Aug 05 10:45:12 IST 2025

2. Implement Echo server using TCP

Algorithm:

Server:
i. Create a ServerSocket on a port (e.g., 4000).
ii. Wait for client to connect using accept().
iii. Create input and output streams for the socket.
iv. Read input messages from the client.
v. Echo back the same messages using the output stream.
vi. Continue until the client sends "bye".
vii. Close all streams and sockets.
Client:

i. Connect to the server on the same port.


ii. Create input and output streams for communication.
iii. Read user input from the keyboard.
iv. Send the message to the server.
v. Read the echoed message from the server and display it.
vi. Repeat until "bye" is entered.
vii. Close all streams and sockets.

Server Program:

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

public class EchoServer {


public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(4000);
System.out.println("Echo Server is running...");

Socket s = ss.accept();
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);

String msg;
while ((msg = br.readLine()) != null) {
System.out.println("Received: " + msg);
pw.println("Echo: " + msg);
if (msg.equalsIgnoreCase("bye")) break;
}

br.close();
pw.close();
s.close();
ss.close();
}
}

Client Program:

import java.net.*;
import java.io.*;
public class EchoClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket(InetAddress.getLocalHost(), 4000);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));

String msg;
System.out.println("Enter messages to send (type 'bye' to exit):");

while (true) {
msg = userInput.readLine();
pw.println(msg);
String reply = br.readLine();
System.out.println("From server: " + reply);
if (msg.equalsIgnoreCase("bye")) break;
}

userInput.close();
br.close();
pw.close();
s.close();
}
}

Output:

Echo Server is running...


Received: hello
Received: how are you
Received: bye

Enter messages to send (type 'bye' to exit):


hello
From server: Echo: hello
how are you
From server: Echo: how are you
bye
From server: Echo: bye
Inference:

You might also like