本文介绍了.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
}
MORE
如果您需要完整的代码这里是糊状。
我想重写为C#
但我不知道这相当于BufferReader。
我有插座,然后我想从套接字的InputStream(用UTF8)读
But i don't know which is equivalent to BufferReader.I have socket, and i want read from socket InputStream (with UTF8)
感谢。
推荐答案
像这样的东西应该做的你,虽然我敢肯定,我错过一吨的异常情况处理和次要之类的东西,呵呵,正常关闭服务器。
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() ;
}
}
}
}
如果您希望缓冲流,只是装饰用的NetworkStream
实例 BufferedStream
:使用
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() ;
}
这篇关于.NET相当于Java的BufferedReader中的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!