コード例 #1
0
  /**
   * Applies the current settings to the given socket.
   *
   * @param s Socket to apply the settings to
   * @return Socket the input socket
   */
  protected Socket applySettings(Socket s) {
    try {
      s.setKeepAlive(SO_KEEPALIVE);
      s.setOOBInline(OOBINLINE);
      s.setReuseAddress(SO_REUSEADDR);
      s.setTcpNoDelay(TCP_NODELAY);
      s.setOOBInline(OOBINLINE);

      s.setReceiveBufferSize(SO_RCVBUF);
      s.setSendBufferSize(SO_SNDBUF);
      s.setSoTimeout(SO_TIMEOUT);
      s.setSoLinger(SO_LINGER, LINGER);
    } catch (SocketException e) {
      throw new RuntimeException(e);
    }
    return s;
  }
コード例 #2
0
 public static void main(String[] args) throws IOException {
   ServerSocket serverSocket = new ServerSocket(8000);
   Socket s = serverSocket.accept();
   /*
    * 输出缓冲区大小
    */
   s.setSendBufferSize(2048);
   /*
    * 接受缓冲区
    */
   s.setReceiveBufferSize(2048);
   /*
    * socket流等待时间超时就不接受数据
    */
   // s.setSoTimeout(5000);
   /*
    * 一个字节的紧急数据,接收方没法区别是否紧急
    */
   s.setOOBInline(true);
   /*
    * 设置网络服务类型高可靠性4表示高可靠性
    * 8表示高吞吐
    * 10表示最小延迟
    * 2表示低成本
    */
   s.setTrafficClass(0x04 | 0x10);
   // 接受客服端信息流
   InputStream in = s.getInputStream();
   // 向客户端发送流
   ByteArrayOutputStream buffer = new ByteArrayOutputStream();
   byte[] buff = new byte[1024];
   int len = -1;
   do {
     try {
       len = in.read(buff);
       if (len != -1) {
         buffer.write(buff, 0, len);
       }
     } catch (SocketTimeoutException e) {
       System.out.println("等待读超时");
       len = 0;
     }
   } while (len != -1);
   System.out.println(new String(buffer.toByteArray()));
 }
コード例 #3
0
ファイル: BioSender.java プロジェクト: anoopgecbh/Tomcat-4
 /** open real socket and set time out when waitForAck is enabled is socket open return directly */
 protected void openSocket() throws IOException {
   if (isConnected()) return;
   try {
     socket = new Socket();
     InetSocketAddress sockaddr = new InetSocketAddress(getAddress(), getPort());
     socket.connect(sockaddr, (int) getTimeout());
     socket.setSendBufferSize(getTxBufSize());
     socket.setReceiveBufferSize(getRxBufSize());
     socket.setSoTimeout((int) getTimeout());
     socket.setTcpNoDelay(getTcpNoDelay());
     socket.setKeepAlive(getSoKeepAlive());
     socket.setReuseAddress(getSoReuseAddress());
     socket.setOOBInline(getOoBInline());
     socket.setSoLinger(getSoLingerOn(), getSoLingerTime());
     socket.setTrafficClass(getSoTrafficClass());
     setConnected(true);
     soOut = socket.getOutputStream();
     soIn = socket.getInputStream();
     setRequestCount(0);
     setConnectTime(System.currentTimeMillis());
     if (log.isDebugEnabled())
       log.debug(
           sm.getString(
               "IDataSender.openSocket",
               getAddress().getHostAddress(),
               new Integer(getPort()),
               new Long(0)));
   } catch (IOException ex1) {
     SenderState.getSenderState(getDestination()).setSuspect();
     if (log.isDebugEnabled())
       log.debug(
           sm.getString(
               "IDataSender.openSocket.failure",
               getAddress().getHostAddress(),
               new Integer(getPort()),
               new Long(0)),
           ex1);
     throw (ex1);
   }
 }
コード例 #4
0
 @Override
 public void setOOBInline(boolean on) throws SocketException {
   sock.setOOBInline(on);
 }