2012-08-17 37 views
2

的我有一个代码在Java中.NET相当于Java的BufferedReader中

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 
} 

如果您需要完整的代码here是糊。

我想重写到C#

但我不知道这相当于BufferReader。 我有套接字,并且我想从套接字InputStream读取(使用UTF8)

谢谢。

+4

我认为你是在C#中寻找'StreamReader'。 – HenryZhang 2012-08-17 16:36:26

回答

1

这取决于你想要什么。 BufferedReader从缓冲另一个阅读器的数据。如果你只是想缓冲读取,你可以根据你想要读取数据的方式使用StreamReader之类的东西。

1

像下面这样的东西是可比的。

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

是的,但从套接字我没有GetStream。 (以“G”开头 - GetHashCode,GetSocketOption,GetType)。 – 2012-08-17 17:02:09

+0

@WiktorTkaczyński,请看看'TcpClient'和'TcpListener'类:它们有'GetStream()'方法。 – 2013-02-07 19:10:15

5

像这样的东西应该做的你,虽然我敢肯定,我错过一吨的异常情况处理和次要之类的东西,哦,正常关闭服务器。

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