You are on page 1of 33

FILE TRANSFER USING TCP/IP /*SERVER*/

import java.io.*; import java.net.*; public class FileServer { public static void main(String rgs[]) throws IOException { ServerSocket ss=new ServerSocket(8080); System.out.println("Server started"); Socket s=ss.accept(); FileOutputStream fos=new FileOutputStream("f1.txt"); DataInputStream dis=new DataInputStream(s.getInputStream()); int b; try { while((b=dis.read())!=-1) fos.write(b); fos.close(); } catch (Exception c) { System.out.println("Server down now......"); } } }

/*CLIENT*/
import java.io.*; import java.net.*; public class FileClient { public static void main(String args[])throws IOException { DataInputStream dis=new DataInputStream(System.in); String n=dis.readLine(); FileInputStream fis=new FileInputStream(n); Socket s=new Socket(InetAddress.getLocalHost(),8080); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); byte b[]=new byte[500]; while(fis.read(b)!=-1) dos.write(b); System.out.println("File Transferred successfully"); } }

OUTPUT : /*SERVER*/
C:\jdk1.3\bin>javac FileServer.java C:\jdk1.3\bin>java FileServer Server started Server down now......

/*CLIENT*/
C:\jdk1.3\bin>javac FileClient.java C:\jdk1.3\bin>java FileClient b1.txt File Transferred successfully

FILE TRANSFER USING UDP /*SERVER*/


import java.io.*; import java.net.*; public class ServerUdp { public static void main(String args[])throws IOException { DatagramSocket ds=new DatagramSocket(8080); System.out.println("SERVER STARTED"); byte b[]=new byte[100]; DatagramPacket dp=new DatagramPacket(b,b.length); FileOutputStream fos=new FileOutputStream("f1.txt"); while(true) { ds.receive(dp); fos.write(b); break; } } }

/*CLIENT*/
import java.io.*; import java.net.*; public class ClientUdp { public static void main(String args[])throws IOException { DataInputStream dis=new DataInputStream(System.in); byte b[]=new byte[400]; DatagramSocket ds=new DatagramSocket(8081); FileInputStream fis=new FileInputStream(dis.readLine()); int r,i=0; while((r=fis.read())!=-1) { b[i]=(byte)r; i++; } DatagramPacket dp=newDatagramPacket (b,b.length,InetAddress.getLocalHost(),8080); ds.send(dp); System.out.println("FILE TRANSFERRED SUCCESSFULLY"); } }

OUTPUT : /*SERVER*/
C:\jdk1.3\bin>javac ServerUdp.java C:\jdk1.3\bin>java ServerUdp Server Started

/*CLIENT*/
C:\jdk1.3\bin>javac ClientUdp.java C:\jdk1.3\bin>java ClientUdp f1.txt File Transferred Successfully

ECHO COMMAND IMPLEMENTATION USING TCP /*SERVER*/


import java.net.*; import java.io.*; public class echoserver { public static void main(String args[])throws IOException { try { ServerSocket Sock=new ServerSocket(8080); while(true) { System.out.println("Server Started"); Socket Client=Sock.accept(); ServiceConnection (Client); } } catch(IOException e) { System.err.println(e); } } public static void ServiceConnection(Socket client) { BufferedReader in=null; OutputStreamWriter Out=null;

try { in=new BufferedReader(new InputStreamReader(client.getInputStream())); Out=new OutputStreamWriter(client.getOutputStream()); while(true) { String line=in.readLine(); if((line==null)||line.equals("bye")) { break; } System.out.println(line); Out.write("server["+line+"]\r\n"); Out.flush(); } } catch(IOException e) { System.err.println(e); } } }

/*CLIENT*/
import java.net.*; import java.io.*; public class echoclient { public static void main(String args[])throws IOException { BufferedReader in=null; PrintWriter Out=null; BufferedReader FromUser=null; try { Socket Sock=new Socket("localhost",8080); in=new BufferedReader(new InputStreamReader(Sock.getInputStream())); FromUser=new BufferedReader(new InputStreamReader(System.in)); Out=new PrintWriter(Sock.getOutputStream(),true); while(true) { String line=FromUser.readLine(); if((line==null||line.equals("bye"))) { Out.println("bye"); break; } Out.println(line); System.out.println(in.readLine()); } } catch(IOException e) { System.err.println(e); } } }

OUTPUT: /*SERVER*/
C:\jdk1.3\bin>javac echoserver.java C:\jdk1.3\bin>java echoserver Server Started hai how are you Server Started

