61FIT3NPR - W07 Tut Sending File Via TCP Socket
61FIT3NPR - W07 Tut Sending File Via TCP Socket
HANOI UNIVERSITY
Client
To connect to another machine we need two pieces of
information first one is the IP address and the second one
is the port number.
In our case, we are using localhost and the port is 900
We make a Socket object using the java.net package
Example
Socket ob = new Socket(ip,port _ number)
Now we call the “sendFile” method with the parameter of a file
path and we open the file and send the file to the server socket
using DataOutputStream Class
import java.io.*;
import java.net.Socket;
dataInputStream.close();
dataInputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
Server
Here, we define the ServerSocket object using the ServerSocket
class.
Example:
ServerSocket server = new ServerSocket(port_number)
when the client sent the request to the server socket. we
will call the “receiveFile” method
We receive the file from the client socket and read the file
using the data input stream class
In this method, we will change the file name and the
location of the file. write the file using FileOutputStream
Class.
Java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
dataInputStream.close();
dataOutputStream.close();
clientSocket.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
long size
= dataInputStream.readLong(); // read file size
byte[] buffer = new byte[4 * 1024];
while (size > 0
&& (bytes = dataInputStream.read(
buffer, 0,
(int)Math.min(buffer.length, size)))
!= -1) {
// Here we write the file using write method
fileOutputStream.write(buffer, 0, bytes);
size -= bytes; // read upto file size
}
// Here we received file
System.out.println("File is Received");
System.out.println("File \""+fileName+"\" is Received.");
fileOutputStream.close();
}
}
Output:
Example 2: Transfer a file from
Server TCP Socket to Client TCP
Socket
https://www.rgagnon.com/javadetails/java-0542.html
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public final static int SOCKET_PORT = 13267; // you may change this
public final static String SERVER = "127.0.0.1"; // localhost
public final static String
FILE_TO_RECEIVED = "c:/temp/source_copy.pdf";
// you may change this, I give a
// receive file
byte [] mybytearray = new byte [FILE_SIZE];
InputStream is = sock.getInputStream();
fos = new FileOutputStream(FILE_TO_RECEIVED);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
System.out.println("File " + FILE_TO_RECEIVED
+ " downloaded (" + current + " bytes read)");
}
finally {
if (fos != null) fos.close();
if (bos != null) bos.close();
if (sock != null) sock.close();
}
}
To try it, first you start the server. You make sure that the file to be sent (as
specified in SimpleFileServer) exists! Then you execute the client module.
Part