-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
89 lines (72 loc) · 1.88 KB
/
Server.java
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
package chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Scanner;
public class Server {
private static InetAddress externalIp;
public static InetAddress getExternalIp() {
return externalIp;
}
public static String getWebServerIp() {
String ip = null;
BufferedReader in = null;
try {
URL whatismyip = new URL ("http://checkip.amazonaws.com");
in = new BufferedReader(new InputStreamReader(
whatismyip.openStream()));
ip = in.readLine(); //you get the IP as a String
System.out.println(ip);
} catch (IOException e1) {
System.out.println("Problem with connection to server " + e1.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.out.println("Problem with closing connection " + e.getMessage());
}
}
}
return ip;
}
public static void main(String[] args) {
Calendar time = null;
InetAddress ip = null;
try {
ip = InetAddress.getByName("0.0.0.0");
externalIp = InetAddress.getByName(getWebServerIp());
} catch (UnknownHostException e) {
e.printStackTrace();
}
ServerSocket server = null;
try {
server = new ServerSocket(1216,10,ip);
HandleClients handler = new HandleClients(server);
new Thread(handler).start();
Scanner input = new Scanner(System.in);
String console = null;
while(true) {
console = input.nextLine();
if (console.equals("stop")) {
break;
}
if (console.equals("time")) {
time = Calendar.getInstance();
System.out.println(time.getTime());
}
}
if (input != null) {
input.close();
}
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}