Skip to content

Instantly share code, notes, and snippets.

@techbless
Created June 23, 2017 17:06
Show Gist options
  • Save techbless/8a7461c6de54b0559ed90d239779ae37 to your computer and use it in GitHub Desktop.
Save techbless/8a7461c6de54b0559ed90d239779ae37 to your computer and use it in GitHub Desktop.
package Client;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
public class fileReciever {
public static void main(String[] args) {
try {
Socket sock = new Socket("127.0.0.1", 10001);
DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
DataInputStream dis = new DataInputStream(sock.getInputStream());
String fileName = "cake.mp3";
dos.writeUTF(fileName);
FileOutputStream fos = new FileOutputStream("D:\\save\\" + fileName);
int readCount = 0;
byte[] buffer = new byte[8192];
long start = System.currentTimeMillis();
while((readCount = dis.read(buffer)) != -1) {
fos.write(buffer, 0, readCount);
}
long end = System.currentTimeMillis();
System.out.println("[8192] Saved Safely! : " + (end - start) + "ms");
/*while(true) {
int data = dis.read();
if(data == -1) break;
fos.write(data);
}*/
dos.close();
dis.close();
fos.close();
sock.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
package Server;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class fileSender {
public static void main(String[] agrs) {
try {
ServerSocket servSock = new ServerSocket(10001);
Socket sock = servSock.accept();
DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
DataInputStream dis = new DataInputStream(sock.getInputStream());
String fileName = dis.readUTF();
FileInputStream fis = new FileInputStream("D:\\out\\" + fileName);
int readCount = 0;
byte[] buffer = new byte[8192];
while((readCount = fis.read(buffer)) != -1) {
dos.write(buffer, 0, readCount);
}
/*while(true) {
int data = fis.read();
if(data == -1) break;
dos.write(data);
}*/
fis.close();
dos.close();
dis.close();
sock.close();
servSock.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment