@Override
 public void setTrafficClass(int trafficClass) {
   try {
     socket.setTrafficClass(trafficClass);
   } catch (SocketException e) {
     throw new ChannelException(e);
   }
 }
 /** 启动服务,监听端口用于接收联通上行信息 */
 public void startSvr() throws IOException {
   while (true) {
     Socket unicomSocket = null;
     unicomSocket = spsvrSocket.accept();
     unicomSocket.setSoLinger(true, 0); // socket关闭时,不再发送缓冲区里的数据,立即释放低层资源
     unicomSocket.setTcpNoDelay(true); // 不使用缓冲区,立即发送数据
     unicomSocket.setTrafficClass(0x04 | 0x10);
     exec.execute(new DealThread(unicomSocket));
   }
 }
Exemple #3
0
 /**
  * Starts a server connection. Note: This method will never return unless the user switches to a
  * different game mode, it will exit the program when done.
  */
 @SuppressWarnings("nls")
 public void startServer() {
   try {
     serv = new ServerSocket(port);
     System.out.println("Waiting for connection on port " + port + ".");
     socket = serv.accept();
     serv.close();
     sendWithoutExceptions(MSG_IAMSERVER);
     sendWithoutExceptions((int) ((seed >> 56) & 255));
     sendWithoutExceptions((int) ((seed >> 48) & 255));
     sendWithoutExceptions((int) ((seed >> 40) & 255));
     sendWithoutExceptions((int) ((seed >> 32) & 255));
     sendWithoutExceptions((int) ((seed >> 24) & 255));
     sendWithoutExceptions((int) ((seed >> 16) & 255));
     sendWithoutExceptions((int) ((seed >> 8) & 255));
     sendWithoutExceptions((int) (seed & 255));
     System.out.println("Sent seed " + seed);
   } catch (Exception e) {
     if (serv == null) {
         /* The user switched out of network mode */
       return;
     }
     System.out.println("Could not listen on port " + port + ".");
     System.exit(1);
   }
   try {
     socket.setTcpNoDelay(true);
     socket.setTrafficClass(IPTOS_LOWDELAY);
   } catch (Exception e) {
     /* We really don't care */
   }
   game.unPauseGame();
   opponentState = new Game(seed, "Opponent game");
   opponentThread = new Thread(opponentState, "Opponent visualisation thread");
   opponentState.moveFrameRight();
   opponentState.setOpponentGame(game);
   opponentState.setNoAutoMove(true);
   game.relayMovements(this);
   opponentThread.start();
   synchronized (opponentState) {
     try {
       if (opponentState.getFrame() == null) opponentState.wait();
     } catch (InterruptedException e) {
       System.exit(1);
     }
   }
   opponentState.unPauseGame();
   readloop();
 }
  /**
   * @param sock The socket on which to set the Traffic Class.
   * @return Whether or not the Traffic Class was set on the given socket.
   * @throws SocketException if the system does not support setting the Traffic Class.
   * @throws IllegalArgumentException if both the Differentiated Services and Type of Services
   *     transport options have been set on the same connection.
   */
  private boolean setTrafficClass(Socket sock) throws SocketException, IllegalArgumentException {
    if (sock == null || (!this.diffServChosen && !this.typeOfServiceChosen)) {
      return false;
    }
    if (this.diffServChosen && this.typeOfServiceChosen) {
      throw new IllegalArgumentException(
          "Cannot set both the "
              + " Differentiated Services and Type of Services transport "
              + " options on the same connection.");
    }

    sock.setTrafficClass(this.trafficClass);

    int resultTrafficClass = sock.getTrafficClass();
    if (this.trafficClass != resultTrafficClass) {
      // In the case where the user has specified the ECN bits (e.g. in
      // Type of Service) but the system won't allow the ECN bits to be
      // set or in the case where setting the traffic class failed for
      // other reasons, emit a warning.
      if ((this.trafficClass >> 2) == (resultTrafficClass >> 2)
          && (this.trafficClass & 3) != (resultTrafficClass & 3)) {
        LOG.warn(
            "Attempted to set the Traffic Class to "
                + this.trafficClass
                + " but the result Traffic Class was "
                + resultTrafficClass
                + ". Please check that your system "
                + "allows you to set the ECN bits (the first two bits).");
      } else {
        LOG.warn(
            "Attempted to set the Traffic Class to "
                + this.trafficClass
                + " but the result Traffic Class was "
                + resultTrafficClass
                + ". Please check that your system "
                + "supports java.net.setTrafficClass.");
      }
      return false;
    }
    // Reset the guards that prevent both the Differentiated Services
    // option and the Type of Service option from being set on the same
    // connection.
    this.diffServChosen = false;
    this.typeOfServiceChosen = false;
    return true;
  }
 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()));
 }
Exemple #6
0
 /**
  * Starts a client connection. Note: this method will never return, it will exit the program when
  * done.
  */
 @SuppressWarnings("nls")
 public void startClient() {
   if (!(serverName == null)) {
     try {
       socket = new Socket(serverName, port);
     } catch (IOException e) {
       System.out.println("Could not connect to " + serverName + ":" + port);
       System.exit(1);
     }
     try {
       socket.setTcpNoDelay(true);
       socket.setTrafficClass(IPTOS_LOWDELAY);
     } catch (Exception e) {
       /* We really don't care */
     }
     /* No unpause, that will be done when we hear from the server */
     readloop();
   }
 }
 /**
  * Sets socket attributes on the socket.
  *
  * @param socket The socket.
  * @throws SocketException
  */
 protected void setSocketAttributes(Socket socket) throws SocketException {
   if (this.soTimeout >= 0) {
     socket.setSoTimeout(this.soTimeout);
   }
   if (this.soSendBufferSize > 0) {
     socket.setSendBufferSize(this.soSendBufferSize);
   }
   if (this.soReceiveBufferSize > 0) {
     socket.setReceiveBufferSize(this.soReceiveBufferSize);
   }
   socket.setTcpNoDelay(this.soTcpNoDelay);
   if (this.soLinger >= 0) {
     socket.setSoLinger(true, this.soLinger);
   }
   if (this.soTrafficClass >= 0) {
     socket.setTrafficClass(this.soTrafficClass);
   }
   socket.setKeepAlive(this.soKeepAlive);
 }
