Click here to Skip to main content
15,887,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I've been developing simple remote desktop program sending, receiving screen images between server, client and doing mouse control, keyboard control.
when I attempted to send server mouse X,Y position to client to control, client received wrong data. ex) server sends data which is MouseControl$Position$10(Mouse_X_Position)$10(Mouse_Y_Position)$450(pictureBox.Width)$450(pictureBox.Height). it works well at first, client receives the same data that server sent. But, a few seconds later client receives data like ntrol$Position$10(Mouse_X_Position)$10(Mouse_Y_Position)$450(pictureBox.Width)$450(pictureBox.Height). Some part of The first word "MouseControl" is disappeared while server still sends right data "MouseControl$Position$10(Mouse_X_Position)$10(Mouse_Y_Position)$450(pictureBox.Width)$450(pictureBox.Height)"

Help me out of this trouble. I want to know why it is happening
Thank you in advance!

Here is my code below


-server
private void StartMouseControl()
{
NetworkStream MouseControlStream = MouseControlDataSock.GetStream();
byte[] buffer = null;

while (true)
{
Point PointXY = pictureBox1.PointToClient(new Point(Control.MousePosition.X, Control.MousePosition.Y));
int MouseX = PointXY.X;
int MouseY = PointXY.Y;

/* when mouse pointer in pictureBox ... */
if ((MouseX >= 0 && MouseX <= pictureBox1.Width) && (MouseY >= 0 && MouseY <= pictureBox1.Height))
{
MouseControlStream.Flush();
buffer = Encoding.Unicode.GetBytes("MouseControl$Postion$" + MouseX.ToString() + "$" + MouseY.ToString() + "$" + pictureBox1.Width.ToString() + "$" + pictureBox1.Height.ToString());
MouseControlStream.Write(buffer, 0, buffer.Length);
MouseControlStream.Flush();
}

Thread.Sleep(400);
}
}




-client
private void StartMouseControl()
{
int BUFFERSIZE = 72;
int read;
byte[] buffer = new byte[BUFFERSIZE];

while (MouseControlDataSock.Connected)
{
/* seperate protocol data from server and save in Sep_message[] */
MouseControlstream = MouseControlDataSock.GetStream();

read = 0;
while (read < buffer.Length)
{
int readbyte = MouseControlstream.Read(buffer, read, buffer.Length - read);
if (readbyte == 0)
{
break;
}
read += readbyte;
}
MouseControlstream.Flush();

string message = Encoding.Unicode.GetString(buffer, 0, read);

string splitter = "$";
char[] split = splitter.ToArray();

string[] Sep_message = message.Split(split, StringSplitOptions.RemoveEmptyEntries);

if (Sep_message[0].Equals("MouseControl"))
{
if (Sep_message[1].Equals("Postion"))
{
// Mouse_X_Position, Mouse_Y_Position, pictureBox.Width, pictureBox.Height
int MouseX_FromServer = int.Parse(Sep_message[2]);
int MouseY_FromServer = int.Parse(Sep_message[3]);
int PicBoxWidth_FromServer = int.Parse(Sep_message[4]);
int PicBoxHeight_FromServer = int.Parse(Sep_message[5]);
}
}

Thread.Sleep(100);
}
}

What I have tried:

*********************************
Posted
Comments
Richard MacCutchan 5-Jun-17 8:10am    
Yes, because like so many new programmers you assume that a socket receive call will read the complete message each time. But TCP/IP does not work that way; you need to keep reading input until you have the complete message. You also need to keep track of how many characters have been received and ensure you do not overwrite them before reading the next part. Google for "socket samples" and you will find lots of examples that show how to do it properly.
MinYoung Lee 5-Jun-17 10:54am    
Thank you, Richard MacCutchan! Yeah, as you said, I didn't know how it works. I will get to leran more about socket. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900