Bind sur localhost
This commit is contained in:
parent
9b6258afd3
commit
cc06ccc334
@ -64,7 +64,7 @@ public class ServerManagementTCP implements Runnable {
|
|||||||
initFileList();
|
initFileList();
|
||||||
initSha512();
|
initSha512();
|
||||||
try {
|
try {
|
||||||
socket = new ServerSocket(TCPPort);
|
socket = new ServerSocket(TCPPort, 10, InetAddress.getLocalHost());
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
logger.writeTCP("Error: cannot listen on port " + TCPPort, LogLevel.Error);
|
logger.writeTCP("Error: cannot listen on port " + TCPPort, LogLevel.Error);
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
|
@ -33,6 +33,7 @@ import protocolP2P.HashResponse;
|
|||||||
import tools.HostItem;
|
import tools.HostItem;
|
||||||
import protocolP2P.Register;
|
import protocolP2P.Register;
|
||||||
import protocolP2P.Unregister;
|
import protocolP2P.Unregister;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
|
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
|
||||||
* @author Louis Royer
|
* @author Louis Royer
|
||||||
@ -62,10 +63,13 @@ public class ServerManagementUDP implements Runnable {
|
|||||||
initFileList();
|
initFileList();
|
||||||
initSha512();
|
initSha512();
|
||||||
try {
|
try {
|
||||||
socket = new DatagramSocket(UDPPort);
|
socket = new DatagramSocket(UDPPort, InetAddress.getLocalHost());
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
logger.writeUDP("Error: cannot listen on port " + UDPPort, LogLevel.Error);
|
logger.writeUDP("Error: cannot listen on port " + UDPPort, LogLevel.Error);
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
logger.writeUDP(e, LogLevel.Error);
|
||||||
|
System.exit(-2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ public class HostItem {
|
|||||||
private int port;
|
private int port;
|
||||||
private Socket tcpSocket;
|
private Socket tcpSocket;
|
||||||
private DatagramSocket udpSocket;
|
private DatagramSocket udpSocket;
|
||||||
|
private InetAddress inetAddress;
|
||||||
|
|
||||||
/** Constructor with hostname and port
|
/** Constructor with hostname and port
|
||||||
* @param hostname Hostname
|
* @param hostname Hostname
|
||||||
@ -125,4 +126,25 @@ public class HostItem {
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return hostname.hashCode() ^ port;
|
return hostname.hashCode() ^ port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InetAddress getInetAddress() {
|
||||||
|
if (inetAddress == null) {
|
||||||
|
try {
|
||||||
|
inetAddress = InetAddress.getByName(getHostname());
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
System.err.println("Error: Unknown host.");
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inetAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HostItem(Socket s) {
|
||||||
|
tcpSocket = s;
|
||||||
|
inetAddress = s.getInetAddress();
|
||||||
|
hostname = inetAddress.getCanonicalHostName();
|
||||||
|
port = s.getPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -120,4 +120,5 @@ public class Logger {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,8 @@ import protocolP2P.DiscoverResponse;
|
|||||||
import protocolP2P.FileList;
|
import protocolP2P.FileList;
|
||||||
import protocolP2P.HashRequest;
|
import protocolP2P.HashRequest;
|
||||||
import localException.InternalError;
|
import localException.InternalError;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
|
||||||
|
|
||||||
/** Tracker management implementation with tcp
|
/** Tracker management implementation with tcp
|
||||||
@ -32,7 +34,7 @@ import localException.InternalError;
|
|||||||
*/
|
*/
|
||||||
public class TrackerManagementTCP implements Runnable {
|
public class TrackerManagementTCP implements Runnable {
|
||||||
|
|
||||||
private int port;
|
private HostItem tracker;
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
private ServerSocket socket;
|
private ServerSocket socket;
|
||||||
private List<HostItem> hostList = new ArrayList<>();
|
private List<HostItem> hostList = new ArrayList<>();
|
||||||
@ -43,15 +45,15 @@ public class TrackerManagementTCP implements Runnable {
|
|||||||
* @param logger Logger object
|
* @param logger Logger object
|
||||||
*/
|
*/
|
||||||
public TrackerManagementTCP(int port, Logger logger) {
|
public TrackerManagementTCP(int port, Logger logger) {
|
||||||
this.port = port;
|
tracker = new HostItem("localhost", port);
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
try {
|
try {
|
||||||
socket = new ServerSocket(port);
|
socket = new ServerSocket(tracker.getPort(), 10, tracker.getInetAddress());
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
logger.writeTCP("Error: cannot listen on port " + port, LogLevel.Error);
|
logger.writeTCP("Error: cannot listen on" + tracker, LogLevel.Error);
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.writeTCP("Error: cannot openning socket", LogLevel.Error);
|
logger.writeTCP("Error: cannot open socket", LogLevel.Error);
|
||||||
System.exit(-2);
|
System.exit(-2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,13 +78,12 @@ public class TrackerManagementTCP implements Runnable {
|
|||||||
*/
|
*/
|
||||||
private class ClientHandler implements Runnable {
|
private class ClientHandler implements Runnable {
|
||||||
private Socket s;
|
private Socket s;
|
||||||
private String addr;
|
private HostItem addr;
|
||||||
/** Constructor with a socket.
|
/** Constructor with a socket.
|
||||||
* @param s Socket of this client
|
* @param s Socket of this client
|
||||||
*/
|
*/
|
||||||
public ClientHandler(Socket s) {
|
public ClientHandler(Socket s) {
|
||||||
this.s = s;
|
this.addr = new HostItem(s);
|
||||||
this.addr = "[" +s.getInetAddress().getHostAddress() + "]:" + s.getPort() + " ";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Implementation of runnable. This method allow to serve one client.
|
/** Implementation of runnable. This method allow to serve one client.
|
||||||
@ -90,11 +91,11 @@ public class TrackerManagementTCP implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
boolean end = false;
|
boolean end = false;
|
||||||
logger.writeTCP(addr + "New connection", LogLevel.Action);
|
logger.writeTCP("[ " + addr + "] New connection", LogLevel.Action);
|
||||||
do {
|
do {
|
||||||
end = handleRequest();
|
end = handleRequest();
|
||||||
} while(!end);
|
} while(!end);
|
||||||
logger.writeTCP(addr + "End of connection", LogLevel.Action);
|
logger.writeTCP("[ " + addr + "] End of connection", LogLevel.Action);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Respond to next request incomming on socket s.
|
/** Respond to next request incomming on socket s.
|
||||||
@ -103,7 +104,7 @@ public class TrackerManagementTCP implements Runnable {
|
|||||||
*/
|
*/
|
||||||
private boolean handleRequest() {
|
private boolean handleRequest() {
|
||||||
try {
|
try {
|
||||||
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s);
|
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket());
|
||||||
Payload p = pd.getPayload();
|
Payload p = pd.getPayload();
|
||||||
switch (p.getRequestResponseCode()) {
|
switch (p.getRequestResponseCode()) {
|
||||||
case LOAD_REQUEST:
|
case LOAD_REQUEST:
|
||||||
|
@ -22,6 +22,8 @@ import protocolP2P.FileList;
|
|||||||
import protocolP2P.HashRequest;
|
import protocolP2P.HashRequest;
|
||||||
import localException.InternalError;
|
import localException.InternalError;
|
||||||
import remoteException.EmptyDirectory;
|
import remoteException.EmptyDirectory;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
/** Tracker management implementation with udp
|
/** Tracker management implementation with udp
|
||||||
* @author Louis Royer
|
* @author Louis Royer
|
||||||
@ -31,7 +33,7 @@ import remoteException.EmptyDirectory;
|
|||||||
*/
|
*/
|
||||||
public class TrackerManagementUDP implements Runnable {
|
public class TrackerManagementUDP implements Runnable {
|
||||||
|
|
||||||
private int port;
|
private HostItem tracker;
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
private DatagramSocket socket;
|
private DatagramSocket socket;
|
||||||
private List<HostItem> hostList = new ArrayList<>();
|
private List<HostItem> hostList = new ArrayList<>();
|
||||||
@ -42,10 +44,10 @@ public class TrackerManagementUDP implements Runnable {
|
|||||||
* @param logger Logger object
|
* @param logger Logger object
|
||||||
*/
|
*/
|
||||||
public TrackerManagementUDP(int port, Logger logger) {
|
public TrackerManagementUDP(int port, Logger logger) {
|
||||||
this.port = port;
|
tracker = new HostItem("localhost", port);
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
try {
|
try {
|
||||||
socket = new DatagramSocket(port);
|
socket = new DatagramSocket(tracker.getPort(), tracker.getInetAddress());
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
logger.writeUDP("Error: cannot listen on port " + port, LogLevel.Error);
|
logger.writeUDP("Error: cannot listen on port " + port, LogLevel.Error);
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
|
Loading…
Reference in New Issue
Block a user