Example usage for java.net Socket close

List of usage examples for java.net Socket close

Introduction

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

Prototype

public synchronized void close() throws IOException 

Source Link

Document

Closes this socket.

Usage

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  ww .  j  a va  2 s  .  c o m
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

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

    s.close();
}

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. j  a  v a  2 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: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  www  .j  a  v  a  2 s .co  m
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

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

    s.close();
}

From source file:SocketTest.java

public static void main(String[] args) {
    try {/*  w  w w  .  j a  va  2 s  .  c o  m*/
        Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
        try {
            InputStream inStream = s.getInputStream();
            Scanner in = new Scanner(inStream);

            while (in.hasNextLine()) {
                String line = in.nextLine();
                System.out.println(line);
            }
        } finally {
            s.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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  2s  . co 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);/* www.jav a  2  s.  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:lookForPorts3.java

public static void main(String[] args) {

    Socket theSocket;
    String host = "localhost";

    if (args.length > 0) {
        host = args[0];/*from  w ww  .j  av  a2s.  c  o m*/
    }

    try {
        InetAddress theAddress = InetAddress.getByName(host);
        for (int i = 1; i <= 65535; i++) {
            try {
                theSocket = new Socket(host, i);
                System.out.println("There is a server on port " + i + " of " + host);
                theSocket.close();
            } catch (IOException e) {
                // must not be a server on this port
            }
        }
    } catch (UnknownHostException e) {
        System.err.println(e);
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
        Socket sock = new Socket("127.0.0.1", 123456);
        byte[] mybytearray = new byte[1024];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new FileOutputStream("s.pdf");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int bytesRead = is.read(mybytearray, 0, mybytearray.length);
        bos.write(mybytearray, 0, bytesRead);
        bos.close();//from   ww w  .jav  a  2 s  . c o m
        sock.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 ww .j a  v  a  2  s .  c o  m
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

    SocketChannel socketChannel = s.getChannel();
    System.out.println(socketChannel.getLocalAddress());

    s.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    Socket s = sf.createSocket(HOST, PORT);
    OutputStream out = s.getOutputStream();
    out.write("\nConnection established.\n\n".getBytes());
    out.flush();//from  w  w  w . jav  a  2  s  .c  om
    int theCharacter = 0;
    theCharacter = System.in.read();
    while (theCharacter != '~') // The '~' is an escape character to exit
    {
        out.write(theCharacter);
        out.flush();
        theCharacter = System.in.read();
    }

    out.close();
    s.close();
}