100% found this document useful (1 vote)
90 views3 pages

8C. File Transfer

The document describes a Java program that transfers a file from a server to a client. The server runs on port 139 and listens for connections from the client. When a connection is made, the server reads the requested file and sends its contents line-by-line to the client. The client writes the received lines to a new file. The program includes the server and client code, sample output, and compilation notes.

Uploaded by

Navin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
90 views3 pages

8C. File Transfer

The document describes a Java program that transfers a file from a server to a client. The server runs on port 139 and listens for connections from the client. When a connection is made, the server reads the requested file and sends its contents line-by-line to the client. The client writes the received lines to a new file. The program includes the server and client code, sample output, and compilation notes.

Uploaded by

Navin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF CSE
211416104185

FILE TRANSFER
Clientfile.java:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientfile
{
public static void main(String args[])
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Socket clsct=new Socket("127.0.0.1",139);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the file name:");
String str=in.readLine();
dout.writeBytes(str+'\n');
System.out.println("Enter the new file name:");
String str2=in.readLine();
String str1,ss;
FileWriter f=new FileWriter(str2);
char buffer[];
while(true) { str1=din.readLine();
if(str1.equals("-1"))
break;
System.out.println(str1);
buffer=new char[str1.length()];
str1.getChars(0,str1.length(),buffer,0);
f.write(buffer);
}
f.close();
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
211416104185
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}}}

Serverfile.java:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverfile
{
public static void main(String args[])
{
try
{
ServerSocket obj=new ServerSocket(139);
while(true)
{
Socket obj1=obj.accept();
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
FileReader f=new FileReader(str);
BufferedReader b=new BufferedReader(f);
String s;
while((s=b.readLine())!=null)
{
System.out.println(s);
dout.writeBytes(s+'\n');
}
f.close();
dout.writeBytes("-1\n");
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
211416104185
}}
catch(Exception e) { System.out.println(e);} } }

OUTPUT:
net.txt
computer lab
dept of CSE
panimalar

SERVER:
Z:\>javac Serverfile.java
Note: Serverfile.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Z:\>java Serverfile
computer lab
dept of CSE
panimalar

CLIENT:
Z:\>javac Clientfile.java
Note: Clientfile.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

Z:\>java Clientfile
Enter the file name:
net.txt

Enter the new file name:


sample.txt
computer lab
dept of CSE
panimalar

You might also like