0% found this document useful (0 votes)
5 views18 pages

Program

The document contains multiple Java programs demonstrating various networking concepts, including file transfer using TCP and UDP sockets, ARP protocol simulation, DNS client-server communication, a TCP date server, and a simple chat application. Each program is accompanied by its output, showcasing successful execution and interaction between client and server. The examples illustrate fundamental networking operations and protocol implementations in Java.

Uploaded by

Mohammed zakir
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)
5 views18 pages

Program

The document contains multiple Java programs demonstrating various networking concepts, including file transfer using TCP and UDP sockets, ARP protocol simulation, DNS client-server communication, a TCP date server, and a simple chat application. Each program is accompanied by its output, showcasing successful execution and interaction between client and server. The examples illustrate fundamental networking operations and protocol implementations in Java.

Uploaded by

Mohammed zakir
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

Program: File transfer using TCP Sockets.

File Server :
import [Link].*;
import [Link].*;

public class FileServer {


public static void main(String[] args) {
int port = 12345;
try (ServerSocket serverSocket = new ServerSocket(port)) {
[Link]("Server started. Waiting for client...");

// Accept client connection


Socket clientSocket = [Link]();
[Link]("Client connected.");

// Get input stream from the client socket


InputStream inputStream = [Link]();
FileOutputStream fileOutputStream = new
FileOutputStream("received_file_sample.txt");

byte[] buffer = new byte[4096];


int bytesRead;
while ((bytesRead = [Link](buffer)) != -1) {
[Link](buffer, 0, bytesRead);
}

[Link]("File received successfully!");

// Close streams and socket


[Link]();
[Link]();
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
File Client:
import [Link].*;
import [Link].*;

public class FileClient {


public static void main(String[] args) {
String serverAddress = "localhost";
int port = 12345;

try (Socket socket = new Socket(serverAddress, port)) {


[Link]("Connected to server.");

OutputStream outputStream = [Link]();


FileInputStream fileInputStream = new FileInputStream("[Link]");

byte[] buffer = new byte[4096];


int bytesRead;
while ((bytesRead = [Link](buffer)) != -1) {
[Link](buffer, 0, bytesRead);
}

[Link]("File sent successfully!");

// Close streams and socket


[Link]();
[Link]();
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
OUTPUT:

Server started. Waiting for client...


Client connected.
File received successfully!
PROGRAM: File transfer using UCP

/*CLIENT CLASS*/

import [Link].*;
import [Link].*;

public class Client


{
public static void main(String args[]) throws Exception
{
DatagramSocket socket = new DatagramSocket(2000);
FileInputStream fis = new FileInputStream("D:/MCA lab/lab 2/[Link]");

byte[] buffer = new byte[1024];


int bytesRead = [Link](buffer);

DatagramPacket packet = new DatagramPacket(


buffer,
bytesRead,
[Link](),
1000
);

[Link](packet);

[Link]();
[Link]();

[Link]("File sent successfully.");


}
}
/*SERVER CLASS*/

import [Link].*;
import [Link].*;

public class Server


{
public static void main(String args[]) throws Exception
{
DatagramSocket socket = new DatagramSocket(1000);

byte[] buffer = new byte[1024];


DatagramPacket packet = new DatagramPacket(buffer, [Link]);

[Link](packet);

FileOutputStream fos = new FileOutputStream("D:/[Link]");


[Link]([Link](), 0, [Link]());

[Link]("File received successfully.");


[Link](new String([Link](), 0, [Link]()));
[Link]();
[Link]();
}
}
OUTPUT:

SERVER:

D:\MCA lab\lab 2>java Server


File received successfully.
miet arts & science college

CLIENT:

D:\MCA lab\lab 2>java Client


File sent successfully
PROGRAM: Designing of ARP protocol

ARP CLIENT

import [Link];
import [Link];

public class ARP {


public static void main(String[] args) {
// Creating a mapping of IP addresses to MAC addresses (ARP Table)
HashMap<String, String> arpTable = new HashMap<>();

// Adding some static entries to the ARP table


[Link]("[Link]", "00-14-22-01-23-45");
[Link]("[Link]", "00-14-22-01-23-46");
[Link]("[Link]", "00-14-22-01-23-47");
[Link]("[Link]", "00-14-22-01-23-48");

Scanner scanner = new Scanner([Link]);

[Link]("===== ARP Protocol Simulation =====");


[Link]("Enter an IP Address to find its MAC Address: ");
String ipAddress = [Link]();

// Checking if the IP exists in the ARP table


if ([Link](ipAddress)) {
[Link]("MAC Address for " + ipAddress + " is: " + [Link](ipAddress));
} else {
[Link]("IP Address not found in the ARP table. Sending ARP Request...");
[Link]("No Response. Entry not found!");
}

[Link]();
}
}
OUTPUT

D:\MCA lab\lab 3>java ARP


===== ARP Protocol Simulation =====
Enter an IP Address to find its MAC Address: [Link]
MAC Address for [Link] is: 00-14-22-01-23-46
PROGRAM: DNS CLIENT SERVER

// UDP DNS Server -- [Link]


import [Link].*;
import [Link].*;

public class udpdnsserver


{
// Method to find hostname index
private static int indexOf(String[] array, String str)
{
str = [Link]();
for (int i = 0; i < [Link]; i++)
{
if (array[i].equalsIgnoreCase(str))
return i;
}
return -1;
}

public static void main(String arg[]) throws IOException


{
String[] hosts = {
"[Link]", "[Link]",
"[Link]", "[Link]"
};

String[] ip = {
"[Link]", "[Link]",
"[Link]", "[Link]"
};

DatagramSocket serversocket = new DatagramSocket(1362);


[Link]("Press Ctrl + C to Quit");

while (true)
{
byte[] receivedata = new byte[1024];
byte[] senddata;

DatagramPacket recvpack =
new DatagramPacket(receivedata, [Link]);
[Link](recvpack);

String hostname = new String(


[Link](), 0, [Link]()
).trim();

[Link]("Request for host " + hostname);


InetAddress ipaddress = [Link]();
int port = [Link]();

String response;
int index = indexOf(hosts, hostname);

if (index != -1)
response = ip[index];
else
response = "Host Not Found";

senddata = [Link]();

DatagramPacket sendpack =
new DatagramPacket(senddata, [Link], ipaddress, port);

[Link](sendpack);
}
}
}

//UDP DNS Client -- [Link]


import [Link].*;
import [Link].*;

public class udpdnsclient


{
public static void main(String args[]) throws IOException
{
BufferedReader br =
new BufferedReader(new InputStreamReader([Link]));

DatagramSocket clientsocket = new DatagramSocket();

InetAddress ipaddress;
if ([Link] == 0)
ipaddress = [Link]();
else
ipaddress = [Link](args[0]);

byte[] senddata;
byte[] receivedata = new byte[1024];

int portaddr = 1362;

[Link]("Enter the hostname : ");


String sentence = [Link]();
senddata = [Link]();

DatagramPacket pack =
new DatagramPacket(senddata, [Link], ipaddress, portaddr);
[Link](pack);

DatagramPacket recvpack =
new DatagramPacket(receivedata, [Link]);
[Link](recvpack);

String result = new String(


[Link](), 0, [Link]()
).trim();

[Link]("IP Address: " + result);

[Link]();
}
}
OUTPUT:
Server:
Press Ctrl + C to Quit
Request for host [Link]
Request for host [Link]
Request for host [Link]

Client:
Enter the hostname : [Link]
IP Address: [Link]

Enter the hostname : [Link]


IP Address: Host Not Found
PROGRAM:

//TCP Date [Link]

import [Link].*;
import [Link].*;
import [Link].*;
class tcpdateserver
{
public static void main(String arg[])
{
ServerSocket ss;
Socket cs;
PrintStream ps;
BufferedReader dis;
String inet;
try
{
ss = new ServerSocket(4444);
[Link]("Press Ctrl+C to quit");
while (true)
{
cs = [Link]();
// Send date to client
ps = new PrintStream([Link]());
Date d = new Date();
[Link](d);

// Receive client address


dis = new BufferedReader(
new InputStreamReader([Link]()));
inet = [Link]();

[Link](
"Client System/IP address is : " + inet);

[Link]();
[Link]();
[Link]();
}
}
catch (IOException e)
{
[Link]("The exception is : " + e);
}
}}
// TCP Date [Link]

import [Link].*;
import [Link].*;

class tcpdateclient
{
public static void main (String args[])
{
Socket soc;
BufferedReader dis;
String sdate;
PrintStream ps;

try
{
InetAddress ia = [Link]();

if ([Link] == 0)
soc = new Socket([Link](), 4444);
else
soc = new Socket([Link](args[0]), 4444);

// Read date from server


dis = new BufferedReader(
new InputStreamReader([Link]()));
sdate = [Link]();

[Link](
"The date/time on server is : " + sdate);

// Send client address to server


ps = new PrintStream([Link]());
[Link](ia);

[Link]();
[Link]();
[Link]();
}
catch (IOException e)
{
[Link]("THE EXCEPTION is : " + e);
}
}
}
OUTPUT:

Server:
Client System/IP address is : [Link]/[Link]

Client:
The date/time on server is : Wed Jan 04 07:12:23 GMT 2023
PROGRAM: CHAT

[Link]

import [Link].*;
import [Link].*;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5000);
[Link]("Server started. Waiting for client...");
Socket socket = [Link]();
[Link]("Client connected.");
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
BufferedReader userInput = new BufferedReader(new InputStreamReader([Link]));
String clientMessage, serverMessage;
while (true) {
clientMessage = [Link]();
if ([Link]("exit")) {
[Link]("Client disconnected.");
break;
}
[Link]("Client: " + clientMessage);
[Link]("You: ");
serverMessage = [Link]();
[Link](serverMessage);
}
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
} }}
[Link]

import [Link].*;
import [Link].*;
public class ChatClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5000);
[Link]("Connected to server.");
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
BufferedReader userInput = new BufferedReader(new
InputStreamReader([Link]));
String clientMessage, serverMessage;
while (true) {
[Link]("You: ");
clientMessage = [Link]();
[Link](clientMessage);
if ([Link]("exit")) {
break;
}
serverMessage = [Link]();
[Link]("Server: " + serverMessage);
}
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
OUTPUT:

SERVER

D:\MCA lab\lab chat>java ChatServer


Server started. Waiting for client...
Client connected.
Client: hello
You: miet

CLIENT

D:\MCA lab\lab chat>java ChatClient


Connected to server.
You: hello
Server: miet
You: miet

You might also like