Ritesh Socket
Ritesh Socket
Submitted By Submitted To
Ritesh Dr. Priti Sharma
MCA , 3rd sem
Roll: 23121
INDEX
UNIX
1. Ls 1
2. Mkdir 2
3. Rmdir 2
4. Cd 3
5. Pwd 4
6. Cat 5
7. Cp 6
8. Rm 7
9. Mv 8
10. Wc 8
11. File 9
12. Cmp 9
13. Chmode 10
14. Touch 10
15. Tac 11
16. Tail 11
17. Head 12
13
18. Date
19. Who 13
14
20. Man
21. Cal 14
22. Tee 15
SOCKET PROGRAMMING
1) ls :
Command: ls
OUTPUT:-
Command: ls -1
OUTPUT:-
1
2) mkdir :
Command: mkdir directory_name
OUTPUT:-
3) rmdir :
OUTPUT:-
2
4) cd :
Command: cd directory
OUTPUT:-
Command: cd ..
OUTPUT:-
3
5) pwd :
Command: pwd
Output::-
4
2. File Oriented Commands:
6) cat :
Command: cat filename
Used for: It reads data from the file and gives their content as output
Output:-
Used for: It reads data from the file and gives their content as output
Output:-
5
Command: cat > filename
Output:-
7) cp :
Output:-
6
8) rm :
Command: rm filename
Output:-
Before rm command
Output:-
After rm command
7
9) mv :
Command: mv source destination_directory
Output:-
10) wc :
Used for:used to count word, alphabet and lines.
Output:-
8
11) file :
Command: file [option] [filename]
Output :-
12) cmp :
Command: cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
Output :-
9
13) chmode :
Command: $ chmod +w file.txt
$ chmod +r file.txt
$ chmod +x file.txt
Output :-
14) touch :
Output :-
10
15) tac :
Command: tac filename
Output:_
16) tail :
Command: tail filename
Output:
Output:
11
17) head :
Command: head filename
Used for: to prints the first lines of one or more files to standard
Output
Output :-
Output:
12
3. General Purpose Commands:
18) date :
Command: date
Output:-
19) who :
Command: who
Output:-
13
20) man :
Command: man ls
Used for: to display the user manual of any command that we can run on the
Terminal
Output:-
21) cal :
Command: cal
Used for: Shows current month calendar on the terminal with the current date
highlighted.
Output:-
14
22) tee :
15
1) WAP in java socket programming where client send a text message and
server receive and print it.
Server.java:-
import java.io.*;
import java.net.*;
16
} catch (IOException e) {
System.err.println("Error handling client: " + e.getMessage());
} finally {
try {
clientSocket.close();
} catch (IOException e) {
System.err.println("Error closing client socket: " + e.getMessage());
}
}
}
}
Output:-
17
Client.java:-
import java.io.*;
import java.net.*;
Output:-
18
2) WAP in java socket programming where client send text message
and received by server using read and write on both sides.
Server.java:-
import java.io.*;
import java.net.*;
// Close resources
in.close();
out.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Output:-
19
Client.java:-
import java.io.*;
import java.net.*;
// Close resources
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
20
3) WAP in java socket programming to check the given number is
prime or not.
Server.java:-
import java.io.*;
import java.net.*;
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress());
Output:-
22
Client.java:-
import java.io.*;
import java.net.*;
String userInputLine;
while (!(userInputLine = userInput.readLine()).equalsIgnoreCase("exit")) {
out.println(userInputLine);
String response = in.readLine();
System.out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
23
4) WAP in java socket programming to check the given
number is Armstrong or not.
Server.java:-
import java.io.*;
import java.net.*;
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");
new ArmstrongHandler(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
24
String inputLine;
while ((inputLine = in.readLine()) != null) {
int number = Integer.parseInt(inputLine);
boolean isArmstrong = isArmstrong(number);
out.println("Number " + number + " is " + (isArmstrong ? "an Armstrong number." :
"not an Armstrong number."));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private boolean isArmstrong(int number) {
int originalNumber = number;
int result = 0;
int digits = String.valueOf(number).length();
25
Client.java:-
import java.io.*;
import java.net.*;
String userInputLine;
while (!(userInputLine = userInput.readLine()).equalsIgnoreCase("exit")) {
out.println(userInputLine);
String response = in.readLine();
System.out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
26
5) WAP in java socket programming to check the given
number is palindrome or not.
Server.java:-
import java.io.*;
import java.net.*;
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");
new PalindromeHandler(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
try (BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
27
String inputLine;
while ((inputLine = in.readLine()) != null) {
String response = isPalindrome(inputLine)
? inputLine + " is a palindrome."
: inputLine + " is not a palindrome.";
out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
28
Client.java:-
import java.io.*;
import java.net.*;
String userInputLine;
while (!(userInputLine = userInput.readLine()).equalsIgnoreCase("exit")) {
out.println(userInputLine);
String response = in.readLine();
System.out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
29
6) WAP in java socket programming to check the given number is
even or odd.
Server.java:-
import java.io.*;
import java.net.*;
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");
new EvenOddHandler(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
try {
int number = Integer.parseInt(inputLine);
String response = isEven(number)
? number + " is even.
30
: number + " is odd.";
out.println(response);
} catch (NumberFormatException e) {
out.println("Please enter a valid integer.");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
31
Client.java:-
import java.io.*;
import java.net.*;
String userInputLine;
while (!(userInputLine = userInput.readLine()).equalsIgnoreCase("exit")) {
out.println(userInputLine);
String response = in.readLine();
System.out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:-
32
7) WAP in java socket programming to find out the factorial of a
number using bind() function.
Server.java:-
import java.io.*;
import java.net.*;
while (true) {
// Accept client connections
Socket socket = serverSocket.accept();
System.out.println("New client connected");
33
this.socket = socket;
}
@Override
public void run() {
try (BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
try {
int number = Integer.parseInt(inputLine);
long factorial = calculateFactorial(number);
out.println("Factorial of " + number + " is " + factorial);
} catch (NumberFormatException e) {
out.println("Please enter a valid integer.");
}
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
34
private long calculateFactorial(int number) {
if (number < 0) return -1; // Factorial of negative numbers is not defined
long result = 1;
for (int i = 1; i <= number; i++) {
result *= i;
}
return result;
}
}
Output:-
35
Client.java:-
import java.io.*;
import java.net.*;
String userInputLine;
while (!(userInputLine = userInput.readLine()).equalsIgnoreCase("exit")) {
out.println(userInputLine);
String response = in.readLine();
System.out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
36
8) WAP in java socket programming to find out the Fibonacci
series.
Server.java:-
import java.io.*;
import java.net.*;
while (true) {
// Accept client connections
Socket socket = serverSocket.accept();
System.out.println("New client connected");
37
@Override
public void run() {
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
try {
int number = Integer.parseInt(inputLine);
long factorial = calculateFactorial(number);
out.println("Factorial of " + number + " is " + factorial);
} catch (NumberFormatException e) {
out.println("Please enter a valid integer.");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
38
Client.java:-
import java.io.*;
import java.net.*;
String userInputLine;
while (!(userInputLine = userInput.readLine()).equalsIgnoreCase("exit")) {
out.println(userInputLine);
String response = in.readLine();
System.out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
39
9) WAP in java socket programming to find out the largest among three
numbers using if else.
Server.java:-
import java.io.*;
import java.net.*;
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");
new LargestNumberHandler(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
String[] numbers = inputLine.split(",");
if (numbers.length != 3) {
out.println("Please enter exactly three numbers separated by commas.");
continue;
40
}
try {
int num1 = Integer.parseInt(numbers[0].trim());
int num2 = Integer.parseInt(numbers[1].trim());
int num3 = Integer.parseInt(numbers[2].trim());
Output:-
41
Client.java:-
import java.io.*;
import java.net.*;
String userInputLine;
while ((userInputLine = userInput.readLine()) != null) {
out.println(userInputLine);
String response = in.readLine();
System.out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
42
10) WAP to illustrate client side program of simple calculator
using TCP.
Client.java:-
import java.io.*;
import java.net.*;
import java.util.Scanner;
try {
// Connect to the server
Socket socket = new Socket(serverAddress, port);
System.out.println("Connected to the server");
43
// Close the resources
socket.close();
scanner.close();
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Output:-
44
11) WAP to illustrate server side program of simple calculator using TCP.
Server.java:-
import java.io.*;
import java.net.*;
try {
// Create a server socket that listens on the specified port
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server is running and waiting for a client to connect...");
45
} else {
out.println("Error: Division by zero!");
clientSocket.close();
serverSocket.close();
return;
}
break;
default:
out.println("Error: Invalid operator!");
clientSocket.close();
serverSocket.close();
return;
}
Output:-
46
12) WAP to illustrate client side program of simple calculator
using UDP.
Client.java:-
import java.net.*;
import java.io.*;
import java.util.Scanner;
try {
// Create a DatagramSocket for UDP communication
DatagramSocket socket = new DatagramSocket();
InetAddress serverIP = InetAddress.getByName(serverAddress);
Output:-
48
13) WAP to illustrate server side program of simple
calculator using UDP.
Server.java:-
import java.net.*;
import java.io.*;
try {
// Create a DatagramSocket to listen for incoming requests
DatagramSocket socket = new DatagramSocket(port);
System.out.println("Server is running and waiting for client...");
while (true) {
// Receive the message from the client
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
socket.receive(receivePacket);
Output:-
50
14) WAP in java socket programming for client application in RMI.
Client.java:-
import java.rmi.Naming;
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:-
51
15) WAP in java socket programming for server application in RMI.
RMIServerInterface.java:-
import java.rmi.Remote;
import java.rmi.RemoteException;
RMIServerImpl.java:-
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
super();
@Override
return "Hello, " + name + "! Greetings from the RMI server.";
52
Server.java:-
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
// Bind the remote object (server) in the RMI registry with a name
Naming.rebind("rmi://localhost:1099/HelloService", server);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:-
53
16) WAP to retrieve date and time using UDP.
Server.java:-
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
try {
// Create a DatagramSocket to listen for incoming requests
DatagramSocket socket = new DatagramSocket(port);
System.out.println("DateTime Server is running on port " + port + "...");
while (true) {
// Buffer to receive incoming data
byte[] receiveBuffer = new byte[1024];
54
Output:-
55
Client.java:-
import java.net.*;
public class DateTimeClient {
public static void main(String[] args) {
String serverAddress = "localhost"; // Server address
int port = 9876; // Server port
try {
// Create a DatagramSocket for sending requests
DatagramSocket socket = new DatagramSocket();
Output:-
56
17) WAP to retrieve date and time using TCP.
Server.java:-
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
try {
// Create a DatagramSocket to listen for incoming requests
DatagramSocket socket = new DatagramSocket(port);
System.out.println("DateTime Server is running on port " + port + "...");
while (true) {
// Buffer to receive incoming data
byte[] receiveBuffer = new byte[1024];
57
Output:-
58
Client.java:-
import java.io.*;
import java.net.*;
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Output:-
59
18) Program for connection-oriented Iterative Service
in which server reverses the string sent by the client
and sends it back.
Server.java:-
60
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
61
Client.java:-
} catch (UnknownHostException e) {
System.out.println("Server not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("I/O error: " + e.getMessage());
}
}
}
Output:-
62
19) Program for connection-oriented Iterative service in
which server changes the case of the strings sent by
the client and sends back (Case Server).
Server.java:-
import java.io.*;
import java.net.*;
String clientInput;
while ((clientInput = input.readLine()) != null) {
String response = convertCase(clientInput);
output.println(response);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
63
if (input.equals(input.toLowerCase())) {
import java.net.*;
import java.io.*;
import java.util.*;
public class server{
public static void main(String [] args)throws IOException{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();
Output:-
64
Client.java:-
import java.io.*;
import java.net.*;
String userInput;
while (!(userInput = consoleInput.readLine()).equalsIgnoreCase("exit")) {
output.println(userInput);
String response = input.readLine();
System.out.println("Server response: " + response);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Output:-
65
20) WAP in java socket programming to demonstrate working of URL.
import java.io.*;
import java.net.*;
try {
// Create a socket to connect to the web server
Socket socket = new Socket(hostname, port);
66
Output:-
67