0% found this document useful (0 votes)
3 views22 pages

CN Program Final

The document contains Java code for various networking applications, including a file download client, a TCP server and client, a DNS client and server, ARP client and server, and a CRC error detection program. Each section demonstrates different networking concepts and protocols, such as TCP, UDP, and error checking. The code snippets illustrate how to establish connections, send and receive data, and handle exceptions in Java.
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)
3 views22 pages

CN Program Final

The document contains Java code for various networking applications, including a file download client, a TCP server and client, a DNS client and server, ARP client and server, and a CRC error detection program. Each section demonstrates different networking concepts and protocols, such as TCP, UDP, and error checking. The code snippets illustrate how to establish connections, send and receive data, and handle exceptions in Java.
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

HTTP WEB CLIENT PROGRAM:

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

public class Download {


public static void main(String[] args) throws Exception {
try {
String fileName = "digital_image_processing.jpg";
String website = "[Link] + fileName;

[Link]("Downloading File From: " + website);

URL url = new URL(website);

InputStream inputStream = [Link]();


OutputStream outputStream = new FileOutputStream(fileName);

byte[] buffer = new byte[2048];


int length = 0;

while ((length = [Link](buffer)) != -1) {


[Link]("Buffer Read of length: " + length);
[Link](buffer, 0, length);
}

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

[Link]("Download Completed!");

} catch (Exception e) {
[Link]("Exception: " + [Link]());
}
}
}
[Link]

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

public class Server {


public static final int PORT = 4000;

public static void main(String args[]) {


ServerSocket sersock = null;
Socket sock = null;

try {
sersock = new ServerSocket(PORT);
[Link]("Server Started :" + sersock);

try {
sock = [Link]();
[Link]("Client Connected :" + sock);

DataInputStream ins = new DataInputStream([Link]());


[Link]([Link]());

PrintStream ios = new PrintStream([Link]());


[Link]("Hello from server");

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

} catch (SocketException se) {


[Link]("Server Socket problem " + [Link]());
}

} catch (Exception e) {
[Link]("Couldn't start " + [Link]());
}

[Link](" Connection from : " + [Link]());


}
}
[Link]

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

class client {
public static void main(String args[]) {
Socket sock = null;
DataInputStream dis = null;
PrintStream ps = null;

[Link](" Trying to connect");

try {
sock = new Socket([Link](), [Link]);

ps = new PrintStream([Link]());
[Link](" Hi from client");

DataInputStream is = new DataInputStream([Link]());


[Link]([Link]());

} catch (SocketException e) {
[Link]("SocketException " + e);
} catch (IOException e) {
[Link]("IOException " + e);
} finally {
try {
}
[Link]();
catch (IOException ie) {
[Link](" Close Error :" + [Link]());
}
}
}
}
[Link]
import [Link].*;
import [Link].*;

class tcpchatserver
{
public static void main(String args[]) throws Exception
{
PrintWriter toClient;
BufferedReader fromUser, fromClient;

try
{
ServerSocket Srv = new ServerSocket(4000);
[Link]("Server started");

Socket Clt = [Link]();


[Link]("Client connected");

toClient = new PrintWriter(


new BufferedWriter(
new OutputStreamWriter([Link]())), true);

fromClient = new BufferedReader(


new InputStreamReader([Link]()));

fromUser = new BufferedReader(


new InputStreamReader([Link]));

String CltMsg, SrvMsg;

while (true)
{
CltMsg = [Link]();

if ([Link]("end"))
break;

[Link]("Client : " + CltMsg);


[Link]("Message to Client : ");
SrvMsg = [Link]();
[Link](SrvMsg);

if ([Link]("end"))
break;
}

[Link]("Client Disconnected");

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

catch (Exception E)
{
[Link]([Link]());
}
}
}

[Link]

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

class tcpchatclient
{
public static void main(String args[]) throws Exception
{
Socket Clt;
PrintWriter toServer;
BufferedReader fromUser, fromServer;

try
{
Clt = new Socket([Link](), 4000);

toServer = new PrintWriter(


new BufferedWriter(
new OutputStreamWriter([Link]())), true);

fromServer = new BufferedReader(


new InputStreamReader([Link]()));

fromUser = new BufferedReader(


new InputStreamReader([Link]));

String CltMsg, SrvMsg;

[Link]("Type \"end\" to Quit");

while (true)
{
[Link]("Message to Server : ");
CltMsg = [Link]();
[Link](CltMsg);

if ([Link]("end"))
break;

SrvMsg = [Link]();
[Link]("Server : " + SrvMsg);

if ([Link]("end"))
break;
}

[Link]();
}
catch (Exception E)
{
[Link]([Link]());
}
}
}
[Link]

import [Link].*;

import [Link].*;

public class dnsclient

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 = new byte[1024];

byte[] receivedata = new byte[1024];

int portaddr = 8080;

[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 modified = new String([Link]());

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


[Link]();

[Link]

import [Link].*;

import [Link].*;

public class dnsserver

private static int indexOf(String[] array, String str)

str = [Link]();

for (int i=0; i < [Link]; i++)

if (array[i].equals(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]"};

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

while (true)

DatagramSocket serversocket=new DatagramSocket(8080);


byte[] senddata = new byte[1021];

byte[] receivedata = new byte[1021];

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

[Link](recvpack);

String sen = new String([Link]());

InetAddress ipaddress = [Link]();

int port = [Link]();

String capsent;

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

if(indexOf (hosts, sen) != -1)

capsent = ip[indexOf (hosts, sen)];

else

capsent = "Host Not Found";

senddata = [Link]();

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

[Link](pack);

[Link]();

}
[Link]

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

class Clientarp {
public static void main(String args[]) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));

Socket clsct = new Socket("[Link]", 139);

BufferedReader din = new BufferedReader(new


InputStreamReader([Link]()));
DataOutputStream dout = new DataOutputStream([Link]());

[Link]("Enter the Logical Address (IP): ");


String str1 = [Link]();

[Link](str1 + "\n");

String str = [Link]();


[Link]("The Physical Address is: " + str);

[Link]();
} catch (Exception e) {
[Link](e);
}
}
}

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

