简体   繁体   English

.NET相当于Java的BufferedReader

[英].NET equivalent of Java's BufferedReader

I have that code in Java 我有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
}

If You need full code here is paste. 如果您需要完整的代码,请粘贴。

I want rewrite that to C# 我想将其重写为C#

But i don't know which is equivalent to BufferReader. 但我不知道这相当于BufferReader。 I have socket, and i want read from socket InputStream (with UTF8) 我有套接字,我想从套接字InputStream中读取(使用UTF8)

Thanks. 谢谢。

Something like this ought to do you, though I'm sure I'm missing a ton of exceptional condition handling and minor things like, oh, graceful server shutdown. 这样的事情应该可以帮助您,尽管我确定我会遗漏大量异常条件处理和一些小事情,例如,正常关闭服务器。

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() ;
      }
    }

  }

}

If you want to buffer the stream, just decorate the NetworkStream instance with a BufferedStream : 如果要缓冲流,只需用BufferedStream装饰NetworkStream实例:

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() ;
}

It depends on what you want. 这取决于您想要什么。 BufferedReader buffers the data from another reader. BufferedReader缓冲来自另一个读取器的数据。 If you just want buffered reads, you can use something like StreamReader depending how you want to read data. 如果只希望缓冲读取,则可以使用StreamReader之类的方法,具体取决于您想要读取数据的方式。

Something like the following would be comparable. 类似以下内容将是可比较的。

using(StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8)) {
    while(reader.Peek() >= 0) {
        Console.WriteLine(reader.ReadLine()); // or something...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM