发送数据时出错

时间:2012-03-05 16:42:10

标签: c# windows-phone-7

我开发此代码来发送数据,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();
        }
    }

我不知道为什么它不发送数据,或者可能不接收数据 请帮助 并谢谢

1 个答案:

答案 0 :(得分: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) { }

如果您有异常,请保留隐藏您的例外,因此请尝试将其更改为

        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) { }