Example usage for java.net ServerSocket accept

List of usage examples for java.net ServerSocket accept

Introduction

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

Prototype

public Socket accept() throws IOException 

Source Link

Document

Listens for a connection to be made to this socket and accepts it.

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();/*from   w ww.  ja  v  a 2  s  .c  o m*/

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

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;/*w w  w.  j  a  v a 2s. c om*/
    while (((line = in.readLine()) != null)) {
        System.out.println(line);
    }
    in.close();
    s.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ServerSocket serverSocket = new ServerSocket(8080);
    Socket socket = serverSocket.accept();

    InputStream inStream = socket.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
    String str = null;//w w  w .  ja v a  2s. c o  m
    while ((str = reader.readLine()) != null) {
        System.out.println(str);
    }
}

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   ww  w.ja  v a  2  s.  c o m*/

    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 {
    ServerSocket ssock = new ServerSocket(Integer.parseInt(args[0]));
    Socket sock = ssock.accept();
    GZIPInputStream zip = new GZIPInputStream(sock.getInputStream());
    while (true) {
        int c;//from w  ww.  j  a v  a  2  s  .  c  o  m
        c = zip.read();
        if (c == -1)
            break;
        System.out.print((char) c);
    }
}

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);/*from   w  ww  . j  a va  2  s  .c  o  m*/
        sock.close();
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    ServerSocket ss = ssf.createServerSocket(5432);
    while (true) {
        Socket s = ss.accept();
        PrintStream out = new PrintStream(s.getOutputStream());
        out.println("Hi");
        out.close();//from ww  w.j  a v  a2 s  .  c  o m
        s.close();
    }

}

From source file:ServerSocketDemo.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();

        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeInt(1);/*  w ww .  j a v a2  s  .  co m*/

        s.close();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int port = 443;
    ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
    ServerSocket ssocket = ssocketFactory.createServerSocket(port);

    Socket socket = ssocket.accept();

    InputStream in = socket.getInputStream();
    OutputStream out = socket.getOutputStream();

    // Read from in and write to out...

    in.close();//  w  w w  .  j a  v  a  2s .co m
    out.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    System.setProperty("javax.net.ssl.keyStore", "mykeystore");
    System.setProperty("javax.net.ssl.keyStorePassword", "wshr.ut");
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    ServerSocket ss = ssf.createServerSocket(5432);
    while (true) {
        Socket s = ss.accept();
        PrintStream out = new PrintStream(s.getOutputStream());
        out.println("Hi");
        out.close();//w  ww .  j  a  v  a 2 s.c o m
        s.close();
    }

}