Created
June 23, 2017 17:06
-
-
Save techbless/8a7461c6de54b0559ed90d239779ae37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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