Send files with java socket - java

i am sending files from two java applications here the source code of the server and the client
public class FileClient {
private Socket s;
public FileClient(String host, int port, String file) {
try {
s = new Socket(host, port);
sendFile(file);
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendFile(String file) throws IOException {
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
while ((fis.read(buffer) > 0)) {
dos.write(buffer);
}
fis.close();
dos.close();
}
public static void main(String[] args) {
FileClient fc = new FileClient("192.168.0.167", 1988, "C:/Users/mhattabi/Desktop/fileData.txt");
}
}
and here the source code of the server
public class FileServer extends Thread {
private ServerSocket ss;
public FileServer(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
Socket clientSock = ss.accept();
saveFile(clientSock);
// ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void saveFile(Socket clientSock) throws IOException {
DataInputStream dis = new DataInputStream(clientSock.getInputStream());
FileOutputStream fos = new FileOutputStream("fileData.txt");
byte[] buffer = new byte[4096];
int filesize = 15123; // Send file size in separate msg
int read = 0;
int totalRead = 0;
int remaining = filesize;
while((read = dis.read(buffer)) > 0) {
totalRead += read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
fos.close();
dis.close();
}
public static void main(String[] args) {
FileServer fs = new FileServer(1988);
fs.start();
}
}
the problem that in the server i received the file but there is extra character in it look like this.Any help will be appreciated thanks

Usual problem.
while ((fis.read(buffer) > 0)) {
dos.write(buffer);
}
You're ignoring the count returned by the read, and assuming that it filled the buffer. It should be:
while ((count = fis.read(buffer) > 0)) {
dos.write(buffer, 0, count);
}
Curiously enough you have this right in the server. NB You don't need a DataOutputStream here.

byte[] buffer = new byte[4096];
I think the" extra character" come from this place;every time you write 4096 byte
,at last time it happened less than 4096 byte.and then you get extra character

Related

Sending a string and file into InputStream

I'm trying to send a string which is the filename and then the file itself to a server. The string is being received and used to create the file by the server. However, there isn't any data actually written into the file by the server.
I got the file transfer to work before I added the Writer's (with the file names being hard-coded) to the server and client but now I can't get both to work at the same time.
Client:
public class Client {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
String fileName = sc.nextLine();
System.out.println(fileName);
try {
File file = new File(fileName);
Socket socket = new Socket("localhost", 15000);
OutputStream os = socket.getOutputStream();
Writer w = new OutputStreamWriter(os, "UTF-8");
w.write(fileName);
w.close();
os.flush();
byte[] mybytearray = new byte[(int) file.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.read(mybytearray, 0, mybytearray.length);
os.write(mybytearray, 0, mybytearray.length);
os.flush();
os.close();
socket.close();
} catch (Exception e) { }
}
}
}
Server:
public class Server extends Thread {
public static final int PORT = 15000;
#Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
while (true) {
Socket sock = serverSocket.accept();
readFile(sock);
}
} catch (Exception e) {
}
}
private void readFile(Socket socket) throws Exception {
InputStream ois = socket.getInputStream();
Reader r = new InputStreamReader(ois, "UTF-8");
String filename = "";
int ch = r.read();
while(ch != -1) {
filename += (char) ch;
System.out.println(filename);
ch = r.read();
}
r.close();
System.out.println(filename);
FileOutputStream fos = new FileOutputStream(filename);
byte[] bytearr = new byte[4096];
System.out.println("Reading file...");
BufferedOutputStream bos = new BufferedOutputStream(fos);
while ((ois.read(bytearr)) > 0) {
bos.write(bytearr);
}
bos.close();
System.out.println("Writing file complete...");
}
public static void main(String[] args) {
new Server().start();
}
}
This is my solution, this approach needs some improvements:
Explanation:
In position 0: Length of file name
In position 1 to the length of the file name: filename as bytes.
In position 1 + length of file name til length: The content of the file.
Basically, I'm sending all the information to the server at once (This is one improvement that you will need to figure out).
Another improvement is to send the file by chunks and not all the file at once.
Client class:
public class Client {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
String fileName = sc.nextLine();
System.out.println(fileName);
try {
File file = new File(fileName);
byte[] mybytearray = new byte[1 + fileName.getBytes().length + (int) file.length()];
mybytearray[0] = (byte) fileName.length();
System.arraycopy(fileName.getBytes(), 0, mybytearray, 1, fileName.getBytes().length);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.read(mybytearray, fileName.getBytes().length + 1, (int) file.length());
Socket socket = new Socket("localhost", 15000);
OutputStream os = socket.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
os.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Server class:
public class Server extends Thread {
public static final int PORT = 15000;
public static void main(String[] args) {
new Server().start();
}
#Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
while (true) {
Socket sock = serverSocket.accept();
readFile(sock);
}
} catch (Exception e) {
}
}
private void readFile(Socket socket) throws Exception {
InputStream ois = socket.getInputStream();
byte[] resultBuff = new byte[0];
byte[] buff = new byte[1024];
int k;
while ((k = ois.read(buff, 0, buff.length)) > -1) {
byte[] tbuff = new byte[resultBuff.length + k];
System.arraycopy(resultBuff, 0, tbuff, 0, resultBuff.length);
System.arraycopy(buff, 0, tbuff, resultBuff.length, k);
resultBuff = tbuff;
}
byte lengthOfFileName = resultBuff[0];
byte fileNameBytes[] = new byte[lengthOfFileName];
System.arraycopy(resultBuff, 1, fileNameBytes, 0, lengthOfFileName);
String filename = new String(fileNameBytes);
FileOutputStream fos = new FileOutputStream(filename + System.currentTimeMillis());
byte[] bytearr = new byte[resultBuff.length - (lengthOfFileName + 1)];
System.out.println("Writing file...");
System.arraycopy(resultBuff, lengthOfFileName + 1, bytearr, 0, bytearr.length);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(bytearr);
bos.close();
System.out.println("Writing file complete...");
}
}
Hope this helps!
Happy coding time!
You need to close the File Ouptut Stream as well. With
bos.close;
Add
fos.close;

How to close Output/Input and FileOutput/Input streams properly?

I'm making simple Client-Server FileTransfer application in java.
Here is the concept of this application:
Client connects to server
Server shows menu to Client which looks like this
2a) Make copy of file on server
2b) Download copy of file from
server
2c) Exit
Client choose one of those options (Client sends message by ObjectOutputStream, code below).
The problem is when i choose one of options (for example Option #2a) I can't choose another one when previous is done. It says that socket is close.
There is my code:
Client:
public class Client {
private Socket s = new Socket("localhost",3002);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
FileInputStream fis;
public Client(String host, int port) throws IOException {
//s = new Socket(host, port);
try {
System.out.println("Hello!");
boolean finished = false;
Scanner sc = new Scanner(System.in);
while(!finished){
System.out.println("\n\n1.Make copy of file on server");
System.out.println("2.Restore copy");
System.out.println("3.Exit\n");
char c = sc.nextLine().charAt(0);
switch(c){
case '1':
this.sendMessage(1);
makeCopy();
oos.close();
break;
case '2':
this.sendMessage(2);
saveFile(s);
break;
case '3':
this.sendMessage(3);
finished=true;
System.exit(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
Client client = new Client("localhost",3002);
}
public void sendMessage(int message_id) throws IOException{
oos.writeInt(message_id);
oos.flush();
}
private void makeCopy() throws IOException {
File file = new File("D:\\klient\\doKopii.bmp");
File dest = new File("D:\\serwer\\kopiaPliku.bmp");
boolean ifExists = dest.exists();
if(ifExists && !file.isDirectory()){
System.out.println("Copy is already on server.");
}
else{
fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
while (fis.read(buffer) > 0) {
oos.write(buffer);
}
}
//fis.close();
//oos.close();
}
private void saveFile(Socket clientSock) throws IOException {
//DataInputStream dis = new DataInputStream(clientSock.getInputStream());
FileOutputStream fos = new FileOutputStream("D:\\klient\\przywroconaKopia.bmp");
File zSerwera = new File("D:\\serwer\\kopiaPliku.bmp");
byte[] buffer = new byte[4096];
int filesize = (int)zSerwera.length();
int read = 0;
int totalRead = 0;
int remaining = filesize;
while((read = ois.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
//fos.close();
//ois.close();
}
}
And Server side code:
public class Server extends Thread{
private ServerSocket ss;
Socket clientSock;
ObjectInputStream ois;
ObjectOutputStream oos;
//DataOutputStream dos;
FileInputStream fis;
//DataInputStream dis;
FileOutputStream fos;
public Server(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while (true) {
clientSock = ss.accept();
oos = new ObjectOutputStream(clientSock.getOutputStream());
ois = new ObjectInputStream(clientSock.getInputStream());
serviceClient(clientSock);
}
}catch(Exception e){
e.printStackTrace();
}
}
public void serviceClient(Socket s) throws Exception{
int message_id;
try{
message_id = ois.readInt();
switch(message_id){
case 1:
saveFile(s);
oos.flush();
break;
case 2:
sendFile(s);
oos.flush();
break;
}
}catch(Exception e){
e.printStackTrace();
}
}
public void sendFile(Socket s) throws IOException {
File file = new File("D:\\serwer\\kopiaPliku.bmp");
fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
while (fis.read(buffer) > 0) {
oos.write(buffer);
}
}
public void saveFile(Socket s) throws IOException{
//dis = new DataInputStream(s.getInputStream());
File copy = new File("D:\\serwer\\kopiaPliku.bmp");
if(copy.exists() && !copy.isDirectory()){
}
else{
fos = new FileOutputStream(copy);
File zSerwera = new File("D:\\klient\\doKopii.bmp");
byte[] buffer = new byte[4096];
int filesize = (int)zSerwera.length();
int read = 0;
int totalRead = 0;
int remaining = filesize;
while((read = ois.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
}
}
public static void main(String[] args) {
Server server = new Server(3002);
server.start();
}
}
Exact Exception:
java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:116)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.writeBlockHeader(ObjectOutputStream.java:1890)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1875)
at java.io.ObjectOutputStream$BlockDataOutputStream.flush(ObjectOutputStream.java:1822)
at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:719)
at com.luki.classes.Client.sendMessage(Client.java:58)
at com.luki.classes.Client.<init>(Client.java:33)
at com.luki.classes.Client.main(Client.java:53)
Where do I have to close all of those streams to prevent socket closing ?
You close your ObjectOutputStreamat the end of your first case statement in Client for no apparent reason. Remove that line and you won't get the error.

Multiple file chunks transfer over socket to multiple clients

I have to transfer chunks of a file to different clients using one server.
When i run the server file and provide the name of the file it successfully makes chunks. when i run the first client for first time it works but when i run it for the client again(by that i mean when i connect as a second client) it fails to transfer chunks to the second client. Complete code of server and client are shown below.
error is for the second client it starts reading the contents of the file as filename and program terminates.
provide a large text file(1MB) file as input to server
Server Code:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
public class server {
private static final int sPort = 8000; //The server will be listening on this port number
public static String str;
public static int c;
public static void main(String[] args) throws Exception {
System.out.println("The server is running.");
ServerSocket listener = new ServerSocket(sPort);
int clientNum = 1;
System.out.println("Enter the name of the file: ");
Scanner in = new Scanner(System.in);
str = in.nextLine();
String path = System.getProperty("user.dir");
String filepath = path +"/"+ str;
in.close();
try {
c=splitFile(new File(filepath));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
while(true) {
new Handler(listener.accept(),clientNum,c).start();
System.out.println("Client " + clientNum + " is connected!");
clientNum++;
}
} finally {
listener.close();
}
}
/**
* A handler thread class. Handlers are spawned from the listening
* loop and are responsible for dealing with a single client's requests.
*/
private static class Handler extends Thread {
private Socket connection;
private int chunkcount;
private ObjectInputStream in; //stream read from the socket
private ObjectOutputStream out; //stream write to the socket
private int no; //The index number of the client
public Handler(Socket connection, int no,int c) {
this.connection = connection;
this.no = no;
this.chunkcount=c;
}
public void run() {
try{
//initialize Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
try{
String path = System.getProperty("user.dir");
path=path+"/"+"chunks"+ "/";
System.out.println(path);
System.out.println("Total chunks: "+chunkcount);
int i=no;
int j=i;
int k=0;
OutputStream op=connection.getOutputStream();
DataOutputStream d = new DataOutputStream(op);
d.writeInt(no);
d.flush();
System.out.println("value of j or clientnum: "+j);
while(j<chunkcount)
{
k++;
j=j+5;
}
System.out.println(k);
d.writeInt(k);
d.flush();
//d.close();
while(i<chunkcount)
{
String pathname= path+Integer.toString(i)+str;
System.out.println(i+str);
sendFile(connection,pathname);
i=i+5;
}
}
catch(Exception e){
e.printStackTrace();
}
}
catch(IOException ioException){
System.out.println("Disconnect with Client " + no);
}
finally{
//Close connections
try{
in.close();
out.close();
connection.close();
}
catch(IOException ioException){
System.out.println("Disconnect with Client " + no);
}
}
}
}
public static int splitFile(File f) throws IOException {
int partCounter = 1;//I like to name parts from 001, 002, 003, ...
//you can change it to 0 if you want 000, 001, ...
int sizeOfFiles = 102400;// 1MB
byte[] buffer = new byte[sizeOfFiles];
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(f))) {//try-with-resources to ensure closing stream
String name = f.getName();
String path = f.getParent();
long sizefile = f.getTotalSpace();
String newpath = path + "/" + "chunks";
File dir = new File(newpath);
dir.mkdir();
int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
//write each chunk of data into separate file with different number in name
File newFile = new File(dir, String.format("%d", partCounter++) + name );
//System.out.println(f.getParent());
try (FileOutputStream out = new FileOutputStream(newFile)) {
out.write(buffer, 0, tmp);//tmp is chunk size
}
}
System.out.println("File details are : "+name+" "+sizefile);
System.out.println("Number of chunks: "+ (partCounter-1));
}
return (partCounter-1);
}
public static void sendFile(Socket conn,String fileName) throws IOException
{
File myFile = new File(fileName);
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
dis.readFully(mybytearray, 0, mybytearray.length);
OutputStream os = conn.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(myFile.getName());
dos.writeLong(mybytearray.length);
dos.write(mybytearray, 0, mybytearray.length);
dos.flush();
dis.close();
}
}
client code:
import java.net.*;
import java.io.*;
public class Client {
Socket requestSocket; //socket connect to the server
ObjectOutputStream out; //stream write to the socket
ObjectInputStream in; //stream read from the socket
public Client() {}
void run()
{
try{
//create a socket to connect to the server
requestSocket = new Socket("localhost", 8000);
System.out.println("Connected to localhost in port 8000");
//initialize inputStream and outputStream
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
System.out.println("Ready to receive files ( Enter QUIT to end):");
BufferedInputStream in1 = new BufferedInputStream(requestSocket.getInputStream());
DataInputStream d = new DataInputStream(in1);
int clientnum=d.readInt();
String path = System.getProperty("user.dir");
String oppath = path + "/" + "Client" + clientnum;
File dir = new File(oppath);
dir.mkdir();
int numchunk=d.readInt();
System.out.println(numchunk);
int jakarta=1;
while(jakarta<=numchunk ){
jakarta++;
String newpath=oppath+"/";
File f = new File(newpath);
f.createNewFile();
receiveFile(requestSocket,newpath);
System.out.println("File Received");
}
}
catch (ConnectException e) {
System.err.println("Connection refused. You need to initiate a server first.");
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//Close connections
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
//send a message to the output stream
public static void receiveFile(Socket s1,String oppath) throws IOException
{
String fileName;
try {
int bytesRead;
InputStream in = s1.getInputStream();
DataInputStream clientData = new DataInputStream(in);
fileName = clientData.readUTF();
OutputStream output = new FileOutputStream(oppath+fileName);
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0
&& (bytesRead = clientData.read(buffer, 0,
(int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
output.flush();
output.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//main method
public static void main(String args[])
{
Client client = new Client();
client.run();
}
}

Java: A client/server file transfer (incomplete files)

I am building an application that will send file across the network through sockets.
There are 2 programs. Server and Client each have two classes Download and Upload.
public class Download implements Runnable{
public ServerSocket server;
public Socket socket;
public int port;
public String saveTo = "";
public InputStream In;
public FileOutputStream Out;
public Download(String saveTo){
try {
server = new ServerSocket(1544);
port = 1544;
this.saveTo = saveTo;
}
catch (IOException ex) {
System.out.println("Exception [Download : Download(...)]");
}
}
#Override
public void run() {
try {
socket = server.accept();
In = socket.getInputStream();
Out = new FileOutputStream(saveTo);
byte[] buffer = new byte[1024];
int count;
while((count = In.read(buffer)) >= 0){
Out.write(buffer, 0, count);
}
Out.flush();
if(Out != null){ Out.close(); }
if(In != null){ In.close(); }
if(socket != null){ socket.close(); }
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
and class Upload:
public class Upload implements Runnable{
public String addr;
public int port;
public Socket socket;
public FileInputStream In;
public OutputStream Out;
public File file;
public Upload(String addr, int port, File filepath){
super();
try {
file = filepath;
socket = new Socket("127.0.0.1",1545);
Out = socket.getOutputStream();
In = new FileInputStream(filepath);
}
catch (Exception ex) {
System.out.println("Exception [Upload : Upload(...)]");
}
}
#Override
public void run() {
try {
byte[] buffer = new byte[1024];
int count;
while((count = In.read(buffer)) >= 0){
Out.write(buffer, 0, count);
}
Out.flush();
if(In != null){ In.close(); }
if(Out != null){ Out.close(); }
if(socket != null){ socket.close(); }
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
And in the class server:
File myFile = new File("temp");
Upload upl = new Upload("temp", 1544,myFile)
fileThreadSend = new Thread(upl);
fileThreadSend.start();
and in class client:
Download dwn = new Download("temp");
Thread fileThread = new Thread(dwn);
fileThread.start();
the problem runs but it doesn't transfer all the file
sometimes it transfer just 9kb of it and sometimes 20 MBS.
what is wrong?
I tried to search but couldn't find applicable any solution.
Thanks in advance
Don't be bothered and use what the JDK has to offer:
create a file from an InputStream;
send the contents of a file to an OutputStream.
More details here. And don't forget to use try-with-resources.
Also, avoid using Thread directly and use an ExecutorService instead.

Send a stop message in a socket

I want a efficient and a fast way of sending a stop message in a socket.
I have a method that send files from one pc to another. All of the files from the sender appear on the receiver's PC. However all of the data is being written to the first file (only). The other files exist, but are empty. This happen because the receiver method doesn't know when to start writing to the next file.
Sender
public static void sendFile (final Socket sock, File source)
{
FileInputStream fileIn = null;
try
{
//Read bytes from the source file
fileIn = new FileInputStream(source);
//Write bytes to the receive
//No need to use a buffered class, we make our own buffer.
OutputStream netOut = sock.getOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int read;
while ((read = fileIn.read(buffer)) != -1)
{
netOut.write(buffer, 0, read);
netOut.flush ();
}
//Send some stop message here
}
catch (Exception e)
{
e.printStackTrace ();
}
finally
{
if (fileIn != null)
{
try
{
fileIn.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
}
}
}
//Send files via socket
public static void sendFile (final Socket sock, File[] source)
{
for (int i = 0; i < source.length; i++)
sendFile (sock, source[i]);
}
Receiver:
public static void receiveFile (final Socket sock, File destination)
{
BufferedOutputStream out = null;
try
{
//Receive data from socket
InputStream clientInputStream = sock.getInputStream();
//Write bytes to a file
out = new BufferedOutputStream (new FileOutputStream (destination));
byte[] buffer = new byte[BUFFER_SIZE];
int read;
while (true)
{
read = clientInputStream.read(buffer);
out.write(buffer, 0, read);
out.flush ();
}
}
catch (IOException e)
{
e.printStackTrace ();
}
finally
{
if (out != null)
{
try
{
out.close ();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
//Receive files via socket
public static void receiveFile (final Socket sock, File[] destination)
{
for (int i = 0; i < destination.length; i++)
receiveFile (sock, destination[i]);
}
You need to modify your send/receive protocol to include at least a minimal header before sending the file. Your header should include at least the size of the data to follow, and anything else you might want (such as file name).
I tried with a header like you suggested, but it doesn't work. The receiver still don't know when to stop(so I get EOFException). All the received data get written to the first file.
public static void sendFile (Socket sock, File source)
{
FileInputStream fileIn = null;
try
{
//Read bytes from the source file
fileIn = new FileInputStream(source);
//Write bytes to the receive
//No need to use a buffered class, we make our own buffer.
OutputStream netOut = sock.getOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int readBytes = 0;
long fileSize = source.length();
long counter = 0;
//Send the file size
DataOutputStream objOut = new DataOutputStream (netOut);
System.out.println ("Writing: " + source.length ());
objOut.writeLong (fileSize);
objOut.flush ();
while ((counter += readBytes) < fileSize)
{
readBytes = fileIn.read(buffer);
netOut.write(buffer, 0, readBytes);
netOut.flush ();
}
fileIn.close();
}
catch (Exception e)
{
e.printStackTrace ();
}
finally
{
if (fileIn != null)
{
try
{
fileIn.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
}
}
}
[]
public static void receiveFile (Socket sock, File destination)
{
BufferedOutputStream fileOut = null;
try
{
//Receive data from socket
InputStream netIn = sock.getInputStream();
//Write bytes to a file
fileOut = new BufferedOutputStream (new FileOutputStream (destination));
byte[] buffer = new byte[BUFFER_SIZE];
int readBytes = 0;
long fileSize;
long counter = 0;
//Receive the file size
DataInputStream objIn = new DataInputStream (netIn);
fileSize = objIn.readLong ();
System.out.println ("Receiving: " + fileSize);
while (true)
{
readBytes = netIn.read (buffer);
fileOut.write (buffer, 0, readBytes);
fileOut.flush ();
counter += readBytes;
if (counter > fileSize)
break;
}
}
catch (IOException e)
{
e.printStackTrace ();
}
finally
{
if (fileOut != null)
{
try
{
fileOut.close ();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
System.out.println ("Ending method");
}

Categories