我開發這個代碼發送數據,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幫助 和感謝
'catch(Exception){}'很容易掩蓋你的問題。不要這樣做。 – 2012-03-05 16:45:12
所以我做什麼????? – juste3alfaza 2012-03-05 16:47:09