Example #1
0
  void test() throws Exception {
    InetSocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort);
    Proxy httpProxy = new Proxy(Proxy.Type.HTTP, proxyAddress);

    try (ServerSocket ss = new ServerSocket(0);
        Socket sock = new Socket(httpProxy)) {
      sock.setSoTimeout(SO_TIMEOUT);
      sock.setTcpNoDelay(false);

      InetSocketAddress externalAddress =
          new InetSocketAddress(InetAddress.getLocalHost(), ss.getLocalPort());

      out.println("Trying to connect to server socket on " + externalAddress);
      sock.connect(externalAddress);
      try (Socket externalSock = ss.accept()) {
        // perform some simple checks
        check(sock.isBound(), "Socket is not bound");
        check(sock.isConnected(), "Socket is not connected");
        check(!sock.isClosed(), "Socket should not be closed");
        check(sock.getSoTimeout() == SO_TIMEOUT, "Socket should have a previously set timeout");
        check(sock.getTcpNoDelay() == false, "NODELAY should be false");

        simpleDataExchange(sock, externalSock);
      }
    }
  }
  /** {@inheritDoc} */
  public Object getOption(SocketOption name) throws IOException {
    if (!(name instanceof StandardSocketOption)) {
      throw new IllegalArgumentException("Unsupported option " + name);
    }

    StandardSocketOption stdOpt = (StandardSocketOption) name;
    final Socket socket = channel.socket();
    try {
      switch (stdOpt) {
        case SO_SNDBUF:
          return socket.getSendBufferSize();

        case SO_RCVBUF:
          return socket.getReceiveBufferSize();

        case SO_KEEPALIVE:
          return socket.getKeepAlive();

        case SO_REUSEADDR:
          return socket.getReuseAddress();

        case TCP_NODELAY:
          return socket.getTcpNoDelay();

        default:
          throw new IllegalArgumentException("Unsupported option " + name);
      }
    } catch (SocketException e) {
      if (socket.isClosed()) {
        throw Util.initCause(new ClosedChannelException(), e);
      }
      throw e;
    }
  }
Example #3
0
  public static void printSocketParameters(Socket cl) throws SocketException {

    boolean SO_KEEPALIVE = cl.getKeepAlive();
    boolean TCP_NODELAY = cl.getTcpNoDelay();

    int SO_LINGER = cl.getSoLinger();
    int SO_TIMEOUT = cl.getSoTimeout();

    int SO_RCVBUF = cl.getReceiveBufferSize();
    int SO_SNDBUF = cl.getSendBufferSize();

    int trafficClassVal = cl.getTrafficClass();
    /*
     		0 <= trafficClassVal <= 255
    	IPTOS_LOWCOST (0x02)
    	IPTOS_RELIABILITY (0x04)
     			IPTOS_THROUGHPUT (0x08)
    	IPTOS_LOWDELAY (0x10)

    */

    int remotePort = cl.getPort();
    int localPort = cl.getLocalPort();
    String localIP = getIPstr(cl.getLocalAddress());
    String remoteIP = getIPstr(cl.getInetAddress());

    System.out.println("Socket Paramaters :");
    System.out.println("SO_KEEPAILVE = " + SO_KEEPALIVE + " TCP_NODELAY = " + TCP_NODELAY);
    System.out.println("SO_LINGER = " + SO_LINGER + "  SO_TIMEOUT = " + SO_TIMEOUT);
    System.out.println("SO_RCVBUF = " + SO_RCVBUF + "  SO_SNDBUF = " + SO_SNDBUF);
    System.out.println("Traffic Class = " + trafficClassVal);
    System.out.println("Local Address = " + localIP + ":" + localPort);
    System.out.println("Remote Address = " + remoteIP + ":" + remotePort);
  }
 @Override
 public boolean isTcpNoDelay() {
   try {
     return socket.getTcpNoDelay();
   } catch (SocketException e) {
     throw new ChannelException(e);
   }
 }
 @Override
 public boolean getTcpNoDelay() throws SocketException {
   return sock.getTcpNoDelay();
 }
Example #6
0
 public boolean getTcpNoDelay() throws SocketException {
   return mSocket.getTcpNoDelay();
 }