Exemple #8
0
 /** 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);
   }
 }
  public TcpConnection(
      Socket p_i3288_1_, String p_i3288_2_, NetHandler p_i3288_3_, PrivateKey p_i3288_4_)
      throws IOException {
    this.field_74478_h = new Object();
    this.field_74475_m = true;
    this.field_74472_n = false;
    this.field_74473_o = Collections.synchronizedList(new ArrayList());
    this.field_74487_p = Collections.synchronizedList(new ArrayList());
    this.field_74486_q = Collections.synchronizedList(new ArrayList());
    this.field_74484_s = false;
    this.field_74481_v = "";
    this.field_74490_x = 0;
    this.field_74489_y = 0;
    this.field_74468_e = 0;
    this.field_74465_f = false;
    this.field_74466_g = false;
    this.field_74488_z = null;
    this.field_74463_A = null;
    this.field_74464_B = 50;
    this.field_74463_A = p_i3288_4_;
    this.field_74479_i = p_i3288_1_;
    this.field_74476_j = p_i3288_1_.getRemoteSocketAddress();
    this.field_74485_r = p_i3288_3_;

    try {
      p_i3288_1_.setSoTimeout(30000);
      p_i3288_1_.setTrafficClass(24);
    } catch (SocketException var6) {
      System.err.println(var6.getMessage());
    }

    this.field_74477_k = new DataInputStream(p_i3288_1_.getInputStream());
    this.field_74474_l =
        new DataOutputStream(new BufferedOutputStream(p_i3288_1_.getOutputStream(), 5120));
    this.field_74482_u = new TcpReaderThread(this, p_i3288_2_ + " read thread");
    this.field_74483_t = new TcpWriterThread(this, p_i3288_2_ + " write thread");
    this.field_74482_u.start();
    this.field_74483_t.start();
  }
  public TcpConnection(
      Socket p_i3288_1_, String p_i3288_2_, NetHandler p_i3288_3_, PrivateKey p_i3288_4_)
      throws IOException {
    this.field_74463_A = p_i3288_4_;
    this.field_74479_i = p_i3288_1_;
    this.field_74476_j = p_i3288_1_.getRemoteSocketAddress();
    this.field_74485_r = p_i3288_3_;

    try {
      p_i3288_1_.setSoTimeout(30000);
      p_i3288_1_.setTrafficClass(24);
    } catch (SocketException var6) {
      System.err.println(var6.getMessage());
    }

    this.field_74477_k = new DataInputStream(p_i3288_1_.getInputStream());
    this.field_74474_l =
        new DataOutputStream(new BufferedOutputStream(p_i3288_1_.getOutputStream(), 5120));
    this.field_74482_u = new TcpReaderThread(this, p_i3288_2_ + " read thread");
    this.field_74483_t = new TcpWriterThread(this, p_i3288_2_ + " write thread");
    this.field_74482_u.start();
    this.field_74483_t.start();
  }
  private void func_74017_b(ServerData p_74017_1_) throws IOException {
    ServerAddress var2 = ServerAddress.func_78860_a(p_74017_1_.field_78845_b);
    Socket var3 = null;
    DataInputStream var4 = null;
    DataOutputStream var5 = null;

    try {
      var3 = new Socket();
      var3.setSoTimeout(3000);
      var3.setTcpNoDelay(true);
      var3.setTrafficClass(18);
      var3.connect(new InetSocketAddress(var2.func_78861_a(), var2.func_78864_b()), 3000);
      var4 = new DataInputStream(var3.getInputStream());
      var5 = new DataOutputStream(var3.getOutputStream());
      var5.write(254);
      if (var4.read() != 255) {
        throw new IOException("Bad message");
      }

      String var6 = Packet.func_73282_a(var4, 256);
      char[] var7 = var6.toCharArray();

      for (int var8 = 0; var8 < var7.length; ++var8) {
        if (var7[var8] != 167 && ChatAllowedCharacters.field_71568_a.indexOf(var7[var8]) < 0) {
          var7[var8] = 63;
        }
      }

      var6 = new String(var7);
      String[] var27 = var6.split("\u00a7");
      var6 = var27[0];
      int var9 = -1;
      int var10 = -1;

      try {
        var9 = Integer.parseInt(var27[1]);
        var10 = Integer.parseInt(var27[2]);
      } catch (Exception var25) {;
      }

      p_74017_1_.field_78843_d = "\u00a77" + var6;
      if (var9 >= 0 && var10 > 0) {
        p_74017_1_.field_78846_c = "\u00a77" + var9 + "\u00a78/\u00a77" + var10;
      } else {
        p_74017_1_.field_78846_c = "\u00a78???";
      }
    } finally {
      try {
        if (var4 != null) {
          var4.close();
        }
      } catch (Throwable var24) {;
      }

      try {
        if (var5 != null) {
          var5.close();
        }
      } catch (Throwable var23) {;
      }

      try {
        if (var3 != null) {
          var3.close();
        }
      } catch (Throwable var22) {;
      }
    }
  }
 @Override
 public void setTrafficClass(int tc) throws SocketException {
   sock.setTrafficClass(tc);
 }