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:org.jfree.chart.demo.SocketThread.java

@Override
public void run() {
    // TODO Auto-generated method stub
    try {//from  w  ww. j  ava  2 s .c o  m
        ServerSocket ss = new ServerSocket(4444);
        Socket socket = null;
        while (Islive) {
            System.out.println("Started :) ");
            socket = ss.accept(); /*  */
            System.out.println("Got a client :) ");

            InputStream sin = socket.getInputStream();
            OutputStream sout = socket.getOutputStream();

            in = new DataInputStream(sin);
            out = new DataOutputStream(sout);

            String line = null;

            while (true) {
                line = in.readUTF(); /*    */
                System.out.println("line start sending  = " + line);
                out.writeUTF(data);
                out.flush();
                line = in.readUTF();
                System.out.println(line);
                if (line == "finish")
                    break;
                else {
                    out.writeUTF("ok");
                    out.flush();
                }
            }
        }
        out.writeUTF("finish");
        out.flush();

        in.close();
        out.close();
        socket.close();
    } catch (Exception x) {

        errors += x.toString() + " ;\n";
        error_flag = true;
    }

}

From source file:de.mylifesucks.oss.ncsimulator.protocol.TcpComm.java

public TcpComm(int listenPort) throws Exception {
    System.out.println("Create socket for listening on port:" + listenPort);
    serverSocket = new ServerSocket(listenPort);
    System.out.println("Listening for connections on port " + serverSocket.getLocalPort());

    // start the read thread
    readThread = new Thread(this);
    readThread.start();/*from  www  .jav  a 2s  .  com*/
}

From source file:org.zodiark.publisher.PublisherTest.java

public final static int findFreePort() {
    ServerSocket socket = null;// ww  w .ja va 2s .  co  m

    try {
        socket = new ServerSocket(0);

        return socket.getLocalPort();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return 8080;
}

From source file:de.unikiel.inf.comsys.neo4j.RDFServerExtensionTest.java

@BeforeClass
public static void setUp() throws IOException, RepositoryException, RDFParseException {
    int port;/*from ww w.ja v a2  s.  c o  m*/
    try (final ServerSocket serverSocket = new ServerSocket(0)) {
        port = serverSocket.getLocalPort();
    }
    server = CommunityServerBuilder.server().onPort(port)
            .withThirdPartyJaxRsPackage("de.unikiel.inf.comsys.neo4j", "/rdf").build();
    server.start();
    GraphDatabaseService db = server.getDatabase().getGraph();
    Repository rep = RepositoryRegistry.getInstance(db).getRepository();
    conn = rep.getConnection();
    InputStream testdata = RDFServerExtensionTest.class.getResourceAsStream("/sp2b.n3");
    conn.add(testdata, "http://example.com/", RDFFormat.N3);
}

From source file:com.basistech.rosette.api.BasicTest.java

private static int getFreePort() {
    try (ServerSocket socket = new ServerSocket(0)) {
        serverPort = socket.getLocalPort();
    } catch (IOException e) {
        fail("Failed to allocate a port");
    }/* w  w  w. ja  v a2  s  .  c  o m*/
    assertNotEquals(0, serverPort);
    return serverPort;
}

From source file:PoolServerBase.java

static public void startServer(int port) {
    ServerSocket ssock;/* www  .  j  av a  2 s .com*/
    Socket sock;
    try {
        ssock = new ServerSocket(port);
        while (true) {
            Socket esock = null;
            try {
                esock = ssock.accept();
                while (listEmpty())
                    yield();
                assignThread(esock);
            } catch (Exception e) {
                try {
                    esock.close();
                } catch (Exception ec) {
                }
            }
        }
    } catch (IOException e) {
    }
}

From source file:Main.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 * Code from http://mina.apache.org//*from  w  w  w.  ja  va  2 s.c o m*/
 */
public static boolean available(int port) {
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
        /* falls through */
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }
    return false;
}

From source file:LCModels.MessageListener.java

public MessageListener(Session s) {

    int test = 0;
    try {//from  ww  w . j  a  v  a 2  s  .  co  m
        ServerSocket ss1 = new ServerSocket(0);
        test = ss1.getLocalPort();
        ss1.close();
    } catch (IOException ex) {
        Logger.getLogger(MessageListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    s.setPort(test);
    try {
        server = new ServerSocket(test);
    } catch (IOException ex) {
        System.out.println("ERROR KO!");
        Logger.getLogger(MessageListener.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.yeetor.util.Util.java

/**
 * ???/*from   ww  w  .j  av a 2  s.  c  om*/
 * @return
 */
public static int getFreePort() {
    ServerSocket tmp;
    int i = 10000;
    while (true) {
        try {

            i = RandomUtils.nextInt(10000, 65535);
            tmp = new ServerSocket(i);
            tmp.close();
            tmp = null;
            return i;
        } catch (Exception e4) {
            continue;
        }
    }
}

From source file:io.crate.testing.Utils.java

/**
 * @return a random available port for binding
 *///from   w w  w.j  a va 2 s.c o m
static int randomAvailablePort() {
    try {
        ServerSocket socket = new ServerSocket(0);
        int port = socket.getLocalPort();
        socket.close();
        return port;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}