-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.java
More file actions
69 lines (58 loc) · 1.61 KB
/
Copy pathserver.java
File metadata and controls
69 lines (58 loc) · 1.61 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
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
public class server extends Thread{
public static ArrayList<Socket> client = new ArrayList<Socket>();
Socket sock;
DataInputStream din;
DataOutputStream dout;
private static ServerSocket server;
public server(Socket sock){
this.sock = sock;
client.add(this.sock);
System.out.println(this.sock + " is added");
}
public void run(){
try {
this.din = new DataInputStream(this.sock.getInputStream());
byte[] buffer = new byte[(64*1024)];
int l=0,j=0;
while(true){
l=din.read(buffer);
// j+=1;
// System.out.println(l);
for (Socket socket : client) {
if (socket == this.sock){
continue;
}
dout = new DataOutputStream(socket.getOutputStream());
dout.write(buffer,0,l);
}
// dout.flush();
}
}catch(SocketException e1){
client.remove(this.sock);
System.out.println( this.sock + " is REMOVED ");
}catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
server = new ServerSocket(9999);
while(true){
System.out.println("waiting.............");
Socket sock = server.accept();
server s1 = new server(sock);
Thread t1 = new Thread(s1);
t1.start();
}
}
}