URDINESH: FILE
Showing posts with label FILE. Show all posts
Showing posts with label FILE. Show all posts

Tuesday, May 20, 2014

JAVA CODE FOR TCP FILE TRANSFER



PROGRAM: (SERVER)

import java.net.*;
import java.io.*;
import java.util.*;
class FileTcpServer
{
public static void main(String a[])throws IOException
{
BufferedReader din=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Source Filename::  ");
String sf=din.readLine();
String str=" ";
File srcf=new File(sf);
if(!srcf.exists() )
{
System.out.println(sf + "File not Found!!");
return;
}
FileInputStream fin=new FileInputStream(srcf);
int len=fin.available();
byte ch[]=new byte[len];
try
{
ServerSocket ss=new ServerSocket(3000);
while(fin.available() >0 )
{
Socket sock=ss.accept();
PrintWriter bw=new PrintWriter(sock.getOutputStream(),true);
fin.read(ch,0,len);
str=new String(ch,0,len);
System.out.println(str);
bw.println(str);
System.out.println(len);
sock.close();
}
}
catch(Exception e)
{         
}
fin.close();
}
}




PROGRAM: (CLIENT)

import java.net.*;
import java.io.*;
import java.util.*;
class FileTcpClient
{
public static void main(String a[])throws IOException
{
String str=" ";
BufferedReader din=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Destination Filename::  ");
String df=din.readLine();
File dstf=new File(df);
if(!dstf.exists() )
{
System.out.println(df + "File already Exists!!");
return;
}
FileOutputStream fout=new FileOutputStream(dstf);
try
{
Socket sock=new Socket(InetAddress.getByName("  "),3000);
DataInputStream br=new DataInputStream(sock.getInputStream());
int len=2341;
while(true )
{
byte buff[]=new byte[len];
br.read(buff,0,len);
str=new String(buff,0,len);
fout.write(buff);
System.out.println(str);
sock.close();
}                     
}
catch(Exception e)
{
}
fout.close();
}
}








OUTPUT: (SERVER)


Enter Source Filename::
Test1.java

OUTPUT: (CLIENT)

Enter Destination Filename::

Test2.java

Monday, May 19, 2014

JAVA CODE FOR RMI FILE TRANSFER



PROGRAM: (SERVER)

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;

class buff implements Serializable
     public byte b[];
   }
interface trans extends Remote
 {
    public buff get(String s) throws RemoteException;
}
public class fileTrans extends UnicastRemoteObject implements  trans
{
    fileTrans()throws RemoteException
{    }
  public buff get(String s) throws RemoteException
 {
    buff o=new buff();
  try
    {
      System.out.println(s);
      FileInputStream o1=new FileInputStream(s);
     o.b=new byte[o1.available()];
     o1.read(o.b);
     System.out.write(o.b);
     o1.close();
}
   catch(Exception e)
  {
   System.out.println(e);
}
return o;
  }
public static void main(String[] x)throws Exception
{
  fileTrans tt=new fileTrans();
  Naming.rebind("file",tt);
}
  }







PROGRAM: (CLIENT)

import java.rmi.*;
import java.rmi.registry.*;
import  java.io.*;
interface trans extends Remote
{
   public buff get(String s) throws RemoteException;
}

class getfile
{
   getfile()
  {

   try
   {
     trans m1=(trans)Naming.lookup("rmi://Localhost/file");
     String s="chord.wav";
     buff o=m1.get(s);
     FileOutputStream fo=new FileOutputStream("ch.wav");
     fo.write(o.b);
  }
  catch(Exception e)
{
   System.out.println(e);
}
  }
public static void main(String[] a)
{
  getfile o=new getfile();
}

  }

Followers