Example usage for java.net InetSocketAddress InetSocketAddress

List of usage examples for java.net InetSocketAddress InetSocketAddress

Introduction

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

Prototype

private InetSocketAddress(int port, String hostname) 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    InetSocketAddress addr1 = new InetSocketAddress("::1", 12345);
    printSocketAddress(addr1);//from ww w.j a v  a2 s.c  om

    InetSocketAddress addr2 = InetSocketAddress.createUnresolved("::1", 12881);
    printSocketAddress(addr2);
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    String host = "localhost";
    int port = 80;

    InetSocketAddress addr = new InetSocketAddress(host, port);
    SocketChannel sc = SocketChannel.open();

    sc.configureBlocking(false);//from w  ww.j av a 2s .c  om

    System.out.println("initiating connection");

    sc.connect(addr);

    while (!sc.finishConnect()) {
        System.out.println("doing something useless");
    }

    System.out.println("connection established");

    sc.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SocketChannel sChannel = SocketChannel.open();
    sChannel.configureBlocking(false);//from w w  w  . j a v a  2 s . com

    sChannel.connect(new InetSocketAddress("hostName", 12345));

    while (!sChannel.finishConnect()) {
        // Do something else
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int port = 80;

    Socket socket = new Socket("google.com", port, InetAddress.getByName("java2s.com"), port);

    socket.bind(new InetSocketAddress("java2s.com", port));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SocketChannel sChannel = SocketChannel.open();
    sChannel.configureBlocking(false);//  w ww . j a  va  2  s  . c o m
    sChannel.connect(new InetSocketAddress("hostName", 12345));

    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
    buf.put((byte) 0xFF);

    buf.flip();

    int numBytesWritten = sChannel.write(buf);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    sock.connect(sockaddr);// www  .j a v a2 s  . c  o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    int timeoutMs = 2000;
    sock.connect(sockaddr, timeoutMs);/* w  ww  .ja va 2 s .  c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    int timeoutMs = 2000; // 2 seconds
    sock.connect(sockaddr, timeoutMs);/*from  ww w .j  a v a 2 s .  c o  m*/
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    int port = 1919;

    SocketAddress address = new InetSocketAddress("127.0.0.1", port);
    SocketChannel client = SocketChannel.open(address);
    ByteBuffer buffer = ByteBuffer.allocate(4);
    IntBuffer view = buffer.asIntBuffer();

    for (int expected = 0;; expected++) {
        client.read(buffer);//from  ww w  . j a  v  a  2 s .  c o m
        int actual = view.get();
        buffer.clear();
        view.rewind();

        if (actual != expected) {
            System.err.println("Expected " + expected + "; was " + actual);
            break;
        }
        System.out.println(actual);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create an unbound server socket
    ServerSocket serverSocket = new ServerSocket();

    // Create a socket address object
    InetSocketAddress endPoint = new InetSocketAddress("localhost", 12900);

    // Set the wait queue size to 100
    int waitQueueSize = 100;

    // Bind the server socket to localhost and at port 12900 with
    // a wait queue size of 100
    serverSocket.bind(endPoint, waitQueueSize);

}