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:MainClass.java

public static void main(String[] args) throws Exception {

    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    ServerSocket ss = ssf.createServerSocket(PORT);
    Socket s = ss.accept();
    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    String line = null;/*from  www.ja va 2  s.c om*/
    while (((line = in.readLine()) != null)) {
        System.out.println(line);
    }
    in.close();
    s.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    char[] passphrase = "password".toCharArray();
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(new FileInputStream(".keystore"), passphrase);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(keystore, passphrase);/* www.j a  v a 2 s. c om*/
    SSLContext context = SSLContext.getInstance("TLS");
    KeyManager[] keyManagers = kmf.getKeyManagers();

    context.init(keyManagers, null, null);

    SSLServerSocketFactory ssf = context.getServerSocketFactory();
    ServerSocket ss = ssf.createServerSocket(PORT);

    Socket s = ss.accept();

    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

    String line = null;
    while (((line = in.readLine()) != null)) {
        System.out.println(line);
    }
    in.close();
    s.close();
}

From source file:SSLSimpleClient.java

public static void main(String[] args) throws Exception {
    SocketFactory sf = SSLSocketFactory.getDefault();
    Socket s = sf.createSocket(args[0], Integer.parseInt(args[1]));

    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    PrintWriter pw = new PrintWriter(s.getOutputStream());
    pw.println("from java2s.");
    pw.flush();//from www .  ja  v  a2  s .c om
    System.out.println(br.readLine());
    s.close();
}

From source file:Whois.java

License:asdf

public static void main(String args[]) throws Exception {
    int c;/*  w ww  .  ja v  a 2s.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();
}

From source file:SquareClient.java

public static void main(String args[]) throws Exception {
    String server = args[0];//from w w  w  .ja  va2 s  .co  m
    int port = Integer.parseInt(args[1]);
    double value = Double.valueOf(args[2]).doubleValue();

    Socket s = new Socket(server, port);
    OutputStream os = s.getOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeDouble(value);

    InputStream is = s.getInputStream();
    DataInputStream dis = new DataInputStream(is);
    value = dis.readDouble();

    System.out.println(value);
    s.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();/*from   ww w. j  ava 2s  .  co m*/
    if (str.equals("Hello"))
        System.out.println("Alive!");
    else
        System.out.println("Dead");
    t.close();
}

From source file:SquareServer.java

public static void main(String args[]) throws Exception {
    int port = Integer.parseInt(args[0]);
    ServerSocket ss = new ServerSocket(port);
    while (true) {
        Socket s = ss.accept();
        InputStream is = s.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        double value = dis.readDouble();
        value *= value;/*from w w w. ja  v a 2s .  c  o m*/

        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeDouble(value);

        s.close();
    }
}

From source file:Main.java

  public static void main(String[] args) throws IOException {
  ServerSocket servsock = new ServerSocket(123456);
  File myFile = new File("s.pdf");
  while (true) {/*from  ww w.jav a 2  s. c om*/
    Socket sock = servsock.accept();
    byte[] mybytearray = new byte[(int) myFile.length()];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
    bis.read(mybytearray, 0, mybytearray.length);
    OutputStream os = sock.getOutputStream();
    os.write(mybytearray, 0, mybytearray.length);
    os.flush();
    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 ww  w .j a  v a 2 s. c o  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  ww w  .  ja  v  a 2  s .  c  om
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

    s.setReceiveBufferSize(1);

    s.close();
}