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:com.nokia.dempsy.messagetransport.tcp.TcpReceiver.java

private void closeQuietly(Socket socket) {
    if (socket != null) {
        try {//from  ww w.j a  v  a 2s.  c  om
            socket.close();
        } catch (IOException ioe) {
            if (logger.isDebugEnabled())
                logger.debug("close socket failed for " + destination, ioe);
        }
    }
}

From source file:eu.smeny.jpapercut.smtp.MailServer.java

private void refuseConnection(Socket client) {
    try {//from w  w  w  .  ja v a  2 s . co  m
        OutputStream output = client.getOutputStream();
        PrintWriter writer = new PrintWriter(output);
        String hostname = retrieveHostname();
        writer.write("421 " + hostname + " Service not available, too much connections");
        writer.flush();
        client.close();
    } catch (IOException ioe) {
        logger.error("IO Error while refusing connection", ioe);
    }
}

From source file:Main.java

public void run() {
    while (true) {
        try {//from w w w. j  a v  a  2s.com
            System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();

            System.out.println("Just connected to " + server.getRemoteSocketAddress());
            DataInputStream in = new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());

            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");

            server.close();
        } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
}

From source file:io.selendroid.android.impl.DefaultAndroidEmulator.java

private static Map<String, Integer> mapDeviceNamesToSerial() {
    Map<String, Integer> mapping = new HashMap<String, Integer>();
    CommandLine command = new CommandLine(AndroidSdk.adb());
    command.addArgument("devices");
    Scanner scanner;// w  w  w . ja v a2s .  co m
    try {
        scanner = new Scanner(ShellCommand.exec(command));
    } catch (ShellCommandException e) {
        return mapping;
    }
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        Pattern pattern = Pattern.compile("emulator-\\d\\d\\d\\d");
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            String serial = matcher.group(0);

            Integer port = Integer.valueOf(serial.replaceAll("emulator-", ""));
            TelnetClient client = null;
            try {
                client = new TelnetClient(port);
                String avdName = client.sendCommand("avd name");
                mapping.put(avdName, port);
            } catch (AndroidDeviceException e) {
                // ignore
            } finally {
                if (client != null) {
                    client.close();
                }
            }
            Socket socket = null;
            PrintWriter out = null;
            BufferedReader in = null;
            try {
                socket = new Socket("127.0.0.1", port);
                out = new PrintWriter(socket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                if (in.readLine() == null) {
                    throw new AndroidDeviceException("error");
                }

                out.write("avd name\r\n");
                out.flush();
                in.readLine();// OK
                String avdName = in.readLine();
                mapping.put(avdName, port);
            } catch (Exception e) {
                // ignore
            } finally {
                try {
                    out.close();
                    in.close();
                    socket.close();
                } catch (Exception e) {
                    // do nothing
                }
            }
        }
    }
    scanner.close();

    return mapping;
}

From source file:TimeServer.java

public void run() {
    Socket client = null;

    while (true) {
        if (sock == null)
            return;
        try {//from   w  w  w.j  a  v a 2  s . c  o  m
            client = sock.accept();
            BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
            PrintWriter os = new PrintWriter(bos, false);
            String outLine;

            Date now = new Date();
            os.println(now);
            os.flush();

            os.close();
            client.close();
        } catch (IOException e) {
            System.out.println("Error: couldn't connect to client.");
            System.exit(1);
        }
    }
}

From source file:com.android.strictmodetest.StrictModeActivity.java

private void closeWithLinger(boolean linger) {
    Log.d(TAG, "Socket linger test; linger=" + linger);
    try {/*from w w w.ja v a  2 s . c  om*/
        Socket socket = new Socket();
        socket.setSoLinger(linger, 5);
        socket.close();
    } catch (IOException e) {
        Log.e(TAG, "Error with linger close", e);
    }
}

From source file:com.linuxbox.enkive.server.ArchivingThreadPoolServer.java

@Override
protected void createAndStartProcessor(Socket socket) throws Exception {
    // If there is space left in the queue, process the message.
    // Otherwise, reject the message and let the client end manage it.
    try {/*from w ww  . ja  v  a  2s  .c o m*/
        ThreadedProcessor session = initializeProcessor(socket);
        threadPool.execute(session);
    } catch (RejectedExecutionException e) {
        socket.close();
    }
}

From source file:com.talis.platform.sequencing.zookeeper.ZkTestHelper.java

public boolean waitForServerDown(String hp, long timeout) {
    long start = System.currentTimeMillis();
    String split[] = hp.split(":");
    String host = split[0];/*www.  j  a  v  a2s.c  om*/
    int port = Integer.parseInt(split[1]);
    while (true) {
        try {
            Socket sock = new Socket(host, port);
            try {
                OutputStream outstream = sock.getOutputStream();
                outstream.write("stat".getBytes());
                outstream.flush();
            } finally {
                sock.close();
            }
        } catch (IOException e) {
            return true;
        }

        if (System.currentTimeMillis() > start + timeout) {
            break;
        }
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            // ignore
        }
    }
    return false;
}