F
feel52
Below you'll find the code i'm working on.
It's in a button click routine and hangs after 3 or 4 sometimes 5 loops
done, probably in sock.receive(....).
Some code was found here( on google i mean) but the amazing( at least for
me) is that if i run the program step by step or i cancel the loop and run
it by pressing the button again and again it never hangs .
I want to have an infinite loop so my socket keeps on listening until i
press another button.
private void button1_Click(object sender, System.EventArgs e)
{
bool start = true;
while (start)
{
byte[] mydata = new Byte[250];
Socket udpsock = new Socket (AddressFamily.InterNetwork,SocketType.Dgram,
ProtocolType.Udp);
int generalPort=myport;
IPAddress mcastIP = IPAddress.Parse(myip);
IPAddress localIP = IPAddress.Parse(myloc);
EndPoint localEnd = (EndPoint) new IPEndPoint(localIP,generalPort);
udpsock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
mysock.Bind(localEnd);
mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembership,
new MulticastOption(mcastIP,localIP));
textBox1.AppendText("Begin datagram:"+"\r\n");
int n = mysock.Receive(data,mydata.Length,SocketFlags.None);
String s =System.BitConverter.ToString(data,0);
textBox1.AppendText(s);
textBox1.AppendText("\r\n");
textBox1.AppendText("End of Datagram"+"\r\n");
mysock.Close();
}
It seems tome that the socket buffer is full so hangs , but. the socket is
closed every time the loop ends..well ,any help will be appreciated.
THANKS.
It's in a button click routine and hangs after 3 or 4 sometimes 5 loops
done, probably in sock.receive(....).
Some code was found here( on google i mean) but the amazing( at least for
me) is that if i run the program step by step or i cancel the loop and run
it by pressing the button again and again it never hangs .
I want to have an infinite loop so my socket keeps on listening until i
press another button.
private void button1_Click(object sender, System.EventArgs e)
{
bool start = true;
while (start)
{
byte[] mydata = new Byte[250];
Socket udpsock = new Socket (AddressFamily.InterNetwork,SocketType.Dgram,
ProtocolType.Udp);
int generalPort=myport;
IPAddress mcastIP = IPAddress.Parse(myip);
IPAddress localIP = IPAddress.Parse(myloc);
EndPoint localEnd = (EndPoint) new IPEndPoint(localIP,generalPort);
udpsock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
mysock.Bind(localEnd);
mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembership,
new MulticastOption(mcastIP,localIP));
textBox1.AppendText("Begin datagram:"+"\r\n");
int n = mysock.Receive(data,mydata.Length,SocketFlags.None);
String s =System.BitConverter.ToString(data,0);
textBox1.AppendText(s);
textBox1.AppendText("\r\n");
textBox1.AppendText("End of Datagram"+"\r\n");
mysock.Close();
}
It seems tome that the socket buffer is full so hangs , but. the socket is
closed every time the loop ends..well ,any help will be appreciated.
THANKS.