URDINESH: JAVA CODE FOR TCP FILE TRANSFER
Showing posts with label JAVA CODE FOR TCP FILE TRANSFER. Show all posts
Showing posts with label JAVA CODE FOR TCP FILE 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

Followers