-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySerialServer.java
More file actions
59 lines (45 loc) · 1.19 KB
/
MySerialServer.java
File metadata and controls
59 lines (45 loc) · 1.19 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
package server_side;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class MySerialServer implements Server //Initializes the specific server according to the client and call for the feet client handler to solve the problem.
{
private int port;
private ClientHandler ch;
private volatile boolean stop;
public MySerialServer(int port,ClientHandler c) {
this.port=port;
this.ch=c;
stop=false;
}
@Override
public void open(ClientHandler c) {
new Thread(()->{
try {
runaServer(c);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}).start();
}
@Override
public void stop() {
stop= true;
}
private void runaServer(ClientHandler c) throws Exception {
ServerSocket server= new ServerSocket(port);
server.setSoTimeout(1000);
while(!stop) {
try {
Socket aclient=server.accept();
try {
c.handleClient(aclient.getInputStream(), aclient.getOutputStream());
aclient.close();
} catch (IOException e) {}
} catch(SocketTimeoutException e) {}
}
server.close();
}
}