Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
Delphi 10.2 Tokyo Professional
|
Seltsames Phänomen beim Senden von Daten
11. Dez 2008, 00:29
Hi,
Ich sende gerade mit einem C# Socket folgendermaßen Daten:
Code:
void Button1Click(object sender, EventArgs e)
{
srv.Bind(new IPEndPoint(IPAddress.Any,7));
srv.Listen(5);
Socket client = srv.Accept();
byte[] buff = {0x48,0x61,0x6C,0x6C,0x6F,0x20};
client.Send(buff,buff.Length,SocketFlags.None);
byte[] buff2 = {0x43,0x68,0x69,0x6E,0x61};
client.Send(buff2,buff2.Length,SocketFlags.None);
client.Shutdown(SocketShutdown.Both);
client.Close();
}
Jetzt dachte ich mir: Machen wir das ganze noch schöner indem ich en try-finally block drumsetze:
Code:
void Button1Click(object sender, EventArgs e)
{
srv.Bind(new IPEndPoint(IPAddress.Any,7));
srv.Listen(5);
Socket client = srv.Accept();
try
{
byte[] buff = {0x48,0x61,0x6C,0x6C,0x6F,0x20};
client.Send(buff,buff.Length,SocketFlags.None);
byte[] buff2 = {0x43,0x68,0x69,0x6E,0x61};
client.Send(buff2,buff2.Length,SocketFlags.None);
}
finally
{
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
Und was geschieht? buff kommt nichtmehr beim Client an! buff2 schon. Folgendes funktioniert allerdings:
Code:
void Button1Click(object sender, EventArgs e)
{
srv.Bind(new IPEndPoint(IPAddress.Any,7));
srv.Listen(5);
Socket client = srv.Accept();
try
{
byte[] buff = {0x48,0x61,0x6C,0x6C,0x6F,0x20};
byte[] buff2 = {0x43,0x68,0x69,0x6E,0x61};
client.Send(buff,buff.Length,SocketFlags.None);
client.Send(buff2,buff2.Length,SocketFlags.None);
}
finally
{
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
Und wenn ich buff und buff2 vor dem try-finally block deklariere, dann gehts auch.
Gibts dafür eine Erklärung?
Gruß
Neutral General
Michael "Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
|