-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxy.java
More file actions
43 lines (34 loc) · 1.29 KB
/
Proxy.java
File metadata and controls
43 lines (34 loc) · 1.29 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
package Proxy;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
//Main class of the package
//Only creates a server socket
public class Proxy {
private static final String HOST_IP = "127.0.0.1";
private InetSocketAddress address;
private ServerSocket proxy;
public Proxy(String ip, int port) throws IOException{
this.address = new InetSocketAddress(ip, port);
this.proxy = new ServerSocket();
this.proxy.setReuseAddress(true);
this.proxy.bind(address);
}
//Loop to listen to petitions and create a thread to handle it
public void open() throws IOException{
while (true){
Socket user = this.proxy.accept();
System.out.println("Connection from " + user.getLocalSocketAddress() + " has "
+ "been established.");
//Using threads to implement simultaneous connections
Server server = new Server(user);
new Thread(server).start();
}
}
public static void main(String[] args) throws IOException{
int PORT = Integer.parseInt(args[0]);
Proxy proxy = new Proxy(HOST_IP, PORT);
proxy.open();
}
}