Example usage for java.net Socket getLocalAddress

List of usage examples for java.net Socket getLocalAddress

Introduction

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

Prototype

public InetAddress getLocalAddress() 

Source Link

Document

Gets the local address to which the socket is bound.

Usage

From source file:Main.java

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

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

    client.close();//from  www .  j av  a2s.  c  o m
}

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  ww  w  .  jav a2  s. c om*/
            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 .  java2s  .  c  o  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:org.sikuliserver.QueueReceiver.java

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

    int port = 9001;
    if (args.length > 1 && args[0].equals("-p")) {
        port = Integer.valueOf(args[1]);
    }//  www  .  j  av a 2  s  .  c om

    System.out.println("[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())
            + "] Launching a SikuliServer on port : " + port);
    ServerSocket serverSocket = new ServerSocket(port);

    System.out.println("[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())
            + "] Server Started and Listening...");

    try {
        while (true) {
            Socket clientSocket = serverSocket.accept();
            System.out.println("[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())
                    + "] Connected to the clientSocket " + clientSocket.getLocalAddress().getHostName());

            BufferedReader is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintStream os = new PrintStream(clientSocket.getOutputStream());
            String line = "";

            StringBuilder sb = new StringBuilder();
            while (!(line = is.readLine()).equals("|ENDS|")) {
                sb.append(line);
            }
            System.out.println("[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())
                    + "] Received :" + sb.toString());
            JSONObject obj = new JSONObject(sb.toString());

            String action = obj.getString("action");
            String picture = obj.getString("picture");
            String text = obj.getString("text");
            String start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
            System.out.println("[" + start + "] Starting to perform :" + sb.toString());

            if (action.equals("click") || action.equals("wait")) {
                URL url = new URL("file:///" + picture);
                InputStream istream = url.openStream();
                //byte[] bytes = IOUtils.toByteArray(istream);
                //String imageDataString = Base64.encodeBase64URLSafeString(bytes);

                //byte[] data = Base64.decodeBase64(imageDataString);
                try (OutputStream stream = new FileOutputStream("sikuliPicture.png")) {
                    //stream.write(data);
                    stream.write(IOUtils.toByteArray(istream));
                }
            }

            SikuliAction sikuliAction = new SikuliAction();
            sikuliAction.doAction(action, picture, text);

            String end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
            System.out.println("[" + end + "] End of action :" + action + "(" + picture + ")");
            os.println("[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + "] Done");
            os.println("|ENDR|");
        }
    } catch (UnknownHostException e) {
        System.out.println("Unknown host: kq6py");
    } catch (IOException e) {
        System.out.println("No I/O");
    } catch (FindFailed ex) {
        Logger.getLogger(QueueReceiver.class.getName()).log(Level.SEVERE, null, ex);
    } catch (JSONException ex) {
        Logger.getLogger(QueueReceiver.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Main.java

public static String getSocketLocalIpAddress() {
    String ipAddress = "";
    Socket socket;
    try {//w  w w.  ja v a  2  s  .c  om
        socket = new Socket("www.google.com", 80);
        ipAddress = socket.getLocalAddress().toString();
        ipAddress = ipAddress.replace("/", "");
        Log.i(TAG, "IP by socket: " + ipAddress);
        socket.close();
        return ipAddress;
    } catch (Exception e) {
        Log.i(TAG, e.getMessage());
    }
    return ipAddress;
}

From source file:com.mirth.connect.connectors.tcp.SocketUtil.java

public static String getLocalAddress(Socket socket) {
    String localAddress = socket == null || socket.getLocalAddress() == null ? ""
            : socket.getLocalAddress().toString() + ":" + socket.getLocalPort();

    // If addresses begin with a slash "/", remove it.
    if (localAddress.startsWith("/")) {
        localAddress = localAddress.substring(1);
    }/*w  w  w .  j  a v a  2 s.c  o m*/

    return localAddress;
}

From source file:Main.java

/**
 * Renders the details of a socket in the returned string
 * @param socket The socket to render/* w w w.j  a  v a  2s .c om*/
 * @return the details of the socket as a string
 */
public static String render(Socket socket) {
    if (socket == null)
        return "NULL";
    StringBuilder b = new StringBuilder("\nSocket [");
    b.append("\n\tLocalPort:").append(socket.getLocalPort());
    b.append("\n\tLocalAddress:").append(socket.getLocalAddress());
    b.append("\n\tLocalSocketAddress:").append(socket.getLocalSocketAddress());
    b.append("\n\tRemotePort:").append(socket.getPort());
    b.append("\n\tRemoteAddress:").append(socket.getInetAddress());
    b.append("\n\tRemoteSocketAddress:").append(socket.getRemoteSocketAddress());
    b.append("\n\tChannel:").append(socket.getChannel());
    b.append("\n\tHashCode:").append(socket.hashCode());
    b.append("\n]");
    return b.toString();
}

From source file:net.grinder.util.NetworkUtil.java

private static InetAddress getAddressWithSocket(String byConnecting, int port) {
    Socket s = null;
    try {/*  ww w .  j  a  v a 2s  . co m*/
        s = new Socket();
        SocketAddress addr = new InetSocketAddress(byConnecting, port);
        s.connect(addr, 2000); // 2 seconds timeout
        return s.getLocalAddress();
    } catch (Exception e) {
        return null;
    } finally {
        IOUtils.closeQuietly(s);
    }
}

From source file:org.devtcg.five.server.UPnPService.java

/**
 * Determine the source IP (our IP) of our route to www.google.com:80. If
 * the user has layers of indirection to access the Internet, it is our
 * assumption that this function will return an address in the RFC1918
 * private space; otherwise, no IGD mappings are necessary.
 *
 * @throws IOException//from   w  w  w.  jav a 2 s. c  om
 */
private static InetAddress determineDefaultRouteLocalAddress() throws IOException {
    Socket socket = new Socket();
    try {
        socket.connect(new InetSocketAddress("www.google.com", 80));
        return socket.getLocalAddress();
    } finally {
        socket.close();
    }
}

From source file:net.grinder.util.NetworkUtils.java

public static InetAddress getAddressWithSocket(String byConnecting, int port) {
    Socket s = new Socket();
    try {/*w ww  .j a va  2 s .co  m*/
        if (tryConnection(byConnecting, port, s)) {
            return s.getLocalAddress();
        }
    } finally {
        IOUtils.closeQuietly(s);
    }
    return null;
}