/*CLIENT*/
C:\jdk1.3\bin>javac echoclient.java C:\jdk1.3\bin>java echoclient hai server[hai] how are you server[how are you] bye

PING COMMAND IMPLEMENTATION USING TCP /*SERVER*/


import java.io.*; import java.net.*; class pingserver { public static void main(String args[])throws IOException { String msg; ServerSocket server=new ServerSocket(1000); int localport=server.getLocalPort(); while(true) { System.out.println("server listening on port:"+localport); Socket client=server.accept(); String cname=client.getInetAddress().getHostAddress(); System.out.println("connection from:"+cname); BufferedReader r=new BufferedReader (new InputStreamReader(client.getInputStream())); PrintWriter w=new PrintWriter(client.getOutputStream(),true); msg=r.readLine(); while(true) { if(msg.equalsIgnoreCase("BYE")) break; w.println(msg); msg=r.readLine(); } } } }

/*CLIENT*/
import java.io.*; import java.net.*; class pingclient { public static void main(String args[])throws IOException { String msg="hai"; Socket client=new Socket("127.0.0.1",1000); PrintWriter w=new PrintWriter(client.getOutputStream(),true); BufferedReader sr=new BufferedReader (new InputStreamReader(client.getInputStream())); BufferedReader r=new BufferedReader(new InputStreamReader(System.in)); for(int i=0;i<4;i++) { w.println(msg); msg=sr.readLine(); if(msg.equalsIgnoreCase("hai")) System.out.println("Reply from Server"); } w.println("BYE"); r.close(); w.close(); sr.close(); client.close(); } }

OUTPUT /*SERVER*/
C:\jdk1.3\bin>javac pingserver.java C:\jdk1.3\bin>java pingserver server listening on port:1000 connection from:127.0.0.1 server listening on port:1000

/*CLIENT*/
. C:\jdk1.3\bin>javac pingclient.java C:\jdk1.3\bin>java pingclient Reply from Server Reply from Server Reply from Server Reply from Server

REMOTE COMMAND EXECUTION /*SERVER*/


import java.net.*; import java.io.*; class remserver { public static void main(String args[])throws IOException { ServerSocket ss=new ServerSocket(8081); Socket s=ss.accept(); String cmd; BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); cmd=in.readLine(); try { Runtime r=Runtime.getRuntime(); Process a=r.exec(cmd); System.out.println("Exception Command:"+cmd); } catch(Exception e) { System.out.println("error"+e); } s.close(); } }

/*CLIENT*/
import java .net.*; import java.io.*; public class remclient { public static void main(String args[])throws IOException { try { Socket s=new Socket("127.0.0.1",8081); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); PrintWriter Out=new PrintWriter(s.getOutputStream(),true); String cmd; System.out.println("Enter the command to execute on the server"); cmd=in.readLine(); Out.println(cmd); } catch(Exception e) { System.out.println(e); } } }

OUTPUT /*SERVER*/
C:\jdk1.3\bin>javac remserver.java C:\jdk1.3\bin>java remserver Exception Command:calc

/*CLIENT*/
C:\jdk1.3\bin>javac remclient.java C:\jdk1.3\bin>java remclient Enter the command to execute on the server calc

ADDRESS RESOLUTION PROTOCOL /*SERVER*/


import java.io.*; import java.net.*; class arpserver { public static void main(String args[])throws IOException { String bytes[]; String a[]=new String[100]; String b1[]=new String[100]; a[0]="172.16.42.1"; a[1]="172.16.42.2"; a[2]="172.16.42.3"; b1[0]="00-4g-56-61-w1-32"; b1[1]="00-4g-57-71-h4-84"; b1[2]="00-06-53-0d-d0-06"; DatagramSocket ds=new DatagramSocket(3001); byte b[]=new byte[1000]; DatagramPacket dp=new DatagramPacket(b,0,b.length); ds.receive(dp); String data=new String(b,0,dp.getLength()); System.out.println("Ip.Address received"+data); int flag=0; for(int i=0;i<3;i++) {

if(data.equals(a[i])) { System.out.println("CORRESPONDING MACADDRESS: "+b1[i]); b=b1[i].getBytes(); DatagramPacket dout=new DatagramPacket(b,0,b.length,InetAddress.getLocalHost(),dp.getPort()); ds.send(dout); i=5; flag=1; } } if(flag==0) { String temp="not found"; b=temp.getBytes(); DatagramPacket dout1=new DatagramPacket(b,0,b.length,InetAddress.getLocalHost(),dp.getPort()); ds.send(dout1); } ds.close(); } }

