URDINESH: TRANSFER
Showing posts with label TRANSFER. Show all posts
Showing posts with label TRANSFER. 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 UDP DATAGRAM-MESSAGE TRANSFER




PROGRAM: (UDP SERVER)

import java.io.*;
import java.net.*;
class UDPserver
{
   public static void main(String args[]) throws Exception
    {
       DatagramSocket serverSocket=new DatagramSocket(9876);
       byte[] receiveData=new byte[1024];
       byte[] sendData=new byte[1024];
     while(true)
     {
         DatagramPacket receivePacket=new                                  

         DatagramPacket(receiveData,receiveData.length);
         serverSocket.receive(receivePacket);
         String sentence=new String(receivePacket.getData());
         System.out.println("RECEIVED:"+sentence);
         InetAddress IPAddress=receivePacket.getAddress();
         int port=receivePacket.getPort();
         String capitalizedSentence=sentence.toUpperCase();
         sendData=capitalizedSentence.getBytes();
        DatagramPacket sendPacket=new     
        DatagramPacket(sendData,sendData.length,IPAddress,port);
        serverSocket.send(sendPacket);
  }
    }
       }

PROGRAM: (CLIENT)

import java.io.*;
import java.net.*;
class  UDPclient
{
     public static void main(String arg[]) throws Exception
      {
          BufferedReader inFromUser=new  BufferedReader(new    
          InputStreamReader(System.in));
          DatagramSocket clientSocket=new  DatagramSocket();
          InetAddress  IPAddress=InetAddress.getByName("localhost");
          byte[] sendData=new byte[1024];
byte[] receiveData=new byte[1024];
String sentence=inFromUser.readLine();
sendData=sentence.getBytes();
DatagramPacket sendPacket=new  DatagramPacket(sendData,sendData.length,IPAddress,9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence=new String(receivePacket.getData());
System.out.println("FROM SERVER:"+ modifiedSentence);
clientSocket.close();
}
  }
  
OUTPUT: (SERVER)


RECEIVED:Hai

OUTPUT: (CLIENT)

Hai

FROM SERVER: hai

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