CN Labda2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Name: Hariharan.

s
Regno: 22mis0340

1. Write a TCP/IP-based client-server program to display the age of the student from the DOB
using TCP client and server.
Server:

import java.io.*;
import java.net.*;
import java.time.LocalDate;
import java.time.Period;

public class Server {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("Server started. Waiting for a
client...");

Socket clientSocket = serverSocket.accept();


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

BufferedReader in = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);

String dobString = in.readLine();


LocalDate dob = LocalDate.parse(dobString);
LocalDate today = LocalDate.now();
int age = Period.between(dob, today).getYears();
out.println(age);

clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Client:

import java.io.*;
import java.net.*;
import java.time.LocalDate;

public class Client {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 9999);

BufferedReader userInput = new BufferedReader(new


InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new
PrintWriter(socket.getOutputStream(), true);

System.out.print("Enter Date of Birth (YYYY-MM-DD):


");
String dobString = userInput.readLine();

out.println(dobString);

String age = in.readLine();


System.out.println("Age of student: " + age);

socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutPut:

Caption

2. Write an online dictionary application by using the TCP client-server program. User will search
the dictionary (available at the server) and get the meaning of the words.

Server:

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

public class DictionaryServer {


private static HashMap<String, String> dictionary = new
HashMap<>();

public static void main(String[] args) {


// Populate the dictionary with some sample words and
meanings
dictionary.put("apple", "a round fruit with red or green
skin and a whitish interior");
dictionary.put("banana", "a long curved fruit that grows
in clusters and has soft pulpy flesh and yellow skin when ripe");
dictionary.put("cat", "a small domesticated carnivorous
mammal with soft fur, a short snout, and retractile claws");

try {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("Dictionary Server started. Waiting
for a client...");

while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);

String word = in.readLine();


String meaning = dictionary.getOrDefault(word,
"Word not found in the dictionary.");

out.println(meaning);

clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Client:

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

public class DictionaryClient {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 9999);

BufferedReader userInput = new BufferedReader(new


InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new
PrintWriter(socket.getOutputStream(), true);

while (true) {
System.out.print("Enter a word to search in the
dictionary (or 'exit' to quit): ");
String word = userInput.readLine();

if (word.equalsIgnoreCase("exit")) {
break;
}

out.println(word);

String meaning = in.readLine();


System.out.println("Meaning: " + meaning);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output:

3. Implement a TCP/IP-based Server program to compute the factorial of a number (can contain an
arbitrary number of digits). Write a client program to test the working of the same.

Server:

import java.io.*;
import java.net.*;
import java.math.BigInteger;

public class FactorialServer {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("Factorial Server started. Waiting
for a client...");

while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");

BufferedReader in = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);

String numberStr = in.readLine();


BigInteger number = new BigInteger(numberStr);
BigInteger factorial = calculateFactorial(number);

out.println(factorial.toString());

clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

private static BigInteger calculateFactorial(BigInteger


number) {
if (number.equals(BigInteger.ZERO)) {
return BigInteger.ONE;
} else {
return
number.multiply(calculateFactorial(number.subtract(BigInteger.ONE)
));
}
}
}

Client:

import java.io.*;
import java.net.*;
import java.math.BigInteger;

public class FactorialClient {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 9999);

BufferedReader userInput = new BufferedReader(new


InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new
PrintWriter(socket.getOutputStream(), true);

System.out.print("Enter a number to calculate its


factorial: ");
String numberStr = userInput.readLine();

out.println(numberStr);

String factorialStr = in.readLine();


System.out.println("Factorial: " + factorialStr);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output:

4. Implement a TCP/IP-based client-server program for checking whether the message sent by the
sender is erroneous or not through the CRC mechanism. Only prepare the codeword at the client
program from the data word and send it to the server.

Server:

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

public class CRCServer {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("CRC Server started. Waiting for a
client...");

while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");

BufferedReader in = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);

String receivedCodeword = in.readLine();

// Perform CRC check on received codeword


boolean isErroneous =
performCRC(receivedCodeword);

if (isErroneous) {
out.println("Error detected: Codeword is
erroneous.");
} else {
out.println("No error detected: Codeword is
correct.");
}

clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

private static boolean performCRC(String codeword) {


// Perform CRC check here
// Return true if error detected, false otherwise
return false; // Placeholder for CRC check result
}
}

Client:

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

public class CRCClient {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 9999);

BufferedReader userInput = new BufferedReader(new


InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new
PrintWriter(socket.getOutputStream(), true);

System.out.print("Enter data word to send: ");


String dataWord = userInput.readLine();

// Prepare codeword using CRC mechanism (not


implemented here)
String codeword = prepareCRC(dataWord);

out.println(codeword);
String response = in.readLine();
System.out.println("Server response: " + response);

socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private static String prepareCRC(String dataWord) {


// Implement CRC mechanism to generate codeword from data
word
// Return the generated codeword
return "101010101"; // Placeholder for generated codeword
}
}

Output:

Caption

You might also like