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);
    Socket sock = ssock.accept();
    ssock.close();// w  ww.  j  a  va 2  s  .c om

    PrintStream pstream = new PrintStream(sock.getOutputStream());
    pstream.print("count? ");
    BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    String line = input.readLine();
    pstream.println("");
    int count = Integer.parseInt(line);
    for (int i = count; i >= 0; i--) {
        pstream.println(i);
    }
    pstream.close();
    sock.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], Integer.parseInt(args[1]));
    GZIPOutputStream zip = new GZIPOutputStream(sock.getOutputStream());
    String line;/*from ww w  .  j  av a  2 s . c o m*/
    BufferedReader bis = new BufferedReader(new FileReader(args[2]));
    while (true) {
        line = bis.readLine();
        if (line == null)
            break;
        line = line + "\n";
        zip.write(line.getBytes(), 0, line.length());
    }
    zip.finish();
    zip.close();
    sock.close();
}

From source file:SimpleSocketServer.java

  public static void main(String args[]) throws Exception {
  ServerSocket serverSocket;/*from ww  w .  j a va2s.co  m*/
  int portNumber = 1777;
  Socket socket;
  String str;

  str = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  str += "<ticketRequest><customer custID=\"1\">";
  str += "</ticketRequest>";

  serverSocket = new ServerSocket(portNumber);

  System.out.println("Waiting for a connection on " + portNumber);

  socket = serverSocket.accept();

  ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

  oos.writeObject(str);

  oos.close();

  socket.close();

}

From source file:MainClass.java

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

    char[] passphrase = "sasquatch".toCharArray();
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(new FileInputStream(".keystore"), passphrase);

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
    tmf.init(keystore);//from   ww w.ja  v  a 2s.c o m

    SSLContext context = SSLContext.getInstance("TLS");
    TrustManager[] trustManagers = tmf.getTrustManagers();

    context.init(null, trustManagers, null);

    SSLSocketFactory sf = context.getSocketFactory();

    Socket s = sf.createSocket(HOST, PORT);
    OutputStream out = s.getOutputStream();
    out.write("\nConnection established.\n\n".getBytes());

    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();
}

From source file:Finger.java

public static void main(String[] arguments) throws Exception {
    StringTokenizer split = new StringTokenizer(arguments[0], "@");
    String user = split.nextToken();
    String host = split.nextToken();

    Socket digit = new Socket(host, 79);
    digit.setSoTimeout(20000);/*from   ww  w.  j av a2 s.com*/
    PrintStream out = new PrintStream(digit.getOutputStream());
    out.print(user + "\015\012");
    BufferedReader in = new BufferedReader(new InputStreamReader(digit.getInputStream()));
    boolean eof = false;
    while (!eof) {
        String line = in.readLine();
        if (line != null)
            System.out.println(line);
        else
            eof = true;
    }
    digit.close();
}

From source file:ConnectMethodExampleForProxyClient.java

public static void main(String args[]) {

    ProxyClient client = new ProxyClient();
    client.getParams().setParameter("http.useragent", "Proxy Test Client");

    client.getHostConfiguration().setHost("www.somehost.com");
    client.getHostConfiguration().setProxy("localproxyaddress", 80);

    Socket socket = null;

    try {//from  w  w  w. ja v a 2 s .c om
        ConnectResponse response = client.connect();
        socket = response.getSocket();
        if (socket == null) {
            ConnectMethod method = response.getConnectMethod();
            System.err.println("Socket not created: " + method.getStatusLine());
        }
        // do something
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        if (socket != null)
            try {
                socket.close();
            } catch (Exception fe) {
            }
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String serverName = args[0];//from ww w  .  jav a2s.c  o m
    int port = Integer.parseInt(args[1]);

    try {
        System.out.println("Connecting to " + serverName + " on port " + port);
        Socket client = new Socket(serverName, port);

        System.out.println("Just connected to " + client.getRemoteSocketAddress());

        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        out.writeUTF("Hello from " + client.getLocalSocketAddress());

        InputStream inFromServer = client.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        System.out.println("Server says " + in.readUTF());

        client.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:BufferedSocketClient.java

  public static void main(String args[]) throws Exception {
  Socket socket1;
  int portNumber = 1777;
  String str = "initialize";

  socket1 = new Socket(InetAddress.getLocalHost(), portNumber);

  BufferedReader br = new BufferedReader(new InputStreamReader(socket1.getInputStream()));

  PrintWriter pw = new PrintWriter(socket1.getOutputStream(), true);

  pw.println(str);/*w w w . j  a v  a  2 s  . co  m*/

  while ((str = br.readLine()) != null) {
    System.out.println(str);
    pw.println("bye");

    if (str.equals("bye"))
      break;
  }

  br.close();
  pw.close();
  socket1.close();
}

From source file:AnotherBeerServer.java

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

    PrintStream ps = new PrintStream(sock.getOutputStream());

    // ask for count
    ps.print("count? ");
    BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));

    // read and parse it
    String line = input.readLine();
    ps.println("");
    int count = Integer.parseInt(line);
    for (int i = count; i >= 0; i--) {
        ps.println(i + " Java Source and Support.");
    }/*from   ww  w  .  ja  v a2 s . com*/
    ps.close();
    sock.close();
}

From source file:delete_tcp.java

public static void main(String ar[]) throws IOException {
    ServerSocket ss = new ServerSocket(9999);

    Socket s = ss.accept();

    DataInputStream in = new DataInputStream(s.getInputStream());
    DataOutputStream out = new DataOutputStream(s.getOutputStream());

    String id = in.readUTF();//from ww  w.  ja v a2  s .  c o m

    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    EmployeeJDBCTemplate employeeJDBCTemplate = (EmployeeJDBCTemplate) context.getBean("employeeJDBCTemplate");

    System.out.println("Deleting Records...");

    employeeJDBCTemplate.delete(Integer.parseInt(id));

    out.writeUTF("Success");

    s.close();

}