程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c# tcp備忘及networkstream.length此流不支持查找解決,

c# tcp備忘及networkstream.length此流不支持查找解決,

編輯:C#入門知識

c# tcp備忘及networkstream.length此流不支持查找解決,


服務端


  bool isRunning = true;
  MouseKeyBoard mk = new MouseKeyBoard();

void InitTcpServer(int port)
        {
            Thread t = new Thread(() =>
            {
                TcpListener listener = new TcpListener(IPAddress.Any, port);
                listener.Start();
                Log("監聽:" + port);


                while (isRunning)
                {
                    var tcpClient = listener.AcceptTcpClient();
                    try
                    {
                        Thread t2 = new Thread(() =>
                        {
                            NetworkStream clientStream = tcpClient.GetStream();

                            

                            while (isRunning)
                            {
                                try
                                {
                                    if (!clientStream.DataAvailable)
                                    {
                                        Thread.Sleep(100); //否則死循環,cpu占用超高
                                        continue;
                                    }
                                    byte[] buffer = new byte[tcpClient.Available]; //new byte[clientStream.Length]; //報錯此流不支持查找
                                    int readBytes = 0;
                                    readBytes = clientStream.Read(buffer, 0, buffer.Length); 
                                    clientStream.Flush();
                                    
                                    string request = Encoding.UTF8.GetString(buffer).Trim();
                                    Log(request);


                                    if (request.StartsWith("SetTime|"))
                                    {
                                        var str = DateTime.Parse(request.Substring(8)).ToString("yyyyMMdd") + DateTime.Now.ToString("HHmmss");
                                        SetLocalTimeByStr(str);
                                        clientStream.Flush();
                                        clientStream.Write(Encoding.Default.GetBytes("ok"), 0, 2); //發送給客戶端
                                        clientStream.Flush();
                                    }
                                    else if (request.StartsWith("MouseClick|"))
                                    {
                                        var arr = request.Split('|');
                                        if (arr.Length >= 2)
                                        {
                                            mk.MouseClick(arr[1]);
                                        }
                                    }
                                    else if (request.StartsWith("ShutDown|"))
                                    {
                                        ExecuteCmd("shutdown -s -t 120");
                                    }
                                    else if (request.StartsWith("CancelShutDown|"))
                                    {
                                        ExecuteCmd("shutdown -a");
                                    }else if (request.StartsWith("SendString"))
                                    {
                                        var arr = request.Split('|');
                                        if (arr.Length >= 2)
                                        {
                                            string str = arr[1];
                                            this.button1.BeginInvoke(new Action(() => {
                                                mk.SendKeys(str);
                                            }));
                                            
                                        }
                                    }


                                }
                                catch (Exception ex)
                                {
                                    Log("ex|" + ex.Message + ex.StackTrace);
                                }

                                Thread.Sleep(100);
                            }
                        });
                        t2.Start();
                    }
                    catch (Exception ex2)
                    {
                        Log("ex2|" + ex2.Message + ex2.StackTrace);
                    }

                    Thread.Sleep(100);
                }


            });

            t.Start();
        }

 

客戶端

private void button2_Click(object sender, EventArgs e)
        {
            tcpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.0.168"), Convert.ToInt32(this.textBox2.Text.Trim())));
            MessageBox.Show("連接成功.");
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Thread t = new Thread(() =>
            {
                NetworkStream clientStream = tcpClient.GetStream();
                
                string request = "SetTime|" + this.textBox1.Text.Trim();
                
                byte[] requestBuffer = Encoding.Default.GetBytes(request);
                
                clientStream.Write(requestBuffer, 0, requestBuffer.Length);                

                clientStream.Flush();
                
                var buffer = new byte[1024];
                while (isRunning)
                {
                    clientStream.Read(buffer, 0, buffer.Length);

                    string str = Encoding.Default.GetString(buffer).Trim();
                    if (str.StartsWith("ok"))
                    {
                        Invoke(() =>
                        {
                            MessageBox.Show("修改時間成功.");
                        });

                    }
                    Thread.Sleep(100);
                }
            });
            t.Start();

            this.button1.Enabled = false;
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved