0% found this document useful (0 votes)
56 views24 pages

Java File Download and Chat Server Code

The document contains multiple Java programs demonstrating various networking concepts, including file downloading, client-server communication, DNS resolution, and ARP requests. Each program is accompanied by its output, showcasing how data is transmitted and received over sockets and datagrams. Additionally, there is a Python program for packet capturing using Scapy.

Uploaded by

balachatgpt3.0
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)
56 views24 pages

Java File Download and Chat Server Code

The document contains multiple Java programs demonstrating various networking concepts, including file downloading, client-server communication, DNS resolution, and ARP requests. Each program is accompanied by its output, showcasing how data is transmitted and received over sockets and datagrams. Additionally, there is a Python program for packet capturing using Scapy.

Uploaded by

balachatgpt3.0
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 :

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;

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

[Link]("Buffer Read of length: " + length);

[Link](buffer, 0, length);

[Link]();

[Link]();

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

}}}

OUTPUT :

D:\XEON\Lab>javac [Link]

D:\XEON\Lab>java Download

Downloading File From:


[Link]

Buffer Read of length: 2048

Buffer Read of length: 2048

Buffer Read of length: 2048

Buffer Read of length: 2048

Buffer Read of length: 2048

Buffer Read of length: 1097

Buffer Read of length: 2048

Buffer Read of length: 2047

Buffer Read of length: 2048

The downloaded file (Stored in the current working directory)


Program :

[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);

while (true) {

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]());

} finally {

try {

if (sersock != null) {

[Link]();

} catch (IOException ie) {

[Link]("Close Error: " + [Link]());

[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 {

if (sock != null) {

[Link]();

} catch (IOException e) {
[Link]("Close Error: " + [Link]());

OUTPUT :

In server window:

D:\XEON\Lab>javac [Link]

D:\XEON\Lab>java Server

Server Started
:ServerSocket[addr=[Link]/[Link],port=0,localport=4000

] Client Connected
:Socket[addr=/[Link],port=1815,localport=4000]

Hi from client

Connection from : /[Link]

In client window:

D:\XEON\Lab>javac [Link]

D:\XEON\Lab>java client

Trying to connect

Hello from server


PROGRAM :

[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]("\nServer started\n");

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;

else {

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

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

SrvMsg = [Link]();

[Link](SrvMsg);

[Link]("\nClient 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]("Client: " + SrvMsg);

[Link]();

} catch (Exception E) {

[Link]([Link]());

OUTPUT :

In server window:

D:\XEON\Lab>javac [Link]

D:\XEON\Lab>java tcpchatserver

Server started

Client connected

Server : hai

Message to Client : hello

Client Disconnected

In client window:

D:\XEON\Lab>javac [Link]

D:\XEON\Lab>java

tcpchatclient Type "end" to Quit


Message to Server : hai

Client : hello

Message to Server : end


PROGRAM :

[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;

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](), 0,


[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");

DatagramSocket serversocket = new DatagramSocket(8080);

while (true) {

byte[] receivedata = new byte[1024];

DatagramPacket recvpack = new DatagramPacket(receivedata,


[Link]);

[Link](recvpack);

String sen = new String([Link](), 0,


[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";


byte[] senddata = [Link]();

DatagramPacket pack = new DatagramPacket(senddata,


[Link], ipaddress, port);

[Link](pack);

OUTPUT :

In server window

D:\XEON\Lab>javac [Link]

D:\XEON\Lab>java dnsserver

Press Ctrl + C to Quit

Request for host [Link]

In client window

D:\XEON\Lab>javac [Link]

D:\XEON\Lab>java dnsclient

Enter the hostname : [Link]

IP Address: [Link]
PROGRAM :

import sys

from [Link] import *

# Define the packet capturing function

def packet_handler(packet):

print([Link]())

# Capture packets on the network interface

sniff(iface='eth0', prn=packet_handler)

OUTPUT :

###[ Ethernet ]###


dst = [Link]
src = [Link]
type = IPv4 ###[ IP ]###
version = 4
ihl = 5
tos = 0x0
len = 1500
id = 52786
flags = DF
frag = 0 ttl = 128
proto= tcp chksum= 0xe877
src = [Link]
dst = [Link]
\options \ ###[ TCP ]###
sport = 54321
dport= http
seq = 3665840854
ack = 2542707921
dataofs = 5
reserved = 0
flags = FA
window= 8760
chksum= 0x9d6c
urgptr= 0
options = [('MSS', 1460), ('SAckOK', ''), ('Timestamp', (12576855,
2413)), ('NOP', None), ('WScale', 7)] ###[ Raw ]###
load = ''
PROGRAM :

[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);

DataInputStream din = new


DataInputStream([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 is listening on port 139...");

while (true) {

Socket obj1 = [Link]();

DataInputStream din = new


DataInputStream([Link]());

DataOutputStream dout = new


DataOutputStream([Link]());

String str = [Link]();

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

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

boolean found = false;

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

if ([Link](ip[i])) {

[Link](mac[i] + '\n');
found = true;

break;

if (!found) {

[Link]("IP Not Found\n");

[Link](); // Close the client socket after response

} catch (Exception e) {

[Link](e);

Output :

D:\XEON\Lab>java Serverarp

D:\XEON\Lab>java Clientarp

Enter the Logical address (IP) : [Link]

The Physical Address is : [Link] AA: C2


PROGRAM :

[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: ");

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]());


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

[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 is running on port 1309...");

while (true) {

byte[] receiveByte = new byte[1204];

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 = {"[Link]"};

boolean found = false;

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

if ([Link](mac[i])) {

byte[] sendByte = ip[i].getBytes();

DatagramPacket sender = new DatagramPacket(sendByte,


[Link], addr, port);

[Link](sender);

found = true;

break;

if (!found) {

String notFoundMessage = "MAC Address Not Found";

byte[] sendByte = [Link]();

DatagramPacket sender = new DatagramPacket(sendByte,


[Link], addr, port);

[Link](sender);

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

You might also like