forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoServer.java
More file actions
59 lines (53 loc) · 1.57 KB
/
Copy pathEchoServer.java
File metadata and controls
59 lines (53 loc) · 1.57 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
import java.net.Socket;
import java.net.ServerSocket;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.File;
import java.io.FilenameFilter;
public class EchoServer{
public static final int ECHO_SERVER_PORT = 9999;
public static void main(String[] args){
System.out.println("EchoServer started.");
ServerSocket ss = null;
try{
ss = new ServerSocket(ECHO_SERVER_PORT);
}catch(IOException ioe){
ioe.printStackTrace();
return;
}
File indicatingFile = new File("kill"); // Just represents file path.
do{
try{
Socket socket = ss.accept();
// explore the ports.
System.out.println("LocalPort: " + socket.getLocalPort() + ", RemotePort: " + socket.getPort());
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String incomingMsg = br.readLine();
System.out.println("ReceiveMsg: " + incomingMsg);
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.println(incomingMsg);
pw.flush();
socket.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}while(!commandKill(indicatingFile));
if(ss!=null){
try{
ss.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
static boolean commandKill(File file){
return file.exists() && file.isFile();
// Just need to verify the existence of the single interesting file, not need to use filter.
}
}