Im creating a remote administration tool for my university degree. Im currently really stuck on a bug in my code and was wondering if anyone could shed some light on my issue.
I have Applications, a server and a client. The server runs fine. However the client is the application that freezes.
Before connecting to the server the client works perfectly. When connected to the server the client is constantly frozen on the screen.
I've narrowed the bug down to a specific piece of code, without this code running the application doesn't freeze. However the application also doesn't work.
Here is an example of that code:
private static void ReceiveResponse()
{
var buffer = new byte[2048]; //The receive buffer
try
{
int received = 0;
if (!IsLinuxServer) received = _clientSocket.Receive(buffer, SocketFlags.None); //Receive data from the server
else received = _sslClient.Read(buffer, 0, 2048);
if (received == 0) return; //If failed to received data return
var data = new byte[received]; //Create a new buffer with the exact data size
Array.Copy(buffer, data, received); //Copy from the receive to the exact size buffer
if (isFileDownload) //File download is in progress
{
Buffer.BlockCopy(data, 0, recvFile, writeSize, data.Length); //Copy the file data to memory
writeSize += data.Length; //Increment the received file size
if (writeSize == fup_size) //prev. recvFile.Length == fup_size
{
using (FileStream fs = File.Create(fup_location))
{
Byte[] info = recvFile;
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
Array.Clear(recvFile, 0, recvFile.Length);
SendCommand("frecv");
writeSize = 0;
isFileDownload = false;
return;
}
}
if (!isFileDownload) //Not downloading files
{
string text = (!IsLinuxServer) ? Encoding.Unicode.GetString(data) : Encoding.UTF8.GetString(data); //Convert the data to unicode string
string[] commands = GetCommands(text); //Get command of the message
foreach (string cmd in commands) //Loop through the commands
{
HandleCommand(Decrypt(cmd)); //Decrypt and execute the command
}
}
}
catch (Exception ex) //Somethind went wrong
{
MessageBox.Show(ex.Message);
RDesktop.isShutdown = true; //Stop streaming remote desktop
// MessageBox.Show("Connection ended");
}
}
This code is how the user receives a request from the server. So it is in a timer to be run every 100ms.
Im wondering if the timer has anything to do with it. Before it was in a While(true) loop and I had the same issue which makes me think that it's the actual code making the UI freeze.
The code still works even if the application is frozen, everything on the app works, apart from the UI.
This is really frustrating and I can't really see anything wrong with the code that would cause the application to freeze.
Any help would be much appreciated.
Thankyou In advance.