Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

c# - .NET equivalent of Java's BufferedReader

I have that code in 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#

But i don't know which is equivalent to BufferReader. I have socket, and i want read from socket InputStream (with UTF8)

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...