Tuesday, July 28, 2009

NP6

TFTP Server

import java.io.*;
import java.net.*;
public class TFTPserver extends Thread
{


static DatagramSocket ds;
static DatagramPacket inPacket,outPacket;
static byte[] buffer;
static int id=0;
static int port=2556;
public static void main(String args[]) throws IOException
{
try{
ds=new DatagramSocket(69);
}
catch(SocketException se){System.out.println("Unable to connect");
}
while(true)
{
buffer=new byte[512];
inPacket=new DatagramPacket(buffer,buffer.length);
ds.receive(inPacket);
InetAddress clientAddress=inPacket.getAddress();
int clientPort=inPacket.getPort();
System.out.println("Clientport:"+clientPort);
String messageIn=new String(inPacket.getData(),0,inPacket.getLength());
id++;
new ClientThread(clientAddress,clientPort,messageIn,id).start();
}
}
}
class ClientThread extends TFTPserver
{
int port,clientid;
DatagramSocket dse;
DatagramPacket dpe,dpk;
InetAddress ca;
File f;
String filename;
ClientThread(InetAddress x,int y,String z,int w)
{
ca=x;
try{
dse=new DatagramSocket(++(TFTPserver.port));}
catch(SocketException see)
{}
filename=z;
port=y;
clientid=w;
}
public void run()
{
while(true)
{
System.out.println("filename received"+filename);
f=new File(filename);
try
{
if(f.exists())
{
BufferedReader br=new BufferedReader(new FileReader(filename));
String str;
while((str=br.readLine())!=null)
{
System.out.println("Sending file data"+str);
dpe=new DatagramPacket(str.getBytes(),str.length(),ca,port);
dse.send(dpe);
}
str="sent";
dpe=new DatagramPacket(str.getBytes(),str.length(),ca,port);
dse.send(dpe);
}
}
catch(Exception e){}
try
{
byte[] buffer=new byte[512];
dpk=new DatagramPacket(buffer,buffer.length);
dse.receive(dpk);
filename=new String(dpk.getData(),0,dpk.getLength());
}
catch(IOException me){}
}
}
}
=================
TFTP Client
import java.io.*;
import java.net.*;
import java.util.*;

public class TFTPClient
{
static InetAddress host;
static DatagramSocket dss;
static DatagramPacket outPacket,inPacket;
static byte[] buffer;
static int port=69;
public static void main(String args[])
{
try
{
host=InetAddress.getByName("localhost");
}
catch(UnknownHostException uhe)
{
System.out.println("Host Not found");
}
try
{
dss=new DatagramSocket();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter file name should be received from server");
String str1,finput;
BufferedWriter bw;
while(!(str1=br.readLine()).equals("null"))
{
System.out.println("haiiiiiiii");
outPacket=new DatagramPacket(str1.getBytes(),str1.length(),host,port);
System.out.println("haiiiiiiiii");
dss.send(outPacket);
bw=new BufferedWriter(new FileWriter("kk.txt"));
System.out.println("Created fileinput stream");
do
{
System.out.println("Receiving file data");
buffer=new byte[512];
inPacket=new DatagramPacket(buffer,buffer.length);
dss.receive(inPacket);
System.out.println("Server Port="+inPacket.getPort());
finput=new String(inPacket.getData(),0,inPacket.getLength());
if(!(finput.equals("sent")))bw.write(finput,0,finput.length());
bw.newLine();
bw.flush();
}
while(!finput.equals("sent"));
port=inPacket.getPort();
System.out.println(port);
}
}
catch(Exception ie)
{}
}
}

Friday, July 24, 2009

NP5

FTP SERVER

import java.io.*;
import java.net.*;
public class fs
{
public static void main(String args[]) throws Exception
{
try
{
ServerSocket ss=new ServerSocket(1334);
Socket s=ss.accept();
DataInputStream in=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
String str;
int x=0;
str=in.readUTF();
File f=new File(str);
if(f.exists())
{ out.write(x);
FileInputStream fin=new FileInputStream(str);
int i;
do
{
i=fin.read();
out.write(i);
}
while(i!=-1);
System.out.println("File transfered Successfully...");
}
else
System.out.println("File not found...");
}
catch(Exception e){}
}
}
FTP CLIENT

import java.io.*;
import java.net.*;
public class fc
{
public static void main(String args[])
{
int i=0,x=1; String str="null";
try
{
Socket s=new Socket("192.168.0.18",1334);


DataInputStream in=new DataInputStream(s.getInputStream());

DataOutputStream out=new DataOutputStream(s.getOutputStream());

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("enter file");

str=br.readLine();

out.writeUTF(str);

x=in.read();

if(x==0)

{

FileOutputStream fout=new FileOutputStream(str);

i=0;
while(i!=-1)
{
i=in.read();
fout.write(i);

}

}
}

catch(Exception e){}


if(x!=0) System.out.println("File not found...");
else System.out.println("File File received successfully");
}





}

Monday, July 20, 2009

update

while(true)
{
String msg1;
msg1=stdin.readLine();
System.out.println("server:"+msg1);
if(msg1!=null)
{
for(int i=0;i<
v.size();i++)
((PrintWriter)v.elementAt(i)).println("server"+":"+msg1);
}
}
}

Thursday, July 16, 2009

NP4

one to many chat



mserver.java

import java.io.*;

import java.net.*;

import java.util.*;

class mserver extends Thread

{

static ServerSocket ss;

static Socket cs=null;

static Vector v=new Vector(100);

static PrintWriter pw;

static int id=1;

public static void main(String args[])

{

try

{

ss=new ServerSocket(4000);

InputScan1 is=new InputScan1();

is.start();

while(true)

{

cs=ss.accept();

new ClientThread(cs,id).start();

pw=new PrintWriter(cs.getOutputStream(),true);

v.addElement(pw);

System.out.println("connected to client"+id);

id++;

}

}

catch(Exception e)

{}

}

}

class ClientThread extends mserver

{

Socket cs;

int id;

ClientThread(Socket clientsocket,int i)

{

cs=clientsocket;

id=i;

}

public void run()

{

try

{

BufferedReader in=new BufferedReader(new InputStreamReader(cs.getInputStream()));

String msg=null;

while(true)

{

msg=in.readLine();

System.out.println("client"+id+":"+msg);

if(msg!=null)

{

for(int i=0;i
((PrintWriter)v.elementAt(i)).println("client"+id+":"+msg);

}

}

}

catch(Exception e)

{}

}

}

class InputScan1 extends mserver

{

DataInputStream stdin=new DataInputStream(System.in);

public void run()

{

try

{

while(true)

{

String msg1;

msg1=stdin.readLine();

System.out.println("server:"+msg1);

if(msg1!=null)

{

for(int i=0;i
((PrintWriter)v.elementAt(i)).println("server"+":"+msg1);

}

}

}

catch(Exception e)

{ }

}

}





mclient.java



import java.io.*;

import java.net.*;

public class mclient extends Thread

{

static Socket cs=null;

public static void main(String args[])

{

try

{

cs=new Socket("localhost",4000);

System.out.println("connected to server");

BufferedReader in=new BufferedReader(new InputStreamReader(cs.getInputStream()));

InputScan is=new InputScan();

is.start();

while(true)

{

String msg;

msg=in.readLine();

System.out.println(msg);

}

}

catch(Exception e) { }

}

}



class InputScan extends mclient

{

DataInputStream stdin=new DataInputStream(System.in);

public void run()

{

try

{

PrintWriter out=new PrintWriter(cs.getOutputStream(),true);

while(true)

{

String msg;

msg=stdin.readLine();

out.println(msg);

}

}

catch(Exception e){}

}

}

Friday, July 10, 2009

NP3

UPD
import java.io.*;
import java.net.*;
import java.util.*;
public class UDPEchoClient
{
private static InetAddress host;
private static final int PORT=1234;
private static DatagramSocket datagramSocket;
private static DatagramPacket inPacket,outPacket;
private static byte[] buffer;
public static void main(String[] args)
{
try{
host=InetAddress.getLocalHost();
}
catch(UnknownHostException u)
{
System.out.println("Host ID not found");
System.exit(1);
}
accessServer();
}
private static void accessServer()
{try{
datagramSocket=new DatagramSocket();
DataInputStream userEntry=new DataInputStream(System.in);
String message="",response="";
do{
System.out.println("Enter Message");
message=userEntry.readLine();
if(!message.equals("CLOSE"))
{
outPacket=new DatagramPacket(message.getBytes(),message.length(),host,PORT);
datagramSocket.send(outPacket);
buffer=new byte[256];
inPacket=new DatagramPacket(buffer,buffer.length);
datagramSocket.receive(inPacket);
response=new String(inPacket.getData(),0,inPacket.getLength());
System.out.println("\n"+response);
}
}while(!message.equals("CLOSE"));
}
catch(IOException ex)
{
ex.printStackTrace();
}
finally{
System.out.println("\nClosing Connection");
datagramSocket.close();
}
}
}

=================
import java.io.*;
import java.net.*;
public class UDPEchoServer
{
private static final int PORT=1234;
private static DatagramSocket datagramSocket;
private static DatagramPacket inPacket,outPacket;
private static byte[] buffer;
public static void main(String[] args)
{
System.out.println("Opening Port...\n");
try{
datagramSocket=new DatagramSocket(PORT);
}
catch(SocketException sockEx)
{
System.out.println("Unable to attach to port");
System.exit(1);
}
handleClient();
}
private static void handleClient()
{
try{
String messageIn,messageOut;
int numMessages=0;
do{
buffer =new byte[256];
inPacket=new DatagramPacket(buffer,buffer.length);
datagramSocket.receive(inPacket);
InetAddress clientAddress=inPacket.getAddress();
int clientPort=inPacket.getPort();
messageIn=new String(inPacket.getData(),0,inPacket.getLength());
System.out.println("Message Receives");
numMessages++;
messageOut="Message"+numMessages+":"+messageIn;
outPacket=new DatagramPacket(messageOut.getBytes(),messageOut.length(),clientAddress,clientPort);
datagramSocket.send(outPacket);
}while(true);
}
catch(IOException e)
{e.printStackTrace();
}
finally
{
System.out.println("\nClosing Connection..\n");
datagramSocket.close();
}
}
}

Friday, July 3, 2009

NP2

One to One Server
import java.io.*;
import java.net.*;
public class server1
{
public static void main(String args[])
{
try
{
ServerSocket ss=new ServerSocket(2020);
Socket s=ss.accept();
DataInputStream in=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
while(true)
{
str=in.readUTF();
System.out.println(str);
str=br.readLine();
out.writeUTF(str);
}
}
catch(Exception e){}
}
}

One to One Client
import java.io.*;
import java.net.*;
public class client1
{
public static void main(String args[])
{
try
{
Socket s=new Socket("localhost",2020);
DataInputStream in=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
while(true)
{
str=br.readLine();
out.writeUTF(str);
str=in.readUTF();
System.out.println(str);
}
}
catch(Exception e){}
}
}