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

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    while (true) {
        System.out.println("Listening");
        Socket sock = ssock.accept();

        DataOutputStream dstream = new DataOutputStream(sock.getOutputStream());
        dstream.writeFloat(3.14159265f);
        dstream.close();//from  w  w  w  .  ja va 2  s.c om
        sock.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], 1234);
    DataInputStream dis = new DataInputStream(sock.getInputStream());
    float f = dis.readFloat();
    System.out.println("PI=" + f);
    dis.close();/*from   ww  w.  ja v  a2  s .co m*/
    sock.close();
}

From source file:ObjServer.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    Hashtable hash = new Hashtable();
    hash.put("Java Source and Support", "www.java2s.com");

    while (true) {
        System.out.println("Listening");
        Socket sock = ssock.accept();

        ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream());
        ostream.writeObject(hash);/* ww w  . j  a va2  s  .co m*/
        ostream.close();
        sock.close();
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    String host = "localhost";

    try {/*from   w  w w.  j  a v  a  2s .c  o  m*/
        InetAddress theAddress = InetAddress.getByName(host);
        for (int i = 1; i < 65536; i++) {
            Socket connection = null;
            connection = new Socket(host, i);
            System.out.println("There is a server on port " + i + " of " + host);
            if (connection != null)
                connection.close();
        } // end for
    } catch (Exception ex) {
        System.err.println(ex);
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ServerSocket server = new ServerSocket(8123);
    while (true) {
        Socket sock = server.accept();
        InetAddress addr = sock.getInetAddress();
        System.out.println("Connection made to " + addr.getHostName() + " (" + addr.getHostAddress() + ")");
        Thread.sleep(5000);/*  ww w.  jav a 2 s . co  m*/
        sock.close();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    Socket sock = ssock.accept();
    ssock.close();//from w w w.  j  a v  a2  s.  co  m

    PrintStream pstream = new PrintStream(sock.getOutputStream());
    for (int i = 100; i >= 0; i--) {
        pstream.println(i);
    }
    pstream.close();
    sock.close();
}

From source file:ObjServer.java

public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], 1234);
    ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
    Hashtable hash = (Hashtable) ois.readObject();
    System.out.println(hash);//from w  w w . j a  va 2s .co m
    ois.close();
    sock.close();
}

From source file:Main.java

public static void main(String args[]) {
    try {//from   w w w .  ja  va2 s .co m

        int port = 5555;
        ServerSocket ss = new ServerSocket(port);

        while (true) {
            // Accept incoming requests
            Socket s = ss.accept();

            // Write result to client
            OutputStream os = s.getOutputStream();
            DataOutputStream dos = new DataOutputStream(os);
            dos.writeInt(100);

            s.close();
        }
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:SecureServer.java

public static void main(String[] args) throws Exception {
    ServerSocketFactory ssf = SSLServerSocketFactory.getDefault();
    SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(98999);
    Socket sock = ss.accept();
    ss.close();//from w  w w  .  j a va 2  s. co m
    OutputStream rawOut = sock.getOutputStream();
    PrintWriter out = new PrintWriter(new OutputStreamWriter(rawOut));
    out.println(new java.util.Date().toString());
    out.flush();
    sock.close();

}

From source file:SocketDemo.java

public static void main(String[] args) {
    try {/*w ww . j av a  2 s  .c  om*/
        ServerSocket server = new ServerSocket(6123);
        while (true) {
            System.out.println("Listening");
            Socket sock = server.accept();
            InetAddress addr = sock.getInetAddress();
            System.out.println("Connection made to " + addr.getHostName() + " (" + addr.getHostAddress() + ")");
            pause(5000);
            sock.close();
        }
    } catch (IOException e) {
        System.out.println("Exception detected: " + e);
    }
}