Socket fail to receive screenshot and text on same algorithm

  • Thread starter Thread starter FLASHCODER
  • Start date Start date
F

FLASHCODER

Guest
I'm testing this code example of Socket in blocking-mode and works fine to receive only screenshot in real time (like promissed by author Iridium), already when i try receive a string (eg: "PING" each 5 seconds) and the screenshot on same algorithm, after somes seconds Socket fails on receiving without throw none type of exception, only crashes whole my app (locking all system).

Someone already had this problem and could share your solution?

Here was my last attempt to also capture some text received (in this case is "PING" sent by client each 5 seconds):

var message = String.Empty;
var lengthData = new byte[4];
var lengthBytesRead = 0;

while (lengthBytesRead < lengthData.Length)
{
var read = clientSocket.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);
if (read <= 0) return;
lengthBytesRead += read;
}

if (BitConverter.IsLittleEndian)
Array.Reverse(lengthData);

var length = BitConverter.ToInt32(lengthData, 0);

if (length > 0)
{
var data = new byte[length];
var bytesRead = 0;

while (bytesRead < data.Length)
{
var read = clientSocket.Receive(data, bytesRead, data.Length - bytesRead, SocketFlags.None);
if (read <= 0) return;
bytesRead += read;
message += Encoding.UTF8.GetString(data, 0, read);
}

if (message.Contains("PING"))

// processing text... (message);

else
if (message.Contains("SCREEN"))

// processing screenshot... (data);
}

Continue reading...
 
Back
Top