Ex.
No:6
Client-Server Model Using TCP
Date:07/08/2025
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 Port
Protocol Purpose
Number
HTTP 80 Web browsing (Hypertext Transfer)
HTTPS 443 Secure web browsing
FTP (Control) 21 File transfer control commands
FTP (Data) 20 File transfer data channel
SMTP 25 Sending emails
IMAP 143 Receiving emails (Internet Message Access Protocol)
IMAPS 993 Secure IMAP
POP3 110 Receiving emails (Post Office Protocol v3)
POP3S 995 Secure POP3
Telnet 23 Remote terminal access
SSH 22 Secure shell remote access
DNS (TCP mode) 53 Domain Name System (zone transfers)
LDAP 389 Directory services
LDAPS 636 Secure LDAP
SMB 445 File sharing in Windows
2. Write the syntax and its explanation for the following:
a. OutputStream :
OutputStream out = new FileOutputStream("file.txt");
b. InputStream :
InputStream in = new FileInputStream("file.txt");
c. InputStreamReader :
InputStreamReader reader = new InputStreamReader(System.in);
d. BufferedReader:
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
In-Lab Programs:
1. Establish reliable connection (TCP) between client and server, send message
from server to client and print message in client.
Output:
Algorithm:
1. Initialize Server Socket
2. Wait for Client Connection
3. Prompt for Input
4. Send Data to Client
5. Check for Termination Character
6. Handle Exceptions
7. Close Resources
Server Program:
import java.net.*;
import java.io.*;
public class Server1{
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
OutputStream os = null;
try {
ss = new ServerSocket(2050);
System.out.println("Server is waiting for connection...");
s = ss.accept();
System.out.println("Client connected.");
System.out.println("Enter the data to send (end with '#'):");
os = s.getOutputStream();
int c;
while ((c = System.in.read()) != -1) {
os.write(c);
os.flush();
if (c == '#') {
break;
}
}
System.out.println("Data sent. Closing connection...");
} catch (IOException e) {
System.out.println("IOException occurred: " + e.getMessage());
e.printStackTrace();
}
finally {
try {
if (os != null) os.close();
if (s != null) s.close();
if (ss != null) ss.close();
} catch (IOException e) {
System.out.println("Error closing resources: " + e.getMessage());
}
}
}
}
Client Program:
import java.net.*;
import java.io.*;
public class Client1{
public static void main(String[] args) {
Socket s = null;
InputStream is = null;
try {
s = new Socket(InetAddress.getLocalHost(), 2050);
is = s.getInputStream();
System.out.println("Client is ready to receive data:");
int d;
while ((d = is.read()) != -1) {
System.out.print((char) d);
if (d == '#') {
break;
}
}
} catch (IOException e) {
System.out.println("IOException occurred: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (is != null) is.close();
if (s != null) s.close();
} catch (IOException e) {
System.out.println("Error closing resources: " + e.getMessage());
}
}
}
}
2. Implement Chat server using TCP
Algorithm:
Create Server Socket
Accept Client Connection
Initialize I/O Streams
Prompt for Message Input
Send Message to Client
Receive Message from Client
Close Connections
Server Program:
import java.net.*;
import java.io.*;
public class Server2 {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(2050);
System.out.println("Server started. Waiting for client...");
Socket socket = serverSocket.accept();
System.out.println("Client connected.");
BufferedReader consoleReader = new BufferedReader(new
InputStreamReader(System.in));
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
System.out.print("Enter message to send to client (end with #): ");
int ch;
while ((ch = consoleReader.read()) != '#') {
outputStream.write(ch);
}
Output:
outputStream.write('#');
outputStream.flush();
System.out.println("\nMessage from client:");
while ((ch = inputStream.read()) != '#') {
System.out.print((char) ch);
}
socket.close();
serverSocket.close();
}
}
Client Program:
import java.net.*;
import java.io.*;
public class Client2 {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 2050);
System.out.println("Connected to server.");
BufferedReader consoleReader = new BufferedReader(new
InputStreamReader(System.in));
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
System.out.print("Enter message to send to server (end with #): ");
int ch;
while ((ch = consoleReader.read()) != '#') {
outputStream.write(ch);
}
outputStream.write('#');
outputStream.flush();
System.out.println("\nMessage from server:");
while ((ch = inputStream.read()) != '#') {
System.out.print((char) ch);
}
socket.close();
}
}
Post-lab:
Inference:
Criteria Marks
Pre-Lab /10
Experiments /30
Quiz /10
On-Time /10
Post-Lab / 15
Total /75
Result:
Thus the implement a server-client model using Transmission Control Protocol
(TCP) to sending message to Client and Server has been executed successfully.