-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
75 lines (61 loc) · 2.43 KB
/
Server.java
File metadata and controls
75 lines (61 loc) · 2.43 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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
public static void main(String[] argv) {
ServerSocket ss = null;
try {
ss = new ServerSocket(3000); // create a server socket
System.out.println("Sunucu Çalışıyor...");
ExecutorService clientPool = Executors.newFixedThreadPool(200); // limit the clients to 200
while (true) {
clientPool.execute(new ClientHandler(ss.accept())); // accept the coming client request and start a
// thread
}
} catch (IOException e) {
System.out.println(e);
} finally {
try {
if (ss != null)
ss.close();
} catch (IOException e) {
System.out.println(e);
}
}
System.out.println("Sunucu Durdu.");
}
private static class ClientHandler extends Thread { // client handler thread class
private Socket s;
ClientHandler(Socket s) { // constructor gets a socket
this.s = s;
}
public void run() {
try {
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
String username = din.readUTF(); // get a username from client
System.out.println("'" + username + "' " + "sunucuya bağlandı.");
File txtFile = new File("./sendToClient.txt"); // create a file class
byte[] bytes = new byte[8192];
InputStream fin = new FileInputStream(txtFile);
int count;
while ((count = fin.read(bytes)) > 0) { // read from file and add the bytes array
dout.write(bytes, 0, count);
}
System.out.println(username + " adlı kullanıcı bağlantıdan ayrıldı.");
fin.close();
dout.close();
s.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}