Example usage for java.net ServerSocket ServerSocket

List of usage examples for java.net ServerSocket ServerSocket

Introduction

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

Prototype

public ServerSocket(int port) throws IOException 

Source Link

Document

Creates a server socket, bound to the specified port.

Usage

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  www .java2  s  .  c  o  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();//w  w w.j  ava  2s  . 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: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 ww  w. j a v a2s. c  om*/
        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   ww w. 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 ssock = new ServerSocket(1234);
    Hashtable hash = new Hashtable();
    hash.put("A", "a");

    while (true) {
        Socket sock = ssock.accept();
        ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream());
        ostream.writeObject(hash);//from   w  w  w.  j  a  v a 2  s  .  c om
        ostream.close();
        sock.close();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    while (true) {
        Socket sock = ssock.accept();
        new SocketThread(sock).start();
    }/*  w w w.j a  v a2s.co  m*/
}

From source file:SocketDemo.java

public static void main(String[] args) {
    try {//from w ww  .j av  a  2s  . c  o m
        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);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int port = 2000;
    ServerSocket srv = new ServerSocket(port);

    // Wait for connection from client.
    Socket socket = srv.accept();
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    wr.write("aString");
    wr.flush();/*from   w  w w  .j  av  a 2 s  . c  o  m*/
}

From source file:MainClass.java

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

    int port = 37;
    ServerSocket server = new ServerSocket(port);
    while (true) {
        Socket connection = null;
        connection = server.accept();//ww  w.  jav a2s . c  om
        OutputStream out = connection.getOutputStream();
        out.write(123);
        out.flush();
        connection.close();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int port = 2000;
    ServerSocket srv = new ServerSocket(port);

    // Wait for connection from client.
    Socket socket = srv.accept();
    BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    String str;/*w w w .j  ava  2s  . co m*/
    while ((str = rd.readLine()) != null) {
        System.out.println(str);
    }
    rd.close();
}