.NET相当于Java的BufferedReader
我用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)
谢谢。
这样的事情应该对你有用,虽然我确定我缺少一些特殊的条件处理和一些小问题,比如哦,优雅的服务器关闭。
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() ; } } } }
如果要缓冲流,只需使用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() ; }
这取决于你想要什么。 BufferedReader缓冲来自另一个阅读器的数据。 如果您只想要缓冲读取,则可以根据读取数据的方式使用StreamReader之类的内容。
像下面这样的东西是可比较的。
上述就是C#学习教程:.NET相当于Java的BufferedReader分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
using(StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8)) { while(reader.Peek() >= 0) { Console.WriteLine(reader.ReadLine()); // or something... } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1015395.html