Example usage for java.net Socket getRemoteSocketAddress

List of usage examples for java.net Socket getRemoteSocketAddress

Introduction

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

Prototype

public SocketAddress getRemoteSocketAddress() 

Source Link

Document

Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

Usage

From source file:Main.java

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

    System.out.println("Just connected to " + client.getRemoteSocketAddress());

    client.close();//from w  w w .  j ava2s  .  c  o  m
}

From source file:Main.java

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

    System.out.println("Just connected to " + client.getRemoteSocketAddress());

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    String serverName = args[0];/*from  w ww .java 2s.com*/
    int port = Integer.parseInt(args[1]);

    try {
        System.out.println("Connecting to " + serverName + " on port " + port);
        Socket client = new Socket(serverName, port);

        System.out.println("Just connected to " + client.getRemoteSocketAddress());

        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        out.writeUTF("Hello from " + client.getLocalSocketAddress());

        InputStream inFromServer = client.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        System.out.println("Server says " + in.readUTF());

        client.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:paxos.GameServer.java

public static void main(String args[]) {
    Game myGame = new Game();
    Socket socket = null;
    ServerSocket GameServer = null;
    System.out.println("Server Listening......");
    try {//ww w.  java2 s. c o m
        GameServer = new ServerSocket(port);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Server error");
    }

    while (true) {
        try {
            socket = GameServer.accept();
            String clientAddress = socket.getRemoteSocketAddress().toString();
            int clientPort = socket.getPort();
            System.out.println("Connection established. Client: " + clientAddress + "on port " + clientPort);

            ServerThread st = new ServerThread(socket, myGame);
            st.start();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Connection Error");
        }
    }
}

From source file:fitnesse.FitNesseExpediter.java

public static LogData makeLogData(Socket socket, Request request, Response response) {
    return new LogData(((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress().getHostAddress(),
            new GregorianCalendar(), request.getRequestLine(), response.getStatus(), response.getContentSize(),
            request.getAuthorizationUsername());
}

From source file:pt.minha.calibration.Calibrator.java

private static void runServer() throws Exception {
    // Reality server
    ServerSocket ss = new ServerSocket(12345);

    logger.info("server: started at {}", ss.getLocalSocketAddress());

    while (true) {
        Socket s = ss.accept();

        logger.info("server: accepted {}", s.getRemoteSocketAddress());

        try {//  w w  w  . j  a v  a2s.  c  o  m
            ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
            oos.flush();
            ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(s.getInputStream()));

            while (!s.isClosed()) {
                Map<String, Object> p = (Map<String, Object>) ois.readObject();
                Benchmark next = (Benchmark) Class.forName((String) p.get("bench")).newInstance();
                next.setParameters(p);

                logger.info("server: running {}", p);

                Object result = next.server();

                logger.info("server: running {} done", p);

                oos.writeObject(result);
                oos.flush();
            }

            logger.info("server: disconnected {}", s.getRemoteSocketAddress());

        } catch (IOException ioe) {
            logger.info("server: disconnected {} on {}", s.getRemoteSocketAddress(), ioe);
        }
    }
}

From source file:Main.java

/**
 * Renders the details of a socket in the returned string
 * @param socket The socket to render//from   w w w . ja v a 2 s.  co  m
 * @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:gridool.util.net.SocketUtils.java

public static boolean write(final Socket socket, final byte[] b, final long delay, final int maxRetry)
        throws IOException {
    final OutputStream sockout = socket.getOutputStream();
    for (int i = 0; i < maxRetry; i++) {
        try {/*from   ww  w .  j  a  va 2  s. c o m*/
            sockout.write(b);
            sockout.flush();
            return true;
        } catch (IOException e) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Failed to write to socket: " + socket.getRemoteSocketAddress() + " #" + i);
            }
        }
        if (delay > 0) {
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ie) {
                ;
            }
        }
    }
    if (LOG.isWarnEnabled()) {
        LOG.warn("Failed to write to socket: " + socket.getRemoteSocketAddress());
    }
    return false;
}

From source file:Main.java

public void run() {
    while (true) {
        try {/*from  w ww. j av  a  2 s  .  c om*/
            System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
            Socket client = serverSocket.accept();

            System.out.println("Just connected to " + client.getRemoteSocketAddress());
            client.close();
        } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
}

From source file:Main.java

public void run() {
    while (true) {
        try {/*from w  ww  . j  a  v  a  2 s.  c o  m*/
            System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();

            System.out.println("Just connected to " + server.getRemoteSocketAddress());
            DataInputStream in = new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());

            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");

            server.close();
        } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
}