Get IP address from NetworkInterface and create server socket
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
public class Main {
static public void main(String args[]) throws Exception {
int port = 80;
NetworkInterface ni = NetworkInterface.getByName("name");
Enumeration e = ni.getInetAddresses();
if (!e.hasMoreElements())
return;
InetAddress ia = (InetAddress) e.nextElement();
ServerSocket ss = new ServerSocket(port, 20, ia);
System.out.println("Listening");
Socket s = ss.accept();
System.out.println(s);
}
}
Related examples in the same category