0% found this document useful (0 votes)
29 views8 pages

Network 62

Uploaded by

22cs054
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)
29 views8 pages

Network 62

Uploaded by

22cs054
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/ 8

Dr.N.G.

P Institute of Technology Reg No : 710722104052

Ex No : 04
DATE : Simulation of DNS using UDP sockets.

AIM:
To write a Java program for file transfer using TCP Sockets.
ALGORITHM:
Server:
1. Import necessary Java packages and create the class File Server.
2. Create a new server socket and bind it to the desired port.
3. Wait for and accept the client's connection.
4. Receive the file name from the client and store it using a Buffered Reader.
5. Create a File object and check if the requested file exists.
6. If the file exists, use a File Reader to read the file content and send it to
the client until the end of the file (EOF) is reached.
7. Close the connection and stop the program.
Client:
1. Import necessary Java packages and create the class File Client.
2. Create a socket to connect to the server at the given IP address and port.
3. Establish a connection with the server.
4. Send the file name to the server using a Print Writer.
5. Use a Buffered Reader to read the file content received from the server.
6. Close the connection and stop the program.
PROGRAM:
File Server (FileServer.java)
java
Copy code
import java.io.*;
import java.net.*;

public class FileServer {


public static void main(String[] args) {
try {
// Create server socket and bind to port 5000
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Server started. Waiting for connection...");

// Accept client connection


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

// Read the file name from client


Buffered Reader in From Client = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String fileName = inFromClient.readLine();

22UCS504 – COMPUTER NETWORKS LABORATORY


Dr.N.G.P Institute of Technology Reg No : 710722104052

System.out.println("File requested: " + fileName);

// Create file object and check if file exists


File file = new File(fileName);
PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(), true);

if (file.exists() && !file.isDirectory()) {


// Send file content to the client
BufferedReader fileReader = new BufferedReader(new FileReader(file));
String line;
while ((line = fileReader.readLine()) != null) {
outToClient.println(line);
}
fileReader.close();
System.out.println("File sent successfully.");
} else {
// Send error message if file not found
outToClient.println("File not found.");
System.out.println("File not found.");
}

// Close connections
inFromClient.close();
outToClient.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
File Client (FileClient.java)
java
Copy code
import java.io.*;
import java.net.*;

public class FileClient {


public static void main(String[] args) {
try {
// Create socket to connect to server at localhost and port 5000
Socket socket = new Socket("localhost", 5000);
System.out.println("Connected to the server.");

// Send file name to the server


PrintWriter outToServer = new PrintWriter(socket.getOutputStream(), true);

22UCS504 – COMPUTER NETWORKS LABORATORY


Dr.N.G.P Institute of Technology Reg No : 710722104052

BufferedReader inFromServer = new BufferedReader(new

InputStreamReader(socket.getInputStream()));
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));

System.out.print("Enter the file name to download: ");


String fileName = userInput.readLine();
outToServer.println(fileName);

// Read and display the content received from the server


String response;
System.out.println("\nFile Content:");
while ((response = inFromServer.readLine()) != null) {
System.out.println(response);
}

// Close connections
userInput.close();
inFromServer.close();
outToServer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:
Server Output:

22UCS504 – COMPUTER NETWORKS LABORATORY


Dr.N.G.P Institute of Technology Reg No : 710722104052

Client Output:

RESULT:
Thus the java application program using UDP Sockets to implement DNS was developed
and executed successfully.

22UCS504 – COMPUTER NETWORKS LABORATORY


Dr.N.G.P Institute of Technology Reg No : 710722104052

Ex No : 05
DATE : Use a tool like Wireshark to capture packets and
examine the packets

AIM:
To capture packets during file transfer using TCP Sockets and examine the packets
using a tool like Wireshark.
ALGORITHM:
Server:
1. Import necessary Java packages and create the class File Server.
2. Create a new server socket and bind it to the desired port.
3. Wait for and accept the client's connection.
4. Receive the file name from the client and store it using a Buffered Reader.
5. Create a File object and check if the requested file exists.
6. If the file exists, use a File Reader to read the file content.
7. Close the connection and stop the program.
Client:
1. Import necessary Java packages and create the class File Client.
2. Create a socket to connect to the server at the given IP address and port.
3. Establish a connection with the server.
4. Send the file name to the server using a Print Writer.
5. Use a Buffered Reader to read the file content received from the server.
6. Close the connection and stop the program.

PROGRAM:
File Server (FileServer.java)
java
Copy code
import java.io.*;
import java.net.*;

public class FileServer {


public static void main(String[] args) {
try {
// Create server socket and bind to port 5000
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Server started. Waiting for connection...");

// Accept client connection


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

// Read the file name from client


BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String fileName = inFromClient.readLine();

22UCS504 – COMPUTER NETWORKS LABORATORY


Dr.N.G.P Institute of Technology Reg No : 710722104052

System.out.println("File requested: " + fileName);


//Create file object and check if file exists
File file = new File(fileName);
PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(), true);

if (file.exists() && !file.isDirectory()) {


// Send file content to the client
BufferedReader fileReader = new BufferedReader(new FileReader(file));
String line;
while ((line = fileReader.readLine()) != null) {
outToClient.println(line);
}
fileReader.close();
System.out.println("File sent successfully.");
} else {
// Send error message if file not found
outToClient.println("File not found.");
System.out.println("File not found.");
}

// Close connections
inFromClient.close();
outToClient.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
File Client (FileClient.java)
java
Copy code
import java.io.*;
import java.net.*;

public class FileClient {


public static void main(String[] args) {
try {
// Create socket to connect to server at localhost and port 5000
Socket socket = new Socket("localhost", 5000);
System.out.println("Connected to the server.");

// Send file name to the server


PrintWriter outToServer = new PrintWriter(socket.getOutputStream(), true);

22UCS504 – COMPUTER NETWORKS LABORATORY


Dr.N.G.P Institute of Technology Reg No : 710722104052

BufferedReader inFromServer = new BufferedReader(new


InputStreamReader(socket.getInputStream()));
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));

System.out.print("Enter the file name to download: ");


String fileName = userInput.readLine();
outToServer.println(fileName);

// Read and display the content received from the server


String response;
System.out.println("\nFile Content:");
while ((response = inFromServer.readLine()) != null) {
System.out.println(response);
}

// Close connections
userInput.close();
inFromServer.close();
outToServer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

OUTPUT:
Server Output:

22UCS504 – COMPUTER NETWORKS LABORATORY


Dr.N.G.P Institute of Technology Reg No : 710722104052

Client Output:

RESULT:
Thus, the Java application program using TCP Sockets to implement file transfer was
developed and executed successfully. The packets during the file transfer were captured and
examined using Wireshark.

22UCS504 – COMPUTER NETWORKS LABORATORY

You might also like