// BFS(int s) traverses vertices reachable from s.
import java.io.*;
import java.util.*;
// This class represents a directed graph using adjacency list
// representation
class Graph
{
private int V; // No. of vertices
private LinkedList adj[]; //Adjacency Lists
// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i queue = new LinkedList();
// Mark the current node as visited and enqueue it
visited[s]=true;
queue.add(s);
while (queue.size() != 0)
{
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print(s+" ");
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
Iterator i = adj[s].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}
// Driver method to
public static void main(String args[])
{
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Following is Breadth First Traversal "+
"(starting from vertex 2)");
g.BFS(2);
}
}
Java_Networking programs
Wednesday, December 16, 2015
Efficient BFS Algorithm in Java .
Tuesday, April 29, 2014
FTP Client and Server in java
//FTPServer.java
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)
{
}
}
public void listFiles(){
try{
File directory = new File(".");
//get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList){
if (file.isFile()){
dout.writeUTF(file.getName());
}
}
}
catch(Exception e){}
}
void SendFile() throws Exception
{ this.listFiles();
dout.writeUTF("exit");
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(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)
{
}
}
}
}
//FTPClient.java
import java.net.*;
import java.io.*;
import java.util.*;
class FTPClient
{
public static void main(String args[]) throws Exception
{
Socket soc=new Socket("localhost",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();
File f=new File(filename);
if(!f.exists())
{
System.out.println("File not Exists...");
dout.writeUTF("File not found");
return;
}
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(f);
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
{ while(true)
{
String ms=din.readUTF();
if(ms.equals("exit"))
break;
System.out.println(ms);}
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
{
while(true)
{
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 :");
int choice;
choice=Integer.parseInt(br.readLine());
if(choice==1)
{
dout.writeUTF("SEND");
SendFile();
}
else if(choice==2)
{
dout.writeUTF("GET");
ReceiveFile();
}
else
{
dout.writeUTF("DISCONNECT");
System.exit(1);
}
}
}
}
Port Scanner In Java
//FileName: PortScanner.java
//Provide the search range while executing the program !
//for example c:/>java PortScanner 5 100
import java.net.*;
public class PortScanner
{
public static void main(String args[])
{
int startPortRange=0;
int stopPortRange=0;
startPortRange = Integer.parseInt(args[0]);
stopPortRange = Integer.parseInt(args[1]);
for(int i=startPortRange; i <=stopPortRange; i++)
{
try
{
Socket ServerSok = new Socket("127.0.0.1",i);
System.out.println("Port which is in use: " + i );
ServerSok.close();
}
catch (Exception e)
{
//System.out.println("Port which is not in use: " + i );
}
}
}
}
Tuesday, March 4, 2014
Simple program to chat with two computers !
Server.java :
import java.net.*;
import java.io.*;
import java.util.*;
class hh extends Thread
{
Socket s;
hh(Socket ss)
{
s=ss;
}
public void run()
{
try
{
DataInputStream din= new DataInputStream(s.getInputStream());
String msg;
msg=din.readUTF();
System.out.println(msg);
}
catch(Exception e)
{
}
}
}
class server
{
public static void main(String args[])
{try
{
ServerSocket s1=new ServerSocket(1800);
Socket s=s1.accept();
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// dos.writeUTF("Hello this is server!!");
String msg;
hh obj=new hh(s);
Thread th =new Thread(obj);
th.start();
System.out.println("Enter your name");
String name=br.readLine();
while(true)
{
msg=br.readLine();
dos.writeUTF(name+" : "+ msg);
}
}
catch(Exception e)
{
System.out.println("OOPS !!");
}
}
}
Here is the client side code !
client.java
import java.net.*;
import java.io.*;
import java.util.*;
class hh extends Thread
{
Socket s;
hh(Socket ss)
{
s=ss;
}
public void run()
{
try
{
DataInputStream din= new DataInputStream(s.getInputStream());
String msg;
while(true)
{
msg=din.readUTF();
System.out.println(msg);
}
}
catch(Exception e)
{
}
}
}
class client
{
public static void main(String args[])
{
try
{
Socket s1=new Socket("localhost",1800);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos=new DataOutputStream(s1.getOutputStream());
// dos.writeUTF("Hi this is client!!");
String msg;
hh obj=new hh(s1);
Thread th =new Thread(obj);
obj.start();
System.out.println("Enter your name");
String name=br.readLine();
while(true)
{
msg=br.readLine();
dos.writeUTF(name+" : "+msg);
}
}
catch(Exception e)
{
System.out.println("OOPS Client is OFF !!");
}
}
}