Java Socket Read dump(final Socket socket)

Here you can find the source of dump(final Socket socket)

Description

Dumps the state of a socket

License

Open Source License

Parameter

Parameter Description
socket The socket to dump

Return

the state of the socket

Declaration

public static String dump(final Socket socket) 

Method Source Code

//package com.java2s;
/**//from   w w  w.ja  v  a 2 s .  c om
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2007, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

public class Main {
    /**
     * Dumps the state of a socket
     * @param socket The socket to dump
     * @return the state of the socket
     */
    public static String dump(final Socket socket) {
        if (socket == null)
            return "Socket [null]";
        final StringBuilder b = new StringBuilder("Socket [");
        b.append("\n\tConnected:").append(socket.isConnected());
        final SocketAddress lsa = socket.getLocalSocketAddress();
        final SocketAddress sa = socket.getRemoteSocketAddress();
        if (sa != null && (sa instanceof InetSocketAddress)) {
            final InetSocketAddress isa = (InetSocketAddress) sa;
            b.append("\n\tRemote Host:").append(isa.getHostString());
            b.append("\n\tRemote Port:").append(isa.getPort());
        }
        if (lsa != null && (lsa instanceof InetSocketAddress)) {
            final InetSocketAddress isa = (InetSocketAddress) lsa;
            b.append("\n\tLocal Iface:").append(isa.getHostString());
            b.append("\n\tLocal Port:").append(isa.getPort());
        }

        try {
            b.append("\n\tKeepAlive:").append(socket.getKeepAlive());
        } catch (Exception x) {
            /* No Op */}
        try {
            b.append("\n\tReuseAddress:").append(socket.getReuseAddress());
        } catch (Exception x) {
            /* No Op */}
        try {
            b.append("\n\tSoLinger:").append(socket.getSoLinger());
        } catch (Exception x) {
            /* No Op */}
        try {
            b.append("\n\tSoTimeout:").append(socket.getSoTimeout());
        } catch (Exception x) {
            /* No Op */}
        try {
            b.append("\n\tTCPNoDelay:").append(socket.getTcpNoDelay());
        } catch (Exception x) {
            /* No Op */}
        b.append("\n\tInputShutdown:").append(socket.isInputShutdown());
        b.append("\n\tOutputShutdown:").append(socket.isOutputShutdown());
        b.append("\n]");
        return b.toString();
    }
}

Related

  1. getBufferedReader(Socket s)
  2. getBufferedReaderFromInputStream(Socket socket)
  3. getInputStream(Socket socket)
  4. getInputStreamFromSocket(final Socket socket)