画像を継続的に送信しようとしていますが、どういうわけか機能していません。問題は、受信者がサイズを文字列に読み取ろうとするときに、不明な 6 バイトを読み取ることがあるということです。すべてのバイトが読み取られたときにネットワークストリームが空であると考えているため、それらがどこから来たのかわかりません。
/// <summary>
/// This is the class that captures the screen and sends it
/// </summary>
public class TCPClient
{
TcpClient me;
bool lauf = true;
NetworkStream ns;
Thread mainThread;
int ind = 0;
public TCPClient()
{
mainThread = new Thread(new ThreadStart(sendThread));
mainThread.Start();
}
public void sendThread()
{
try
{
me = new TcpClient("127.0.0.1",16500);
ns = me.GetStream();
while (lauf)
{
MemoryStream tempMS = this.getScreen();
byte[] package = tempMS.ToArray();
String tempS = "" + package.Length;
byte[] tempBS = System.Text.Encoding.UTF8.GetBytes(tempS);
ns.Write(tempBS, 0, tempBS.Length);
ns.Write(package, 0, package.Length);
Console.WriteLine("Paket gesendet " + package.Length);
//package = null;
Console.WriteLine("Memoryuse: " + Convert.ToDouble(((Process.GetCurrentProcess().WorkingSet64))/1024)/1024);
Thread.Sleep(1000);
//Die ms müssen für die Steuerung evtl runtergeregelt werden,da sonst eine komfortable steuerung nicht möglich ist
}
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadKey();
mainThread.Abort();
ns.Close();
me.Close();
}
}
public MemoryStream getScreen()
{
Bitmap bitmap = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(0,0,0,0,bitmap.Size);
Cursors.Default.Draw(graphics,new Rectangle(Cursor.Position,Cursor.Current.Size));
graphics.Dispose();
}
MemoryStream memoryStream = new System.IO.MemoryStream();
bitmap.Save(memoryStream,System.Drawing.Imaging.ImageFormat.Png);
bitmap.Dispose();
//memoryStream.Flush();
memoryStream.Position = 0;
return memoryStream;
}
public void setLauf(bool pLauf)
{
lauf = pLauf;
}
}
public class TcpReceiver
{
private TcpClient me;
private MainForm MF;
private bool lauf = true;
private Thread recieveThread;
public TcpReceiver(MainForm tMF)
{
MF = tMF;
recieveThread = new Thread(new ThreadStart(recieveImage));
recieveThread.Start();
}
public void recieveImage()
{
try
{
TcpListener tempListener = new TcpListener(IPAddress.Any,16500);
tempListener.Start();
me = tempListener.AcceptTcpClient();
//MF.newClient(me.Client.AddressFamily.ToString());
NetworkStream ns = me.GetStream();
while(lauf)
{
if (ns.DataAvailable)
{
byte[] tempSA = new byte[6];
ns.Read(tempSA, 0, tempSA.Length);
String tempS = System.Text.Encoding.UTF8.GetString(tempSA);
int size = Int32.Parse(tempS);
byte[] tempBA = new byte[size];
ns.Read(tempBA, 0, tempBA.Length);
MemoryStream tempMS = new MemoryStream(tempBA);
Bitmap tempBitmap = new Bitmap(tempMS);
MF.showData(tempBitmap);
Thread.Sleep(100);
}
}
}
catch(Exception e)
{
}
}
public void setLauf(bool tLauf)
{
lauf = tLauf;
}
}