-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
111 lines (100 loc) · 3.75 KB
/
Server.java
File metadata and controls
111 lines (100 loc) · 3.75 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
ArrayList<ClientHandler> clientHandlers=new ArrayList<>();;
Server(ServerSocket ss) {
try {
while(!ss.isClosed()){
Socket s = ss.accept();
System.out.println("A client is connected!");
ClientHandler clientHandler = new ClientHandler(s, this);
clientHandlers.add(clientHandler);
Thread client = new Thread(clientHandler);
client.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(5001);
new Server(ss);
}
}
class ClientHandler implements Runnable{
Server server;
ClientHandler currentClientHandler;
boolean receive = true;
private Socket s;
DataInputStream readData;
DataOutputStream sendClient;
ClientHandler(Socket s, Server server) throws Exception{
this.s=s;
readData=new DataInputStream(s.getInputStream());
sendClient = new DataOutputStream(s.getOutputStream());
this.server=server;
currentClientHandler=this;
}
public void run(){
try {
while (true) {
// This flag is used to avoid exceptions.
char flag = 'n';
if (receive) {
while (flag == 'n') {
flag = readData.readChar();
}
receive = false;
}
if (flag == 's') {
// reading file name;
int nameLength = readData.readInt();
StringBuilder fileName = new StringBuilder();
for (int j = 0; j < nameLength; j++) {
fileName.append(readData.readChar());
}
String file = "C:\\Share Stream\\";
sendOtherClients(file, fileName);
}
}
} catch(Exception e){
System.out.println("File handling problems!");
}
}
void sendOtherClients(String filePath, StringBuilder fileName){
// System.out.println(server.clientHandlers.size());
for(ClientHandler client: server.clientHandlers){
if(client==currentClientHandler) continue;
sendContents(filePath,fileName,client);
}
}
void sendContents(String filePath, StringBuilder fileName, ClientHandler client){
try {
client.sendClient.writeChar('s');
// Send file name and path
client.sendClient.writeInt(filePath.length());
client.sendClient.flush();
client.sendClient.writeChars(filePath);
client.sendClient.flush();
client.sendClient.writeInt(fileName.length());
client.sendClient.flush();
client.sendClient.writeChars(fileName.toString());
client.sendClient.flush();
// Send content of the file
long contentSize=readData.readLong();
client.sendClient.writeLong(contentSize);
int bytes=0;
byte[] buffer=new byte[4*1024];
while(contentSize>0 && (bytes=readData.read(buffer,0,(int)Math.min(buffer.length, contentSize)))!=-1){
client.sendClient.write(buffer,0,bytes);
client.sendClient.flush();
contentSize-=bytes;
}
receive=true;
} catch(Exception e){
e.printStackTrace();
}
}
}