-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathServer.java
73 lines (64 loc) · 1.81 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
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
/**
* @author ambarmodi
*
*/
public class Server {
private static int PORT = 8080;
private ServerSocket serverSocket;
private static HashMap<String,Integer> requestedRes = new HashMap<String,Integer>();
private static final String DELIMITER = "|";
public static void main(String[] args) {
try {
System.out.println("\n====================Server Details====================");
System.out.println("Server Machine: "+ InetAddress.getLocalHost().getCanonicalHostName());
System.out.println("Port number: " + PORT);
System.out.println();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Here one instance of server is started..
try {
Server server = new Server();
server.start();
} catch (IOException e) {
System.err.println("Error occured:" + e.getMessage());
System.exit(0);
}
}
public Server() throws IOException {
serverSocket = new ServerSocket(PORT);
}
/**
* @throws IOException
*/
private void start() throws IOException {
while (true) {
Socket socket = serverSocket.accept();
HttpHandler connection = new HttpHandler(socket);
Thread request = new Thread(connection);
request.start();
}
}
/**
*
* @param res
* @param port2
* @param ipAddress
*/
public static void printResult(String res, int port2, String ipAddress) {
ipAddress = ipAddress.split(":")[0].replace("/", "");
if(requestedRes.get(res) == null) {
requestedRes.put(res, 1);
} else {
requestedRes.put(res, requestedRes.get(res) + 1);
}
System.out.println(res + DELIMITER+ ipAddress + DELIMITER + port2 +DELIMITER + requestedRes.get(res));
}
}