-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSharingServer.java
More file actions
33 lines (24 loc) · 927 Bytes
/
FileSharingServer.java
File metadata and controls
33 lines (24 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package fileSharing;
import java.io.*;
import java.net.*;
public class FileSharingServer {
public static void main(String[] args) throws IOException {
ServerSocket soc = new ServerSocket(12345);
System.out.println("Server started...");
Socket sock = soc.accept();
System.out.println("Client connected...");
DataInputStream dis = new DataInputStream(sock.getInputStream());
String fileName = dis.readUTF();
long fileSize = dis.readLong();
FileOutputStream fos = new FileOutputStream(fileName);
byte[] buffer = new byte[4096];
int read;
long remaining = fileSize;
while((read = dis.read(buffer,0,Math.min(buffer.length, (int) remaining))) > 0) {
fos.write(buffer,0,read);
remaining -= read;
}
fos.close();
System.out.println("File received: " + fileName);
}
}