Java Code Examples for java.net.Socket#isInputShutdown()

The following examples show how to use java.net.Socket#isInputShutdown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SocketUtils.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
public static void closeQuietly(final Socket socket) {
    if (socket == null) {
        return;
    }

    try {
        try {
            // can't shudown input/output individually with secure sockets
            if ((socket instanceof SSLSocket) == false) {
                if (socket.isInputShutdown() == false) {
                    socket.shutdownInput();
                }
                if (socket.isOutputShutdown() == false) {
                    socket.shutdownOutput();
                }
            }
        } finally {
            if (socket.isClosed() == false) {
                socket.close();
            }
        }
    } catch (final Exception ex) {
        logger.debug("Failed to close socket due to: " + ex, ex);
    }
}
 
Example 2
Source File: SslSource.java    From Azzet with Open Software License 3.0 6 votes vote down vote up
@Override
public InputStream getStream( String request ) throws Exception
{
	byte[] path = getAbsolute( request ).getBytes();

	Socket s = socketPool.poll();
	if (s == null || s.isClosed() || s.isOutputShutdown() || s.isInputShutdown())
	{
		s = socketFactory.createSocket( address.getAddress(), address.getPort() );
	}

	DataOutputStream o = new DataOutputStream( s.getOutputStream() );
	o.writeInt( path.length );
	o.write( path );
	o.flush();

	DataInputStream i = new DataInputStream( s.getInputStream() );
	int size = i.readInt();

	return new SocketInputStream( s, size, socketPool );
}
 
Example 3
Source File: SocketUtils.java    From nifi with Apache License 2.0 6 votes vote down vote up
public static void closeQuietly(final Socket socket) {
    if (socket == null) {
        return;
    }

    try {
        try {
            // Can't shutdown input/output individually with secure sockets
            if (!(socket instanceof SSLSocket)) {
                if (!socket.isInputShutdown()) {
                    socket.shutdownInput();
                }
                if (!socket.isOutputShutdown()) {
                    socket.shutdownOutput();
                }
            }
        } finally {
            if (!socket.isClosed()) {
                socket.close();
            }
        }
    } catch (final Exception ex) {
        logger.debug("Failed to close socket due to: " + ex, ex);
    }
}
 
Example 4
Source File: TCPConnector.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public void sendToServer(final IAgent agent, final String data) throws GamaRuntimeException {
	OutputStream ostream = null;
	final ClientServiceThread c = (ClientServiceThread) agent.getAttribute(_TCP_SOCKET);
	Socket sock = null;
	if (c != null) {
		sock = c.getMyClientSocket();
	}
	if (sock == null || sock.isClosed() || sock.isInputShutdown()) { return; }
	try {
		ostream = sock.getOutputStream();
		final PrintWriter pwrite = new PrintWriter(ostream, true);
		pwrite.println(data);
		pwrite.flush();
	} catch (final Exception e) {
		throw GamaRuntimeException.create(e, agent.getScope());
	}

}
 
Example 5
Source File: NioSocket.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
/** Close the channel, and clean up any data associated with it. */
public void close(final SocketChannel channel) {
  try {
    final Socket s = channel.socket();
    if (!s.isInputShutdown()) {
      s.shutdownInput();
    }
    if (!s.isOutputShutdown()) {
      s.shutdownOutput();
    }
    if (!s.isClosed()) {
      s.close();
    }
    channel.close();
  } catch (final IOException e1) {
    log.log(Level.FINE, "error closing channel", e1);
  }
  decoder.close(channel);
  writer.close(channel);
  reader.close(channel);
}
 
Example 6
Source File: SqueakTCPSocket.java    From trufflesqueak with MIT License 6 votes vote down vote up
private Status clientStatus() throws IOException {
    if (clientChannel == null) {
        return Status.Unconnected;
    }

    maybeCompleteConnection();
    final Socket socket = clientChannel.socket();

    if (socket.isInputShutdown()) {
        return Status.OtherEndClosed;
    }

    if (socket.isOutputShutdown()) {
        return Status.ThisEndClosed;
    }

    if (!socket.isConnected()) {
        return Status.Unconnected;
    }

    if (socket.isClosed()) {
        return Status.ThisEndClosed;
    }

    return Status.Connected;
}
 
Example 7
Source File: MegaProxyServer.java    From megabasterd with GNU General Public License v3.0 5 votes vote down vote up
private static void forwardData(Socket inputSocket, Socket outputSocket) {
    try {
        InputStream inputStream = inputSocket.getInputStream();
        try {
            OutputStream outputStream = outputSocket.getOutputStream();
            try {
                byte[] buffer = new byte[4096];
                int read;
                do {
                    read = inputStream.read(buffer);
                    if (read > 0) {
                        outputStream.write(buffer, 0, read);
                        if (inputStream.available() < 1) {
                            outputStream.flush();
                        }
                    }
                } while (read >= 0);
            } finally {
                if (!outputSocket.isOutputShutdown()) {
                    outputSocket.shutdownOutput();
                }
            }
        } finally {
            if (!inputSocket.isInputShutdown()) {
                inputSocket.shutdownInput();
            }
        }
    } catch (IOException e) {

    }
}
 
Example 8
Source File: HAReceiveService.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String toString() {

    final Socket s = client.socket();
    
    return super.toString() //
            + "{client.isOpen()=" + client.isOpen()//
            + ",client.isConnected()=" + client.isConnected()//
            + ",socket.isInputShutdown()="
            + (s == null ? "N/A" : s.isInputShutdown())//
            + ",clientSelector.isOpen=" + clientSelector.isOpen()//
            + "}";

}
 
Example 9
Source File: NetUtils.java    From portforward with Apache License 2.0 5 votes vote down vote up
/**
 * Quietly shutdown the input of a {@linkplain Socket}. Exception are ignored.
 * 
 * @param socket
 *            the socket to shutdown the input
 */
public static void shutdownInput(Socket socket) {
    if (socket == null) return;
    try {
        if (!socket.isInputShutdown()) socket.shutdownInput();
    } catch (Throwable e) {
        log.debug(e.getMessage(), e);
    }
}
 
Example 10
Source File: RpcSocketPool.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private boolean isAlive(Socket socket) {
	return socket != null && socket.isBound() && !socket.isClosed()
			&& socket.isConnected() && !socket.isInputShutdown()
			&& !socket.isOutputShutdown();
}
 
Example 11
Source File: RpcSocketPool.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private boolean isAlive(Socket socket) {
	return socket != null && socket.isBound() && !socket.isClosed()
			&& socket.isConnected() && !socket.isInputShutdown()
			&& !socket.isOutputShutdown();
}
 
Example 12
Source File: RpcSocketPool.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private boolean isAlive(Socket socket) {
	return socket != null && socket.isBound() && !socket.isClosed()
			&& socket.isConnected() && !socket.isInputShutdown()
			&& !socket.isOutputShutdown();
}
 
Example 13
Source File: RpcSocketPool.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private boolean isAlive(Socket socket) {
	return socket != null && socket.isBound() && !socket.isClosed()
			&& socket.isConnected() && !socket.isInputShutdown()
			&& !socket.isOutputShutdown();
}
 
Example 14
Source File: RpcSocketPool.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private boolean isAlive(Socket socket) {
	return socket != null && socket.isBound() && !socket.isClosed()
			&& socket.isConnected() && !socket.isInputShutdown()
			&& !socket.isOutputShutdown();
}
 
Example 15
Source File: RpcSocketPool.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private boolean isAlive(Socket socket) {
	return socket != null && socket.isBound() && !socket.isClosed()
			&& socket.isConnected() && !socket.isInputShutdown()
			&& !socket.isOutputShutdown();
}
 
