You are on page 1of 7

Exp No: 9 FILE TRANSFER USING JAVA SOCKETS

01.09.2010

AIM:
To demonstrate file transfer using Java Sockets.

DESCRIPTION:

 Socket class:
The creation of Socket class object is used to establish connection between the client and
the server. It is contained in the java.net package.
Creating instance of Socket class:
Syntax:
Socket s= new Socket (String hostname, int port);

 ServerSocket class:
ServerSocket runs on the server side and listens to incoming TCP connections. Each
ServerSocket listens on a particular port on the server side.
Creating instance of ServerSocket class:
Syntax:
ServerSocket s= new ServerSocket(int port);

 FileInputStream class:
It creates an InputStream that can be used to read bytes from a file.
Creating instance of FileInputStream class:
Syntax:
FileInputStream fos= new FileInputStream(String Filepath);

 FileOutputStream class:
It creates an OutputStream that can be used to write bytes to a file.
Creating instance of FileOutputStream class:
Syntax:
FileOutputStream fos= new FileOutputStream(String Filepath);

 DataInputStream class:
It is an InputStream that contains methods for reading the Java standard data types.
Creating instance of DataInputStream class:
Syntax:
DataInputStream dis=new DataInputStream(s.getInputStream());
Where s is an Socket object and the getInputStream method returns an
InputStream that can read data from a socket.

 DataOutputStream class:
It is an OutputStream that contains methods for writing the Java standard data types.
Creating instance of DataOutputStream class:
Syntax:
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
Where s is an Socket object and the getOutputStream method returns an
OutputStream that can read data from an application to the other end of the socket.

ALGORITHM:
Server Side:

1. Import the packages java.io.* and java.net.*.


2. Create a ServerSocket on a particular port.
3. Using the ServerSocket object call the accept( ) method using which the server listens for
incoming connections and establish the connection.
4. Create DataInputStream object to receive the source filename from the client.
5. Create FileInputStream object to read the contents of the source file.
6. The contents being read using the read method is written to the client through the socket using
the write method.
7. Repeat step 6 till the all the contents have been written to the client side.
8. Stop.

Client Side:

1. Import the packages java.io.* and java.net.*.


2. Create an object for Socket.
3. Create a DataInputStream object to receive the source filename from the user.
4. Create a DataOutputStream object to send the source filename to the server through the
socket.
5. Create a DataInputStream object to receive the contents of the file from the server.
6. Create a FileOutputStream object to write the contents being received from the server onto an
output file and perform the write operation.
7. Stop.
Code:

//Server.java
import java.net.*;
import java.io.*;
public class server
{
public static void main(String[] args) throws Exception
{

ServerSocket s=new ServerSocket(2234);


Socket s1;
int temp;
s1 = s.accept();
DataInputStream dis=new DataInputStream(s1.getInputStream());
String filename=dis.readLine();
System.out.println("Filename is "+filename);
FileInputStream fis=new FileInputStream(filename);
DataOutputStream dos=new
DataOutputStream(s1.getOutputStream());
while((temp=fis.read())!=-1)
{
System.out.print((char)temp);
dos.write(temp);
}
dos.close();
fis.close();
}
}

//Client.java
import java.io.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;

class gclient extends WindowAdapter implements ActionListener


{
Frame f;
Button b;
TextField tf,t2;
Label l;
TextArea aa;
String g="";
public gclient()
{
f=new Frame("File Download");
tf=new TextField("Enter the file name");
t2=new TextField("Enter the new file");
b=new Button("Download");
l=new Label("Connecting....pls wait");
aa=new TextArea("Content of the file",20,100);
f.setLayout(new FlowLayout());
f.add(l);
f.add(tf);
f.add(t2);
f.add(b);
f.add(aa);
b.addActionListener(this);
f.addWindowListener(this);
f.setSize(500,500);
f.setVisible(true);

}
public void actionPerformed(ActionEvent ae)
{
//socketconnection
try
{
Socket c1=new Socket("127.0.0.1",2234);
int s;
DataInputStream dis=new DataInputStream(System.in);
l.setText("Connected Successfully..");
String filename=tf.getText();
String f2=t2.getText();
DataOutputStream dos=new
DataOutputStream(c1.getOutputStream());
dos.writeBytes(filename);
dos.writeBytes("\n");
DataInputStream in=new DataInputStream(c1.getInputStream());
FileWriter fw=new FileWriter(f2);
BufferedWriter bw=new BufferedWriter(fw);
while((s=in.read())!=-1)
{
System.out.print((char)s);
bw.write(s);
g+=(char)s;
aa.setText(g);
}
dos.close();
dis.close();
in.close();
bw.close();
fw.close();
}
catch(Exception e)
{
}
}
public void windowClosing(WindowEvent e){ System.exit(0); }
public static void main(String[] args) throws Exception
{
gclient s=new gclient();
}
}

Sample Output:

You might also like