Example usage for java.net Socket Socket

List of usage examples for java.net Socket Socket

Introduction

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

Prototype

public Socket(InetAddress address, int port) throws IOException 

Source Link

Document

Creates a stream socket and connects it to the specified port number at the specified IP address.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    InetAddress addr;// ww  w .  j a  va  2  s. 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[] argv) throws Exception {
    Socket t = new Socket("127.0.0.1", 7);
    DataInputStream dis = new DataInputStream(t.getInputStream());
    PrintStream ps = new PrintStream(t.getOutputStream());
    ps.println("Hello");
    String str = dis.readUTF();/* w  w  w  .  jav  a 2  s  . c  om*/
    if (str.equals("Hello"))
        System.out.println("Alive!");
    else
        System.out.println("Dead");
    t.close();
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    Socket s = new Socket("internic.net", 43);
    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();
    String str = "asdfasdfasdf\n";
    byte buf[] = str.getBytes();
    out.write(buf);/* ww  w. j a  v  a 2 s  .  co m*/
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

    s.setReceiveBufferSize(1);

    s.close();
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    Socket s = new Socket("internic.net", 43);
    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();
    String str = "asdfasdfasdf\n";
    byte buf[] = str.getBytes();
    out.write(buf);/*from w  w  w.j a  va2  s.  co m*/
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

    s.sendUrgentData(1);

    s.close();
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    Socket s = new Socket("internic.net", 43);
    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();
    String str = "asdfasdfasdf\n";
    byte buf[] = str.getBytes();
    out.write(buf);/*from   w  w w. ja v  a2s  .  c om*/
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

    System.out.println(s.toString());

    s.close();
}

From source file:WebPing.java

public static void main(String[] args) {
    try {/*from   w  w w  .j a v a2  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:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    Socket s = new Socket("internic.net", 43);
    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();
    String str = "asdfasdfasdf\n";
    byte buf[] = str.getBytes();
    out.write(buf);//ww w .jav  a2s .  c o m
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

    s.setTrafficClass(1);
    System.out.println(s.getTrafficClass());

    s.close();
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    Socket s = new Socket("internic.net", 43);
    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();
    String str = "asdfasdfasdf\n";
    byte buf[] = str.getBytes();
    out.write(buf);//from   w  w  w  . j a v  a2  s.c o m
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

    s.setOOBInline(true);
    System.out.println(s.getOOBInline());

    s.close();
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    Socket s = new Socket("internic.net", 43);
    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();
    String str = "asdfasdfasdf\n";
    byte buf[] = str.getBytes();
    out.write(buf);// w  ww.  ja  v a 2  s.c  o m
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

    s.shutdownInput();
    s.shutdownOutput();

    s.close();
}

From source file:Whois.java

License:asdf

public static void main(String args[]) throws Exception {
    int c;//w  w  w . j  av a  2  s  .  c o m
    Socket s = new Socket("internic.net", 43);
    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();
    String str = "asdfasdfasdf\n";
    byte buf[] = str.getBytes();
    out.write(buf);
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }
    s.close();
}