Example 16
Source File: ECHelper.java    From aMuleRemote with GNU General Public License v3.0 4 votes vote down vote up
public ECClient getECClient() throws IOException, IllegalArgumentException {
    
    // This should prevent the null Exception on mServerVersion. However it's not clear why that happened.
    // Need to check if client is null when calling getECClient
    
    if (DEBUG) Log.d(TAG, "ECHelper.getECClient: Validating server info");
    if (! this.validateServerInfo()) return null;

    if (DEBUG) Log.d(TAG, "ECHelper.getECClient: Client last idle at " + Long.toString(mECClientLastIdle));
    
    if (mECClient != null && System.currentTimeMillis() - mECClientLastIdle > IDLE_CLIENT_MAX_AGE_MILLIS) {
        if (DEBUG) Log.d(TAG, "ECHelper.getECClient: Current client is too old. Resetting.");
        resetClient();
    }
    
    Socket s = null;
    if (! mServerVersion.equals("Fake")) {
        s = getAmuleSocket();
        if (s.isClosed() || s.isInputShutdown() || s.isOutputShutdown()) {
            if (DEBUG) Log.d(TAG, "ECHelper.getECClient: Current socket is not valid. Resetting.");
            resetSocket();
            s = getAmuleSocket();
        }
        if (!s.isConnected()) {
            if (DEBUG) Log.d(TAG, "ECHelper.getECClient: Current socket is not connected. Connecting to " + mServerHost);
            s.connect(new InetSocketAddress(InetAddress.getByName(mServerHost), mServerPort), mClientConnectTimeout);
            s.setSoTimeout(mClientReadTimeout);
        }
    }
    
    if (mECClient == null) {
        if (DEBUG) Log.d(TAG, "ECHelper.getECClient: Creating a " + mServerVersion + " client");
        ECClient c;
        if (mServerVersion.equals("V204")) {
            c = new ECClientV204();
        } else  if (mServerVersion.equals("V203")) {
            c = new ECClientV203();
        } else if (mServerVersion.equals("Fake")) {
            c = new ECClientFake();
        } else {
            c = new ECClient();
        }
        c.setClientName("Amule Remote for Android");
        c.setClientVersion(mApp.mVersionName);
        try {
            c.setPassword(mServerPassword);
        } catch (NoSuchAlgorithmException e) {
        }
        c.setSocket(s);
        mECClient = c;
        mECClientLastIdle = System.currentTimeMillis();
    }
    
    if (mECClient != null) {
        if (Flavor.ACRA_ENABLED) ACRA.getErrorReporter().putCustomData("ServerVersion", mECClient.getServerVersion());
        // TBV: Can be removed? setClientStale(false);
    }
    return mECClient;
}
 
Example 17
Source File: MultiThreadedSocketServer.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run() {
	// Successfully created Server Socket. Now wait for connections.
	while (!closed) {
		try {
			// Accept incoming connections.
			if (myAgent.dead()) {
				closed = true;
				this.interrupt();
				return;
			}
			// DEBUG.OUT(myServerSocket+" server waiting for connection");

			final Socket clientSocket = myServerSocket.accept();
			DEBUG.OUT(clientSocket + " connected");

			if (!clientSocket.isClosed() && !clientSocket.isInputShutdown()) {
				final IList<String> list_net_agents =
						Cast.asList(myAgent.getScope(), myAgent.getAttribute(INetworkSkill.NET_AGENT_GROUPS));
				if (list_net_agents != null && !list_net_agents.contains(clientSocket.toString())) {
					list_net_agents.addValue(myAgent.getScope(), clientSocket.toString());
					myAgent.setAttribute(INetworkSkill.NET_AGENT_GROUPS, list_net_agents);
					clientSocket.setSoTimeout(TCPConnector._TCP_SO_TIMEOUT);
					clientSocket.setKeepAlive(true);

					final ClientServiceThread cliThread = new ClientServiceThread(myAgent, clientSocket);
					cliThread.start();

					myAgent.setAttribute(TCPConnector._TCP_CLIENT + clientSocket.toString(), cliThread);
				}
			}

		} catch (final SocketTimeoutException ste) {
			// DEBUG.OUT("server waiting time out ");
			// try {
			// Thread.sleep(1000);
			// } catch(InterruptedException ie){
			// }
			// catch (Exception e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
		} catch (final Exception ioe) {

			if (myServerSocket.isClosed()) {
				closed = true;
			} else {
				ioe.printStackTrace();
			}
		}
	}
	// DEBUG.OUT("closed ");
	try {
		myAgent.setAttribute(TCPConnector._TCP_SERVER + myServerSocket.getLocalPort(), null);
		myServerSocket.close();
	} catch (final Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}
 
Example 18
Source File: NioSocketChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isShutdown() {
    Socket socket = javaChannel().socket();
    return socket.isInputShutdown() && socket.isOutputShutdown() || !isActive();
}