Example usage for java.net Socket getInetAddress

List of usage examples for java.net Socket getInetAddress

Introduction

In this page you can find the example usage for java.net Socket getInetAddress.

Prototype

public InetAddress getInetAddress() 

Source Link

Document

Returns the address to which the socket is connected.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    InetAddress addr;//  ww  w.  j  a v  a 2s .  c  o m
    Socket sock = new Socket(args[0], 80);
    addr = sock.getInetAddress();
    System.out.println("Connected to " + addr);
    sock.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ServerSocket server = new ServerSocket(8123);
    while (true) {
        Socket sock = server.accept();
        InetAddress addr = sock.getInetAddress();
        System.out.println("Connection made to " + addr.getHostName() + " (" + addr.getHostAddress() + ")");
        Thread.sleep(5000);//from   w w  w  .  j  a v  a2  s.  c  om
        sock.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket client = new Socket("google.com", 80);

    System.out.println(client.getInetAddress());

    client.close();/*from ww  w  . j a v a 2s  .  c  o m*/
}

From source file:WebPing.java

public static void main(String[] args) {
    try {/*from  w ww . jav  a  2  s.c  o m*/
        InetAddress addr;
        Socket sock = new Socket("www.java2s.com", 80);
        addr = sock.getInetAddress();
        System.out.println("Connected to " + addr);
        sock.close();
    } catch (java.io.IOException e) {
        System.out.println("Can't connect to " + args[0]);
        System.out.println(e);
    }
}

From source file:SocketDemo.java

public static void main(String[] args) {
    try {//  w ww .ja v  a2s .c  o  m
        ServerSocket server = new ServerSocket(6123);
        while (true) {
            System.out.println("Listening");
            Socket sock = server.accept();
            InetAddress addr = sock.getInetAddress();
            System.out.println("Connection made to " + addr.getHostName() + " (" + addr.getHostAddress() + ")");
            pause(5000);
            sock.close();
        }
    } catch (IOException e) {
        System.out.println("Exception detected: " + e);
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    int port = 2345;
    ServerSocket ss = new ServerSocket(port);
    while (true) {
        try {//from   w  ww. ja va 2s  . c o m
            Socket s = ss.accept();

            String response = "Hello " + s.getInetAddress() + " on port " + s.getPort() + "\r\n";
            response += "This is " + s.getLocalAddress() + " on port " + s.getLocalPort() + "\r\n";
            OutputStream out = s.getOutputStream();
            out.write(response.getBytes("US-ASCII"));
            out.flush();
            s.close();
        } catch (IOException ex) {
        }
    }
}

From source file:getSocketInfo.java

public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {
        try {/*from w ww .  j  a  va  2  s  .  co  m*/
            Socket theSocket = new Socket(args[i], 80);
            System.out.println("Connected to " + theSocket.getInetAddress() + " on port " + theSocket.getPort()
                    + " from port " + theSocket.getLocalPort() + " of " + theSocket.getLocalAddress());
        } // end try
        catch (UnknownHostException e) {
            System.err.println("I can't find " + args[i]);
        } catch (SocketException e) {
            System.err.println("Could not connect to " + args[i]);
        } catch (IOException e) {
            System.err.println(e);
        }
    }
}

From source file:flink.iso8583.example.Server.java

public static void main(String[] args) throws Exception {
    mfact = ConfigParser.createFromClasspathConfig("flink/iso8583/example/config.xml");
    log.info("Setting up server socket...");
    ServerSocket server = new ServerSocket(9999);
    log.info("Waiting for connections...");
    while (true) {
        Socket sock = server.accept();
        log.info("New connection from " + sock.getInetAddress() + ":" + sock.getPort());
        new Server(sock).start();
    }/*w w w .  j a  v a  2  s  .c om*/
}

From source file:j8583.example.Server.java

public static void main(String[] args) throws Exception {
    mfact = ConfigParser.createFromClasspathConfig("j8583/example/config.xml");
    System.err.println("Setting up server socket...");
    ServerSocket server = new ServerSocket(9999);
    System.err.println("Waiting for connections...");
    while (true) {
        Socket sock = server.accept();
        System.err.println(String.format("New connection from %s:%d", sock.getInetAddress(), sock.getPort()));
        new Thread(new Server(sock), "j8583-server").start();
    }// w  ww .  ja  v  a2  s . co  m
}

From source file:gomokuserver.GomokuServer.java

/**
 * @param args the command line arguments
 * @throws java.lang.InterruptedException
 *///from  w w w.jav  a 2s.  c om
public static void main(String[] args) throws InterruptedException {
    if (args.length != 1) {
        System.out.println("Run with listen portno as argument");
        return;
    }

    int portno = Integer.parseInt(args[0]);
    ServerSocket sc;
    try {
        System.out.print("Creating socket bound to " + portno + " ... ");
        sc = new ServerSocket(portno);

        System.out.println("DONE");
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
        return;
    }
    FrontOffice fo = new FrontOffice();
    Lobby lb = new Lobby();
    fo.setReferalLobby(lb);
    fo.start();
    lb.start();
    while (!interrupted()) {
        try {
            Socket playersocket = sc.accept();
            playersocket.setKeepAlive(true);
            System.out.println("Received client from " + playersocket.getInetAddress());
            fo.handle(playersocket);
        } catch (IOException ex) {
            Logger.getLogger(GomokuServer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    fo.interrupt();
    lb.interrupt();
    fo.join();
    lb.join();
}