Example usage for java.net SocketException printStackTrace

List of usage examples for java.net SocketException printStackTrace

Introduction

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

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:yt_server_side.YT_Server_Side.java

/**
 * listens on the specified port./* ww w. j  a va 2 s . c  o m*/
 *
 * @param port - port to listen on.
 */
void listen(int port) {
    System.out.println("Attempting to listen on " + port);
    try {

        if (serverSocket == null) {
            serverSocket = new ServerSocket(port);
        } else if (!serverSocket.isBound()) {
            serverSocket = new ServerSocket(port);
        }
        System.out.println("Server Socket initalized on " + port);

        while (true) { // loop it so that even after a connection has been made and data sent it listents again

            System.out.println("Server is Accepting Connections.");

            clientSocket = serverSocket.accept();

            clientSocket.setSoLinger(true, 1500);
            clientSocket.setKeepAlive(true);

            System.out.println("Accept stopped blocking");

            System.out.println("Attempting to read objects from " + clientSocket.getInetAddress() + " @ "
                    + System.currentTimeMillis());

            try (ObjectInputStream oi = new ObjectInputStream(clientSocket.getInputStream());) {
                Object obj = oi.readObject();

                System.out.println("Obj to String \n\t" + obj.toString());
                if (obj instanceof Client_Profile) {

                    new Thread(new Serializer((Client_Profile) obj)).start();

                } else if (((String) obj).contains("Request CP")) {
                    String cid = ((String) obj).substring(9);
                    if (respond(findCP(cid), clientSocket.getOutputStream())) {
                        System.out.println(
                                "Succesfully responded with CP: " + cid + " @ " + System.currentTimeMillis());
                    } else {
                        System.out.println("Failed to respond with CP: " + cid);
                    }
                } else if (((String) obj).contains("Testing")) {
                    String defaultResponse = "Responding to test message from "
                            + clientSocket.getRemoteSocketAddress();
                    respond(defaultResponse, clientSocket.getOutputStream());
                }
            } catch (IOException | ClassNotFoundException ex) {
                ex.printStackTrace(System.out);
            }

        }
        // always return to listening... dont let the server die.
    } catch (java.net.BindException be) {
        System.out.println(port + " already in use");
        listen(0);
    } catch (SocketException se) {
        se.printStackTrace(System.out);
        listen(port);
    } catch (IOException ex) {
        ex.printStackTrace(System.out);
        listen(port);
    }
}

From source file:org.vle.aid.medline.DownloadMedline.java

private void download(int i, File targetDir) {
    File targetFile = new File(targetDir, mServerFileNames[i]);
    OutputStream out = null;//from w  ww  .  j a  v a 2 s.  c  o  m
    BufferedOutputStream bufOut = null;
    try {
        out = new FileOutputStream(targetFile);
        bufOut = new BufferedOutputStream(out);
        mFTPClient.retrieveFile(mServerFileNames[i], bufOut);
    } catch (SocketException e) {
        System.out.println("SocketException=" + e);
        System.out.println();
        System.out.println("Reconnecting to server.");
        reconnectFTPClient();
        printLastReply("Server reply from Retrieve file=" + mServerFileNames[i]);
    } catch (IOException e) {
        // includes connection closed exception
        printLastReply("Server reply from Retrieve file=" + mServerFileNames[i]);
        System.out.println("Download IOException. Stack trace follows.");
        e.printStackTrace(System.out);
        try {
            targetFile.delete();
        } catch (SecurityException e2) {
            System.out.println("Could not remove file=" + targetFile);
            System.out.println("Security exception. Stack trace follows.");
            e2.printStackTrace(System.out);
        }
    } finally {
        Streams.closeOutputStream(bufOut);
        Streams.closeOutputStream(out);
    }
}