Skip to content
Snippets Groups Projects
SocketDetails.java 1.06 KiB
Newer Older
am13g23's avatar
am13g23 committed
import java.net.InetAddress;
import java.net.Socket;

public class SocketDetails{ //class to store details of the listen sockets of active users
    private InetAddress ip;
    private int port;

    public SocketDetails(Socket socket, int passPort){ //on construction set the IP and port number
        ip = socket.getInetAddress();
        port = passPort;
    }

    public InetAddress getIp() { //returns the ip address
        return ip;
    }

    public int getPort() { //returns the port number
        return port;
    }

    public Socket getSocket() throws Exception{ //opens a socket back to the user and returns that
        return new Socket(ip, port);
    }

    @Override
    public int hashCode() { //hash code function so two objects with the same port and host address have the same hash
        return (ip.getHostAddress() + port).hashCode();
    }

    @Override
    public boolean equals(Object obj) { //equals function that checks that the ips and ports match
        return ((SocketDetails)obj).getIp().equals(ip) && ((SocketDetails)obj).getPort() == port;
    }
}