2

我有 Java 中的代码

public void read() throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF8"));
    String requestURL = null;
    Vector property = new Vector();
    String line;
       //MORE OF CODE
}

如果您需要完整的代码,请在此处粘贴。

我想将其重写为 C#

但我不知道哪个相当于BufferReader。我有套接字,我想从套接字 InputStream 读取(使用 UTF8)

谢谢。

4

3 回答 3

5

这样的事情应该对你有用,尽管我确信我错过了大量的异常条件处理和一些小事情,哦,优雅的服务器关闭。

static void Main( string[] args )
{
  string      localMachineName    = Dns.GetHostName() ;
  IPHostEntry localMachineInfo    = Dns.GetHostEntry( localMachineName ) ;
  IPAddress   localMachineAddress = localMachineInfo.AddressList[0] ;
  IPEndPoint  localEndPoint       = new IPEndPoint( localMachineAddress , PORT_NUMBER ) ;

  using ( Socket server = new Socket( localEndPoint.AddressFamily , SocketType.Stream , ProtocolType.Tcp ) )
  {
    server.Bind(   localEndPoint                    ) ;
    server.Listen( PENDING_CONNECTIONS_QUEUE_LENGTH ) ;

    while ( true )
    {
      using ( Socket        connection       = server.Accept()                                         )
      using ( NetworkStream connectionStream = new NetworkStream( connection       , FileAccess.Read , false ) )
      using ( TextReader    connectionReader = new StreamReader(  connectionStream , Encoding.UTF8  ) )
      {
        IPEndPoint remoteEndpoint = (IPEndPoint) connection.RemoteEndPoint ;

        string line ;
        while ( null != (line=connectionReader.ReadLine()) )
        {
          line = line.Trim() ;
          Console.WriteLine( "Client says: {0}" , line ) ;
          if ( string.Equals( "exit"     , line , StringComparison.InvariantCultureIgnoreCase ) ) break ;
          if ( string.Equals( "quit"     , line , StringComparison.InvariantCultureIgnoreCase ) ) break ;
          if ( string.Equals( "goodbye"  , line , StringComparison.InvariantCultureIgnoreCase ) ) break ;
          if ( string.Equals( "good-bye" , line , StringComparison.InvariantCultureIgnoreCase ) ) break ;
        }

        connection.Shutdown( SocketShutdown.Both ) ;
        connection.Close() ;
      }
    }

  }

}

如果你想缓冲流,只需NetworkStream用 a 装饰实例BufferedStream

using ( Socket     connection       = server.Accept()                                                            )
using ( Stream     connectionStream = new NetworkStream( connection       , FileAccess.Read , false            ) )
using ( TextReader connectionReader = new StreamReader( new BufferedStream( connectionStream ) , Encoding.UTF8 ) )
{
  IPEndPoint remoteEndpoint = (IPEndPoint) connection.RemoteEndPoint ;

  string line ;
  while ( null != (line=connectionReader.ReadLine()) )
  {
    line = line.Trim() ;
    Console.WriteLine( "Client says: {0}" , line ) ;
    if ( string.Equals( "exit"     , line , StringComparison.InvariantCultureIgnoreCase ) ) break ;
    if ( string.Equals( "quit"     , line , StringComparison.InvariantCultureIgnoreCase ) ) break ;
    if ( string.Equals( "goodbye"  , line , StringComparison.InvariantCultureIgnoreCase ) ) break ;
    if ( string.Equals( "good-bye" , line , StringComparison.InvariantCultureIgnoreCase ) ) break ;
  }

  connection.Shutdown( SocketShutdown.Both ) ;
  connection.Close() ;
}
于 2012-08-17T19:15:10.147 回答
2

像下面这样的东西是可以比较的。

using(StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8)) {
    while(reader.Peek() >= 0) {
        Console.WriteLine(reader.ReadLine()); // or something...
    }
}
于 2012-08-17T16:43:37.327 回答
1

这取决于你想要什么。BufferedReader 缓冲来自另一个阅读器的数据。如果您只想要缓冲读取,则可以使用 StreamReader 之类的东西,具体取决于您想要读取数据的方式。

于 2012-08-17T16:38:17.047 回答