class Serverarp {
public static void main(String args[]) {
try {
ServerSocket obj = new ServerSocket(139);
[Link]("Server Started...");

Socket obj1 = [Link]();


[Link]("Client Connected...");

BufferedReader din = new BufferedReader(new


InputStreamReader([Link]()));
DataOutputStream dout = new DataOutputStream([Link]());

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


String mac[] = {"6A:08:AA:C2", "8A:BC:E3:FA"};
while (true) {
String str = [Link]();

for (int i = 0; i < [Link]; i++) {


if ([Link](ip[i])) {
[Link](mac[i] + "\n");
break;
}
}
}
} catch (Exception e) {
[Link](e);
}
}
}
[Link]
import [Link].*;
import [Link].*;

public class clientrarp {


public static void main(String args[]) {
try {
DatagramSocket client = new DatagramSocket();
InetAddress addr = [Link]("[Link]");

byte[] sendByte;
byte[] receiveByte = new byte[1024];

BufferedReader in = new BufferedReader(new InputStreamReader([Link]));

[Link]("Enter the Physical Address (MAC): ");


String str = [Link]();

sendByte = [Link]();

DatagramPacket sender = new DatagramPacket(sendByte, [Link], addr, 1309);


[Link](sender);

DatagramPacket receiver = new DatagramPacket(receiveByte, [Link]);


[Link](receiver);

String s = new String([Link]()).trim();


[Link]("The Logical Address is: " + s);

[Link]();
catch (Exception e) {
[Link](e);
}
}
}

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

public class serverrarp {


public static void main(String args[]) {
try {
DatagramSocket server = new DatagramSocket(1309);
[Link]("Server Started...");

while (true) {
byte[] receiveByte = new byte[1024];
byte[] sendByte;

DatagramPacket receiver = new DatagramPacket(receiveByte, [Link]);


[Link](receiver);
String str = new String([Link]()).trim();

InetAddress addr = [Link]();


int port = [Link]();

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


String mac[] = {"D4:3D:7E:12:A3:D9"};

for (int i = 0; i < [Link]; i++) {


if ([Link](mac[i])) {
sendByte = ip[i].getBytes();
DatagramPacket sender = new DatagramPacket(sendByte, [Link], addr,
port);
[Link](sender);
break;
}
}
}
} catch (Exception e) {
[Link](e);
}
}
}
[Link]
import [Link].*;

class crc {
public static void main(String args[]) {
Scanner input = new Scanner([Link]);

// Read data
[Link]("Enter the size of the data:");
int n = [Link]();

int data[] = new int[n];


[Link]("Enter the data bits:");
for (int i = 0; i < n; i++) {
data[i] = [Link]();
}

// Read divisor
[Link]("Enter the size of the divisor:");
int m = [Link]();

int divisor[] = new int[m];


[Link]("Enter the divisor bits:");
for (int i = 0; i < m; i++) {
divisor[i] = [Link]();
}

// Append zeros to data


int appendedData[] = new int[n + m - 1];
[Link](data, 0, appendedData, 0, n);

// Perform division
int remainder[] = divide(appendedData, divisor);

[Link]("CRC (Remainder): ");


for (int i = 0; i < [Link] - 1; i++) {
[Link](remainder[i]);
}

// Generate CRC code


int crcCode[] = new int[n + m - 1];
[Link](data, 0, crcCode, 0, n);
[Link](remainder, 0, crcCode, n, m - 1);

[Link]("\nCRC Code: ");


for (int i = 0; i < [Link]; i++) {
[Link](crcCode[i]);
}

// Receiver side
[Link]("\nEnter received data:");
int received[] = new int[n + m - 1];
for (int i = 0; i < [Link]; i++) {
received[i] = [Link]();
}

receive(received, divisor);
}

static int[] divide(int data[], int divisor[]) {


int remainder[] = new int[[Link]];
[Link](data, 0, remainder, 0, [Link]);

for (int i = 0; i < [Link] - [Link] + 1; i++) {


if (remainder[0] == 1) {
for (int j = 1; j < [Link]; j++) {
remainder[j - 1] = exor(remainder[j], divisor[j]);
}
} else {
for (int j = 1; j < [Link]; j++) {
remainder[j - 1] = exor(remainder[j], 0);
}
}

remainder[[Link] - 1] = data[i + [Link]];


}

return remainder;
}

static int exor(int a, int b) {


return (a == b) ? 0 : 1;
}

static void receive(int data[], int divisor[]) {


int remainder[] = divide(data, divisor);

for (int i = 0; i < [Link]; i++) {


if (remainder[i] != 0) {
[Link]("Error detected in received data");
return;
}
}

[Link]("No error in received data");


}
}
1
2
3
4
TCP

UDP

2. Traffic received (packets/sec) for the server

TCP TCP

UDP UDP

3. Traffic/Link utilization from the WAN to the server

You might also like