C# .NET 相当于 Java 的 BufferedReader
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12009695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET equivalent of Java's BufferedReader
提问by Wiktor Tkaczyński
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 hereis paste.
如果您需要完整的代码,请在此处粘贴。
I want rewrite that to C#
我想将其重写为 C#
But i don't know which is equivalent to BufferReader. I have socket, and i want read from socket InputStream (with UTF8)
但我不知道哪个相当于 BufferReader。我有套接字,我想从套接字 InputStream 中读取(使用 UTF8)
Thanks.
谢谢。
回答by Peter Ritchie
It depends on what you want. BufferedReader buffers the data from anotherreader. If you just want buffered reads, you can use something like StreamReader depending how you want to read data.
这取决于你想要什么。BufferedReader 缓冲来自另一个阅读器的数据。如果您只需要缓冲读取,则可以使用 StreamReader 之类的东西,具体取决于您希望如何读取数据。
回答by Fraser
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...
}
}
回答by Nicholas Carey
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 NetworkStreaminstance with a BufferedStream:
如果你想缓冲流,只需NetworkStream用一个装饰实例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() ;
}