/*CLIENT*/
import java.io.*; import java.net.*; class ARPC { public static void main(String args[])throws IOException { DatagramSocket ds=new DatagramSocket(); byte b[]=new byte[1000]; byte b1[]=new byte[1000]; DataInputStream din=new DataInputStream(System.in); System.out.println("ENTER THE IPADDRESS FOR MACADDRESS"); String data=din.readLine(); b=data.getBytes(); DatagramPacket dp=new DatagramPacket(b,0,b.length,InetAddress.getLocalHost(),3001); ds.send(dp); DatagramPacket din1=new DatagramPacket(b1,0,b1.length); ds.receive(din1); String data1=new String(b1,0,din1.getLength()); System.out.println("MACAddres you Requested:"+data1); } }

OUTPUT /*SERVER*/
C:\jdk1.3\bin>javac arpserver.java C:\jdk1.3\bin>java arpserver Ip.Address received172.16.42.1 CORRESPONDING MACADDRESS: 00-4g-56-61-w1-32

/*CLIENT*/

C:\jdk1.3\bin>javac ARPC.java C:\jdk1.3\bin>java ARPC ENTER THE IPADDRESS FOR MACADDRESS 172.16.42.1 MACAddres you Requested:00-4g-56-61-w1-32

REVERSE ADDRESS RESOLUTION PROTOCOL /*SERVER*/


import java.io.*; import java.net.*; class pp { public static void main(String args[]) throws IOException { String byte1; String a[]=new String[100]; String b1[]=new String[100]; a[0]="00-49-56-61-w1-84"; a[1]="00-49-57-71-h4-84"; a[2]="00-06-53-0d-d0-06"; b1[0]="172.16.42.1"; b1[1]="172.16.42.2"; b1[2]="172.16.42.3"; DatagramSocket ds=new DatagramSocket(3000); byte b[]=new byte[1000]; DatagramPacket dp=new DatagramPacket(b,0,b.length); ds.receive(dp); String data=new String(b,0,dp.getLength()); System.out.println("MAC address receive"+data); int flag=0; for(int i=0;i<3;i++) {

if(data.equals(a[i])) { System.out.println("Corresponding IP Address"+b1[1]); b=b1[i].getBytes(); DatagramPacket dout=new DatagramPacket(b,0,b.length,InetAddress.getLocalHost(),dp.getPort()); ds.send(dout); i=5; flag=1; } if(flag==0) { String temp="not found"; b=temp.getBytes(); DatagramPacket dout1=new DatagramPacket(b,0,b.length,InetAddress.getLocalHost(),dp.getPort()); ds.send(dout1); } ds.close(); } } }

/*CLIENT*/
import java.io.*; import java.net.*; class Rarpcl { public static void main(String args[]) throws IOException { DatagramSocket ds=new DatagramSocket(); byte b[]=new byte[1000]; byte b1[]=new byte[1000]; DataInputStream din=new DataInputStream(System.in); System.out.println("Enter the MAC Address for IPAddress"); String data=din.readLine(); b=data.getBytes(); DatagramPacket dp=new DatagramPacket(b,0,b.length,InetAddress.getLocalHost(),3000); ds.send(dp); DatagramPacket din1=new DatagramPacket(b1,0,b1.length); ds.receive(din1); String data1=new String(b1,0,din1.getLength()); System.out.println("IPAddress you requested:"+data1); } }

OUTPUT /*SERVER*/
C:\jdk1.3\bin>javac Rarpsr.java C:\jdk1.3\bin>java Rarpsr MAC address receive00-49-56-61-w1-84 Corresponding IP Address172.16.42.2

/*CLIENT*/
C:\jdk1.3\bin>javac Rarpcl.java Note: Rarpcl.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details. C:\jdk1.3\bin>java Rarpcl Enter the MAC Address for IPAddress 00-49-56-61-w1-84 IPAddress you requested:172.16.42.1

REMOTE METHOD INVOCATION /*REMOTE INTERFACE*/


import java.rmi.*; public interface fibo extend remote { public int[](int n)throws remote Execption }

/*IMPLEMENTATION*/
import java.rmi.*; import java.rmi.server.*; public class fiboimp extends UnicastRemoteObject implements fibo { public fiboimp()throws RemoteException { super(); } public int[] data(int n)throws RemoteException { int i,x=0,y=1,z; int[]res=new int[20]; res[0]=x; res[1]=y; for(i=2;i<n;i++) { z=x+y; res[i]=z; x=y; y=z; } return(res); } }

/*SERVER*/
import java.rmi.*; public class sfibo { public static void main(String args[])throws RemoteException { try { fiboimp m=new fiboimp(); Naming.rebind("fibo",m); System.out.print("Iam register"); } catch(Exception e){} } }

/*CLIENT*/
import java.rmi.*; import java.io.*; class clfibo { public static void main(String args[])throws IOException { int i; System.out.println("Enteer the no:\n"); DataInputStream dis=new DataInputStream(System.in); int d=Integer.parseInt(dis.readLine()); try { fibo m=(fibo)Naming.lookup("fibo"); int[] s=new int[d]; s=m.data(d); System.out.println("The series is: "); for(i=0;i<d;i++) System.out.println(s[i]); } catch(Exception e){} } }

OUTPUT
C:\jdk1.3\bin>javac fibo.java C:\jdk1.3\bin>javac fiboimp.java C:\jdk1.3\bin>start rmiregitry The system cannot find the file rmiregitry. C:\jdk1.3\bin>start rmiregistry C:\jdk1.3\bin>rmic fiboimp

/*SERVER*/
C:\jdk1.3\bin>javac sfibo.java C:\jdk1.3\bin>java sfibo Iam register

/*CLIENT*/
C:\jdk1.3\bin>javac clfibo.java C:\jdk1.3\bin>java clfibo Enteer the no: 3 The series is: 0 1 1

FILE TRANSFER IN CLIENT SERVER ARCHITECTURE USING RS232C


#include<stdio.h> #include<dos.h> #include<string.h> #include<conio.h> void main() { int c,ch,choice,i; char filename[15],filename 2[15],buf; FILE *in,*out; clrscr(); outportb(PORT+0,0*03); outportb(PORT+1,0*03); outportb(PORT+3,0*07); outportb(PORT+4,0*06); printf("THIS COMPUTER IS GOING TO ACT AS !"); printf("\n1.SENDER \n2.RECEIVER \n\nENTER THE CHOICE:"); scanf("%d",&choice); if(choice==1) { printf("ENTER THE FILEnAme TO BE CREATED:"); scanf("%s",filename); for(i=0;i<=strlen(filename);i++) outportb(PORT1,filename[i]); in=fopen(filename,"rt"); printf("\n\nSENDING....\n\n"); while(!feof(in)) { buf=fgetc(in); printf("%c",buf); outportb(PORT!,buf); delay(10); } }

else { i=0; buf='a'; while(buf!=NULL) { c=inportb(PORT 1+5); if(c&1) { buf=inportb(PORT1); filename 2[i]=buf; i++; } } out=fopen(filename2,"wt"); printf("\nFILENAME RECEIVED:%s",filename[2]); printf("\nRENDING FROM THE PORT:"); printf("\nWRITING TO FILE %s",filename2); do { c=importb(PORT1+5); if(c&1) { buf=inportb(PORT1); printf("%c",buf); fputc(buf,out); delay(10); } if(kbhit()) { ch=getch(); } } while(ch!=27); } }

OUTPUT:
This computer is going to act as a: 1.Sender 2.Receiver Enter the choice: 1 Enter the file name to be sent: a.doc Sending.

SOCKET PROGRAM FOR HTTP FOR WEBPAGE DOWNLOAD


import java.io.*; import java.net.*; class weblink { public static void main(String args[])throws IOException { URL u=new URL("http://www.google.co.in"); BufferedReader dis=new BufferedReader(new InputStreamReader(u.openStream())); System.out.println("Protocol name"+u.getProtocol()); System.out.println("host name is:"+u.getHost()); int port; port=u.getPort(); System.out.println("Port name is:"+port); System.out.println("file name is"+u.getFile()); System.out.println("ref name is"+u.getRef()); String str; while((str=dis.readLine())!=null) System.out.println(str); } }

/*OUTPUT*/
C:\jdk1.3\bin>javac webpage.java C:\jdk1.3\bin>java webpage protocol name ishttp host name iswww.google.co.in port name is:-1 filename is ref name isnull <!doctype html><html><head><meta http-equiv="content-type" content="text/html; c harset=ISO-88591"><title>Google</title><script>window.google={kEI:"ZEqPTLLmLorg 4waZvbzRCQ",kEXPI:"25856,25900,26446",kCSI: {e:"25856,25900,26446",ei:"ZEqPTLLmLo