2014-12-23 47 views
0

非常簡單的問題。我通過按鈕按下動作創建的線程中的while循環只發生一次,「this」不再重複。它確實收到第一條消息,但由於while循環被卡住,未來的消息不會被收到。第一條消息確實打印出來。雖然(真)只發生一次沒有錯誤

此外,在打印消息之後,切換循環從不發生。

 private void button1_Click(object sender, EventArgs e) 
    { 
     Thread Listener = new Thread(ServerListener); 
     Listener.Start(); 
    } 



    public void ServerListener() 
    { 
     byte[] buf; 
     NetworkStream serverStream = clientSocket.GetStream(); 
     while (true) 
     { 
      Console.WriteLine("this"); 
      if (serverStream.DataAvailable) 
      { 
       buf = new byte[clientSocket.ReceiveBufferSize]; 
       serverStream.Read(buf, 0, buf.Length); 
       string stringdata = Encoding.ASCII.GetString(buf); 
       Console.WriteLine("Received message: " + stringdata); 

       switch (stringdata) 
       { 
        case "ping": 
         buf = Encoding.ASCII.GetBytes("pong"); 
         Console.WriteLine("Sent pong"); 
         serverStream.Write(buf, 0, buf.Length); 
         serverStream.Flush(); 
         break; 
        case "handshake": 
         buf = Encoding.ASCII.GetBytes("lol"); 
         Console.WriteLine("Sent confirmation"); 
         serverStream.Write(buf, 0, buf.Length); 
         serverStream.Flush(); 
         break; 
       } 

      } 
     } 
    } 

我確定有人會來告訴我使用任務。我知道如何使用任務,但我發現任務與長時間運行選項和普通線程之間沒有區別。該工作

+1

您是否嘗試過在調試器中逐步查看它在做什麼? – cost

+0

您的線程死亡,但有一個例外。抓住並檢查它。 – dymanoid

+0

我嘗試捕獲Saruman所述的任何異常。 –

回答

0

我已經瞭解到我需要將我的傳出數據框起來。有關框架示例,請查看下面的代碼。框架告訴接收器需要多少數據。

public void SendResponse(int command, Object[] args) 
    { 
     if (ClientSocket == null) 
     { 
      Console.WriteLine("Command: ClientSocket"); 
      return; 
     } 
     var serverStream = this.ClientSocket.GetStream(); 
     if (!serverStream.CanRead || !serverStream.CanWrite) 
     { 
      Console.WriteLine("Command: serverStream Error"); 
      return; 
     } 
     byte[] toSend = null; 
     switch (command) 
     { 
      // 0 - genneral, 1 - handshake response 
      case 0: 
       toSend = Encoding.ASCII.GetBytes(args[0].ToString()); 
       break; 
      case 1: 
       Rectangle bounds = Screen.GetBounds(Point.Empty); 
       using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555)) 
       { 
        using (Graphics g = Graphics.FromImage(bitmap)) 
        { 
         g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size); 
        } 
        toSend = ImageToByte(bitmap); 
       } 
       break; 
     } 

     try 
     { 
//--- Framing occurs here mainly 
      byte[] bufferedToSend = new byte[toSend.Length + 4]; 
      byte[] lengthOfSend = BitConverter.GetBytes(toSend.Length); 
      Array.Copy(lengthOfSend, bufferedToSend, 4); 
      toSend.CopyTo(bufferedToSend, 4); 
      serverStream.Write(bufferedToSend, 0, bufferedToSend.Length); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
     } 
    } 
0

確定現在我張貼代碼

客戶:

using System; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
using System.Net.Sockets; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     TcpClient clientSocket; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click_1(object sender, EventArgs e) 
     { 
      clientSocket = new TcpClient("127.0.0.1", 8001); // Note the change 
      Thread Listener = new Thread(ServerListener); 
      Listener.Start(); 
     } 

     public void ServerListener() 
     { 
      byte[] buf; 
      Console.WriteLine("Thread started"); 

      NetworkStream serverStream = clientSocket.GetStream(); 
      while (true) 
      { 
       Console.WriteLine("this"); 
       if (serverStream.DataAvailable) 
       { 
        buf = new byte[clientSocket.ReceiveBufferSize]; 
        serverStream.Read(buf, 0, buf.Length); 
        string stringdata = Encoding.ASCII.GetString(buf); 
        Console.WriteLine("Received message: " + stringdata); 

        switch (stringdata) 
        { 
         case "ping": 
          buf = Encoding.ASCII.GetBytes("pong"); 
          Console.WriteLine("Sent pong"); 
          serverStream.Write(buf, 0, buf.Length); 
          serverStream.Flush(); 
          break; 
         case "handshake": 
          buf = Encoding.ASCII.GetBytes("lol"); 
          Console.WriteLine("Sent confirmation"); 
          serverStream.Write(buf, 0, buf.Length); 
          serverStream.Flush(); 
          break; 
        } 

       } 
      } 
     } 
    } 
} 

SERVER:

using System; 
using System.Linq; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      try 
      { 
       IPAddress IP = IPAddress.Parse("127.0.0.1"); 

       TcpListener Listener = new TcpListener(IP, 8001); 
       Listener.Start(); 

       Socket s = Listener.AcceptSocket(); 
       Console.WriteLine("Conected"); 
       ASCIIEncoding asen = new ASCIIEncoding(); 
       s.Send(asen.GetBytes("ping")); 
       Console.WriteLine("Data sent"); 

       s.Close(); 
       Listener.Stop(); 

      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Error..... " + e.StackTrace); 
      } 

      Console.ReadKey(); 
     } 
    } 
} 

問題你有:

我認爲這是與您的服務器或者你沒有向我們展示的代碼。在服務器上方作爲控制檯應用程序和客戶端運行winform應用程序。