Java Mini & Major Projects | Simple Java Projects with Source Code Free Downloa | Free Java Projects
Java Mini & Major Projects | Simple Java Projects with Source Code Free Downloa | Free Java Projects
1- UDP Socket Programming (Mini Project)
Client Program
import java.net.*;
import java.io.*;
class MyClientt{
public static void main(String args[])throws Exception
{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop"))
{
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}
}
Server Program
import java.net.*;
import java.io.*;
class MyServerr{
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop"))
{
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
2- "Simulation of nslookup command in java”
import java.net.*;
import javca.util.*;
public class Nslookup
{
public static void main(String[] args) throws Exception{
Scanner s=New Scanner(System.in);
String address,hostName,hostAddress;
InetAddress host,home;
System.out.println("Enter Address::");
address=s.net().trime();
host=InetAddress.getBy Name(address);
home=InetAddress.getLocalHost();
hostAdss=host.getHostAddress();
System.out.println("Server Address:" +home.getHostAddress());
System.out.println(" Name:-" +hostName);
System.out.println(" Address:-" +hostAddress);
}
3- “Simulation
of IP Spoofing”
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.util.Timer;
public class Ipspoof extends JFrame implements ActionListener
{
String ipaddr="10.0.0.13";
String getway="10.0.0.1";
String mask="255.0.0.0";
String dns1="8.8.8.8";
String dns2="8.8.4.4";
JButton button1;
JButton button2;
JLabel lb1;
JLabel lb2;
JLabel lb3;
Label ip;
Label lmask;
Label lget;
JLabel lbdns1;
JLabel lbdns2;
Label ldns1;
Label ldns2;
Thread t1,t2,t3,t4,t5 ;
public Ipspoof()
{
getContentPane().setLayout(null);
setupGUI();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void setupGUI()
{
button1 = new JButton();
button1.setLocation(60,23);
button1.setSize(120,25);
button1.setText("Start Spoofing");
button1.setToolTipText("Start Spoofing");
button1.addActionListener(this);
button1.setActionCommand("Start");
getContentPane().add(button1);
button2 = new JButton();
button2.setLocation(185,23);
button2.setSize(145,25);
button2.setText("Stop Spoofing");
button2.setToolTipText("Stop Spoofing");
button2.addActionListener(this);
button2.setActionCommand("Stop");
getContentPane().add(button2);
lb1 = new JLabel();
lb1.setLocation(30,57);
lb1.setSize(101,25);
lb1.setText("IP");
lb1.setToolTipText("IP");
getContentPane().add(lb1);
lb2 = new JLabel();
lb2.setLocation(30,89);
lb2.setSize(101,25);
lb2.setText("Mask");
getContentPane().add(lb2);
lb3 = new JLabel();
lb3.setLocation(30,119);
lb3.setSize(100,25);
lb3.setText("Getway");
getContentPane().add(lb3);
ip = new Label();
ip.setLocation(142,58);
ip.setSize(189,25);
//ip.setText(getip());
getContentPane().add(ip);
lmask = new Label();
lmask.setLocation(142,89);
lmask.setSize(189,25);
// lmask.setText(getIpMask());
getContentPane().add(lmask);
lget = new Label();
lget.setLocation(142,120);
lget.setSize(189,25);
// lget.setText(getDefaultAddress());
getContentPane().add(lget);
lbdns1 = new JLabel();
lbdns1.setLocation(29,150);
lbdns1.setSize(100,25);
lbdns1.setText("Primary DNS");
getContentPane().add(lbdns1);
lbdns2 = new JLabel();
lbdns2.setLocation(29,181);
lbdns2.setSize(100,25);
lbdns2.setText("Secondary DNS");
getContentPane().add(lbdns2);
ldns1 = new Label();
ldns1.setLocation(142,151);
ldns1.setSize(189,25);
// ldns1.setText("DNS");
getContentPane().add(ldns1);
ldns2 = new Label();
ldns2.setLocation(143,180);
ldns2.setSize(189,25);
// ldns2.setText("DNS");
getContentPane().add(ldns2);
setTitle("Ip Spoofing");
setSize(399,254);
setVisible(true);
setResizable(false);
class refreshAddress extends TimerTask {
public void run() {
getAddress();
}
}Timer timer = new Timer();
timer.schedule(new refreshAddress(), 0, 5000);
}
void spoof()
{
new Thread(new testing("netsh interface ip set address name=\"Local Area Connection\" source=static addr="+ ipaddr +" mask="+mask+" "+ getway));
new Thread(new testing("netsh interface ip set dns name=\"Local Area Connection\" static "+dns1));
new Thread(new testing("netsh interface ip add dns name=\"Local Area Connection\" "+dns2));
}
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
if( command.equals("Start")) {
spoof();
getAddress();
}
if( command.equals("Stop")) {
new Thread(new testing("netsh interface ip set address name=\"Local Area Connection\" source=dhcp"));
new Thread(new testing("netsh interface ip set dns \"Local Area Connection\" source=dhcp"));
getAddress();
}
} public void getAddress() {
String defaultAddress = "";
String dns="";
String mask= "";
try {
String ipd= Inet4Address.getLocalHost().getHostAddress(); // for ip
System.out.println(ipd);
ip.setText(ipd);
//start mask
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); //for mask
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback())
continue;
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast == null )
continue;
String bs=broadcast.toString();
String bs1=bs.replaceAll("/","");
String ips=ipd.substring(0, ipd.indexOf('.'));
if(bs1.startsWith(ips,0))
{
System.out.println(bs.replaceAll("/",""));
lmask.setText(bs.replaceAll("/",""));
}
}
}
Process getway = Runtime.getRuntime().exec("netstat -rn"); //for getway
BufferedReader getwayoutput = new BufferedReader(new InputStreamReader(getway.getInputStream()));
String getwayline = getwayoutput.readLine();
while (getwayline != null) {
if (getwayline.contains("0.0.0.0")) {
StringTokenizer stringTokenizer = new StringTokenizer(getwayline);
stringTokenizer.nextElement(); // first element is 0.0.0.0
stringTokenizer.nextElement(); // second element is 0.0.0.0
defaultAddress = (String) stringTokenizer.nextElement();
System.out.println(defaultAddress);
lget.setText(defaultAddress);
break;
}
getwayline = getwayoutput.readLine();
}
Process dnsresult = Runtime.getRuntime().exec("nslookup localhost"); //for dns
BufferedReader dnsoutput = new BufferedReader(new InputStreamReader(dnsresult.getInputStream()));
String dnsline = dnsoutput.readLine();
while (dnsline != null) {
if (dnsline.contains("Address")) {
StringTokenizer stringTokenizer = new StringTokenizer(dnsline);
stringTokenizer.nextElement(); // first element is 0.0.0.0
dns = (String) stringTokenizer.nextElement();
System.out.println(dns);
ldns1.setText(dns);
break;
}
dnsline = dnsoutput.readLine();
}
} // while
catch (Exception e) {
e.printStackTrace();
}
}
public static void main( String args[] )
{
new Ipspoof(); }
public void initialize()
{
}
class testing implements Runnable
{
String name;
testing(String name)
{
this.name = name;
try
{
System.out.println(this.name+"\n");
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(this.name);
try
{
p.waitFor();
}
catch(Exception e)
{
e.printStackTrace();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
}
}
}
4- FILE TRANSFER USING FTP PROTOCOL
FTPClient
import java.io.*;
import java.net.*;
class FTPClient {
public static void main(String args[]) throws Exception {
Socket soc = new Socket("127.0.0.1", 5217);
transferfileClient t = new transferfileClient(soc);
t.displayMenu();
}
}
class transferfileClient {
Socket ClientSoc;
DataInputStream din;
DataOutputStream dout;
BufferedReader br;
transferfileClient(Socket soc) {
try {
ClientSoc = soc;
din = new DataInputStream(ClientSoc.getInputStream());
dout = new DataOutputStream(ClientSoc.getOutputStream());
br = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception ex) {
}
}
void SendFile() throws Exception {
String filename;
System.out.print("Enter File Name :");
filename = br.readLine();
dout.writeUTF(filename);
String msgFromServer = din.readUTF();
if (msgFromServer.compareTo("File Already Exists") == 0) {
String Option;
System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");
Option = br.readLine();
if (Option == "Y") {
dout.writeUTF("Y");
} else {
dout.writeUTF("N");
return;
}
}
System.out.println("Sending File ...");
FileInputStream fin = new FileInputStream(filename);
int ch;
do {
ch = fin.read();
dout.writeUTF(String.valueOf(ch));
} while (ch != -1);
fin.close();
System.out.println(din.readUTF());
}
void ReceiveFile() throws Exception {
String fileName;
System.out.print("Enter File Name :");
fileName = br.readLine();
dout.writeUTF(fileName);
String msgFromServer = din.readUTF();
if (msgFromServer.compareTo("File Not Found") == 0) {
System.out.println("File not found on Server ...");
return;
} else if (msgFromServer.compareTo("READY") == 0) {
System.out.println("Receiving File ...");
File f = new File(fileName);
if (f.exists()) {
String Option;
System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");
Option = br.readLine();
if (Option == "N") {
dout.flush();
return;
}
}
FileOutputStream fout = new FileOutputStream(f);
int ch;
String temp;
do {
temp = din.readUTF();
ch = Integer.parseInt(temp);
if (ch != -1) {
fout.write(ch);
}
} while (ch != -1);
fout.close();
System.out.println(din.readUTF());
}
}
public void displayMenu() throws Exception {
int choice;
do {
System.out.println("[ MENU ]");
System.out.println("1. Send File");
System.out.println("2. Receive File");
System.out.println("3. Exit");
System.out.print("\nEnter Choice :");
choice = Integer.parseInt(br.readLine());
switch (choice) {
case 1:
dout.writeUTF("SEND");
SendFile();
break;
case 2:
dout.writeUTF("GET");
ReceiveFile();
break;
case 3:
dout.writeUTF("DISCONNECT");
System.exit(1);
}
}while(choice != 3);
}
}
FTPServer
import java.net.*;
import java.io.*;
import java.util.*;
public class FTPServer {
public static void main(String args[]) throws Exception {
ServerSocket soc = new ServerSocket(5217);
System.out.println("FTP Server Started on Port Number 5217");
while (true) {
System.out.println("Waiting for Connection ...");
transferfile t = new transferfile(soc.accept());
}
}
}
class transferfile extends Thread {
Socket ClientSoc;
DataInputStream din;
DataOutputStream dout;
transferfile(Socket soc) {
try {
ClientSoc = soc;
din = new DataInputStream(ClientSoc.getInputStream());
dout = new DataOutputStream(ClientSoc.getOutputStream());
System.out.println("FTP Client Connected ...");
start();
} catch (Exception ex) {
}
}
void SendFile() throws Exception {
String filename = din.readUTF();
File f = new File(filename);
if (!f.exists()) {
dout.writeUTF("File Not Found");
return;
} else {
dout.writeUTF("READY");
FileInputStream fin = new FileInputStream(f);
int ch;
do {
ch = fin.read();
dout.writeUTF(String.valueOf(ch));
} while (ch != -1);
fin.close();
dout.writeUTF("File Receive Successfully");
}
}
void ReceiveFile() throws Exception {
String filename = din.readUTF();
if (filename.compareTo("File not found") == 0) {
return;
}
File f = new File("TEST" + filename );
String option;
if (f.exists()) {
dout.writeUTF("File Already Exists");
option = din.readUTF();
} else {
dout.writeUTF("SendFile");
option = "Y";
}
if (option.compareTo("Y") == 0) {
FileOutputStream fout = new FileOutputStream(f);
int ch;
String temp;
do {
temp = din.readUTF();
ch = Integer.parseInt(temp);
if (ch != -1) {
fout.write(ch);
}
} while (ch != -1);
fout.close();
dout.writeUTF("File Send Successfully");
} else {
return;
}
}
public void run() {
while (true) {
try {
System.out.println("Waiting for Command ...");
String Command = din.readUTF();
if (Command.compareTo("GET") == 0) {
System.out.println("\tGET Command Received ...");
SendFile();
continue;
} else if (Command.compareTo("SEND") == 0) {
System.out.println("\tSEND Command Receiced ...");
ReceiveFile();
continue;
} else if (Command.compareTo("DISCONNECT") == 0) {
System.out.println("\tDisconnect Command Received ...");
System.exit(1);
}
} catch (Exception ex) {
}
}
}
}
5- “Password
Authentication Protocol
(PAP) Using OTP”
SERVER
import java.util.*;
import java.io.*;
import java.net.*;
class TimeOutTask extends TimerTask {
boolean isTimedOut = false;
public void run() {
isTimedOut = true;
}
}
class OTPServer {
public static void main(String args[]) throws IOException {
ServerSocket serverSocket = new ServerSocket(7777);
System.out.println("Server running and waiting for client...");
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Server waits for a client to send its user ID
String id = in.readLine();
// Server generates an OTP and waits for client to send this
Random r = new Random();
String otp = new String();
for(int i=0 ; i < 8 ; i++) {
otp += r.nextInt(10);
}
System.out.println(otp);
// Server starts a timer of 10 seconds during which the OTP is valid.
TimeOutTask task = new TimeOutTask();
Timer t = new Timer();
t.schedule(task, 100000L);
// Server listens for client to send its ID and OTP to check if it is
// valid
String newId = in.readLine();
String newOtp = in.readLine();
if(newId.equals(id)) {
// User ID is verified
if(task.isTimedOut) {
// User took more than 100 seconds and hence the OTP is invalid
out.println("Time out!");
} else if(!newOtp.equals(otp)) {
out.println("Incorrect OTP!");
} else {
out.println("Logged In!");
}
}
System.exit(0);
}
}
PROGRAM FOR CLIENT
import java.util.*;
import java.io.*;
import java.net.*;
class OTPClient {
public static void main(String args[]) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("Connecting to the server...");
Socket clientSocket = new Socket("localhost", 7777);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Client enters ID. This will be used by the program for verifying who
// is communicating as well as check the OTP against the ID, on the
// server side
System.out.println("Enter your ID:");
String id = scan.nextLine();
System.out.println("Contacting server...");
out.println(id);
System.out.println("Server has sent the OTP. Please enter it here:");
String otp = scan.nextLine();
System.out.println("Verifying...");
out.println(id);
out.println(otp);
System.out.println(in.readLine());
in.close();
out.close();
clientSocket.close();
}
}