CN Labda2
CN Labda2
CN Labda2
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;
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client:
import java.io.*;
import java.net.*;
import java.time.LocalDate;
out.println(dobString);
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;
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);
out.println(meaning);
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client:
import java.io.*;
import java.net.*;
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);
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;
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
out.println(factorial.toString());
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Client:
import java.io.*;
import java.net.*;
import java.math.BigInteger;
out.println(numberStr);
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.*;
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
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();
}
}
Client:
import java.io.*;
import java.net.*;
out.println(codeword);
String response = in.readLine();
System.out.println("Server response: " + response);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Output:
Caption