2012-03-05 77 views
-1

我開發這個代碼發送數據,x & y座標WP7屏幕到PC服務器,但它不工作,但調試器工作正常,我相信我的代碼是正確的。 有證據顯示 此代碼爲WP7客戶端錯誤發送數據

TransformGroup transformG; 
    TranslateTransform translation; 
    double DeltaX, DeltaY; 

    private Socket client_socket; 
    private delegate void mydelegate(byte[] buffer); 
    private delegate void ShowMessagedelegate(string MSG); 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 


     this.ManipulationDelta += new EventHandler<ManipulationDeltaEventArgs>(MainPage_ManipulationDelta); 
     transformG = new TransformGroup(); 
     translation = new TranslateTransform(); 
     transformG.Children.Add(translation); 
    } 
    void MainPage_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) 
    { 

     DeltaX = e.DeltaManipulation.Translation.X; 
     DeltaY = e.DeltaManipulation.Translation.Y; 

    } 
    void StartSending() 
    { 
      try 
      { 
       SocketAsyncEventArgs socketEventArgs = new SocketAsyncEventArgs(); 
       byte[] buffer = Encoding.UTF8.GetBytes(DeltaX.ToString()+"/"+DeltaY.ToString()); 
       socketEventArgs.SetBuffer(buffer, 0, buffer.Length); 
       client_socket.SendToAsync(socketEventArgs); 
      } 
      catch (Exception) { } 
    } 


    void Conncet(string IP_Address) 
    { 

     client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs() 
     { 
      RemoteEndPoint = new IPEndPoint(IPAddress.Parse(IP_Address), 4532) 
     }; 
     socketEventArg.Completed += OnConncetCompleted; 
     client_socket.ConnectAsync(socketEventArg); 
    } 





    private void ConnectBTN_Click(object sender, RoutedEventArgs e) 
    { 
     Conncet(IPAddress_TXT.Text); 
     Thread th = new Thread(new ThreadStart(StartSending)); 
    } 

沒有錯誤,此代碼爲服務器端

[DllImport("user32.dll")] 
    static extern bool GetCursorPos(ref Point lpPoint); 
public Int32 pX, pY; 
Socket SenderSocket; 
void StartReceiving() 
    { 
     byte[] response = new byte[10000]; 
     SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); 
     socketEventArg.Completed += OnReceiveCompleted; 
     socketEventArg.SetBuffer(response, 0, response.Length); 
     SenderSocket.ReceiveAsync(socketEventArg); 
    } 

    void OnReceiveCompleted(object sender, SocketAsyncEventArgs e) 
    { 
     //this.Dispatcher.BeginInvoke(new mydelegate(ModifCursorPos), e.Buffer); 
     this.BeginInvoke(new mydelegate(ModifCursorPos), e.Buffer); 
    } 
    private void ModifCursorPos(byte[] buffer) 
    { 
     try 
     { 
      MemoryStream ms = new MemoryStream(buffer); 
      string chaine = Encoding.UTF8.GetString(buffer); 
      string[] pos = chaine.Split('/'); 

       pX = Convert.ToInt32(pos[0]); 
       pY = Convert.ToInt32(pos[1]); 
       //posX.Text = pX.ToString(); 
       //posY.Text = pY.ToString(); 

      ms.Close(); 

      this.Cursor = new Cursor(Cursor.Current.Handle); 
      Cursor.Position = new Point(Cursor.Position.X + pX, Cursor.Position.Y + pY); 
      posX.Text = Cursor.Position.X.ToString();//try to get X in textbox 
      posY.Text = Cursor.Position.Y.ToString();//try to get Y in textbox 
     } 
     catch (Exception) { } 
     finally 
     { 
      StartReceiving(); 
     } 
    } 

我不知道爲什麼它不發送數據,也可能不收到 plz幫助 和感謝

+0

'catch(Exception){}'很容易掩蓋你的問題。不要這樣做。 – 2012-03-05 16:45:12

+0

所以我做什麼????? – juste3alfaza 2012-03-05 16:47:09

回答

0
 try 
     { 
      SocketAsyncEventArgs socketEventArgs = new SocketAsyncEventArgs(); 
      byte[] buffer = Encoding.UTF8.GetBytes(DeltaX.ToString()+"/"+DeltaY.ToString()); 
      socketEventArgs.SetBuffer(buffer, 0, buffer.Length); 
      client_socket.SendToAsync(socketEventArgs); 
     } 
     catch (Exception) { } 

這將繼續隱藏自己的異常,如果你有一個這樣的TR Ÿ改變這

 try 
     { 
      SocketAsyncEventArgs socketEventArgs = new SocketAsyncEventArgs(); 
      byte[] buffer = Encoding.UTF8.GetBytes(DeltaX.ToString()+"/"+DeltaY.ToString()); 
      socketEventArgs.SetBuffer(buffer, 0, buffer.Length); 
      client_socket.SendToAsync(socketEventArgs); 
     } 
     catch (Exception e) { Console.Write(e.Tostring()); } 

這將有助於弄清楚是什麼原因造成的問題,然後,如果你得到一個異常張貼在這裏了,我們可以幫助您更

也同樣的事情,這一個 試MemoryStream ms = new MemoryStream(buffer); string chaine = Encoding.UTF8.GetString(buffer); string [] pos = chaine.Split('/');

  pX = Convert.ToInt32(pos[0]); 
      pY = Convert.ToInt32(pos[1]); 
      //posX.Text = pX.ToString(); 
      //posY.Text = pY.ToString(); 

     ms.Close(); 

     this.Cursor = new Cursor(Cursor.Current.Handle); 
     Cursor.Position = new Point(Cursor.Position.X + pX, Cursor.Position.Y + pY); 
     posX.Text = Cursor.Position.X.ToString();//try to get X in textbox 
     posY.Text = Cursor.Position.Y.ToString();//try to get Y in textbox 
    } 
    catch (Exception) { } 
+0

好吧,我添加異常,它顯示我eroor,我創建一個視頻 http://www.youtube.com/watch?v=K7N0Ls__qm0&feature=youtu.be – juste3alfaza 2012-03-05 18:20:32

+0

女巫嘗試抓住拋出錯誤? – Shredder2500 2012-03-05 18:45:18

+0

WP7客戶端 – juste3alfaza 2012-03-05 18:49:37