当前位置:编程学习 > C#/ASP.NET >>

求救!关于StreamReader

private void thStart(object obj)
        {
            Console.WriteLine("建立分支点对点连接...");
            NetworkStream NS = (NetworkStream)obj;
            SR = new StreamReader(NS);
            SW = new StreamWriter(NS);
            while (true)
            {
                if (NS.DataAvailable)
                {
                    Console.WriteLine("开始接受客户端命令...");
                    byte[] stream = new byte[256];
                    NS.Read(stream, 0, stream.Length);
                    Console.WriteLine(System.Text.Encoding.UTF8.GetString(stream));
                    SW.WriteLine("登陆成功拉");
                    SW.Flush();
                    string order = SR.ReadLine();
                    Console.WriteLine("命令:{0}", order);
                    string[] spliter = order.Split(',');
                    switch (spliter[0])
                    {
                        //用户登录
                        case "Login":
                            Console.WriteLine("用户试图登陆");
                            SW.Write("Login,1");
                            break;
                        //读取用户列表
                        case "ReadUserList":
                            string tempStr = "ReadUserList,";
                            for (int i = 0; i < UserListTable.Count; i++)
                            {
                                tempStr += UserListTable[i];
                            }
                            SW.Write(tempStr);
                            break;
                    }
                }
            }
        }

为什么我的string order = SR.ReadLine();
读取不到数据?而且程序卡到这不能继续执行~~ --------------------编程问答-------------------- 怎么没人回答呢?555555555555555 --------------------编程问答-------------------- 没有人知道吗?? --------------------编程问答-------------------- 真的没人知道吗?
--------------------编程问答-------------------- //先看看别人是怎么写的
//搜索出来的

void StreamFile(Socket clientSocket)
 {
  NetworkStream networkStream = 
    new NetworkStream(clientSocket);
  System.IO.StreamWriter streamWriter =
    new System.IO.StreamWriter(networkStream);
  System.IO.StreamReader streamReader = 
    new System.IO.StreamReader(@"C:\a.txt");
  string strStream;
  do {
   strStream = streamReader.ReadLine();
   if(strStream != null )
   {
    streamWriter.WriteLine(strStream);
    streamWriter.Flush();
   }
  } while (strStream != null);
  streamReader.Close();
  networkStream.Close();
  streamWriter.Close();         
 }
 
 //Client1 Code
 using System.Net.Sockets;
 TcpClient serverSocket;
 try {
  serverSocket = new TcpClient("localhost", 65000);
 } catch() {}
 NetworkStream networkStream = 
   severSocket.GetStream();
 System.IO.StreamReader streamReader = 
   new System.IO.StreamReader(networkStream);
 try {
  string strOut;
  do {
   if (strOut != null)
   {
    streamReader.ReadLine(strOut);
    if (strOut != null ) 
    {
     Console.WriteLine(strOut);
    }
   }
  } while (strOut != null );
 } catch() {}

------------
纯属顶贴 --------------------编程问答-------------------- 汗,刚才回复了,怎么就不见了,谁给俺删了!
NS.Read(stream, 0, stream.Length);
改为BeginRead 和 EndRead试试
------------
纯属试试
--------------------编程问答-------------------- 有可能是你的socket收发数据没成功 --------------------编程问答-------------------- SR.ReadLine(); 
是要读入一行才能返回,也就是发来的数据中要有换行标记,就是说发送的数据结尾要有'\n' 要是不行试一下 '\r\n'.
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,