使用套接字类程序挂起? [英] Using socket class program hangs?

查看:57
本文介绍了使用套接字类程序挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面你会找到我正在处理的代码。

这是一个按钮点击例程,并在3或4之后挂起5循环

完成,可能在sock.receive(....)。

这里找到了一些代码(谷歌我的意思)但是惊人的(至少对于

me)如果我一步一步地运行程序或者我取消循环并运行

它再次按下按钮它再也不会挂起。

我想要一个无限循环,所以我的套接字继续听,直到我按b $ b按另一个按钮。

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);


IPAddre ss localIP = IPAddress.Parse(myloc);

EndPoint localEnd =(EndPoint)新IPEndPoint(localIP,generalPort);

udpsock.SetSocketOption(SocketOptionLevel.Socket,

SocketOptionName.ReuseAddress,1);

mysock.Bind(localEnd);

mysock.SetSocketOption(

SocketOptionLevel。 IP,SocketOptionName.AddMembershi p,

new MulticastOption(mcastIP,localIP));

textBox1.AppendText(" Begin datagram:" +" \\ n \\ n");

int n = mysock.Receive(data,mydata.Length,SocketFlags.None);


String s = System.BitConverter.ToString(data,0);

textBox1.AppendText(s);

textBox1.AppendText(" \\\\ n");

textBox1.AppendText(" End of Datagram" +" \\\\ n");

mysock.Close();

}


好​​像套接字缓冲区已满,所以挂起,但是。套接字是每次循环结束时关闭
。等等,任何帮助都将受到赞赏。

感谢。


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.AddMembershi p,
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.

推荐答案

为什么使用SocketOptionName.ReuseAddress?这闻起来好像它可能是问题。

另外,为什么你在循环中反复创建套接字?如果你需要一个监听器,请创建一个套接字并循环接收。


-

William Stacey,MVP


" feel52" < FE ********************* @ europeonline.com>在消息中写道

新闻:Bq ********************* @ news3.tin.it ...
Why are you using SocketOptionName.ReuseAddress? This smells like it could
be the issue.
Also, why are you creating the socket over and over again in the loop? If
you want a listener, create the socket once and loop on receive.

--
William Stacey, MVP

"feel52" <fe*********************@europeonline.com> wrote in message
news:Bq*********************@news3.tin.it...

下面你会找到我正在处理的代码。
这是一个按钮点击例程,挂起后有3或4个有时5个循环完成,可能在sock.receive(....)。
一些代码在这里找到(在谷歌我的意思),但惊人的(至少对我来说)是如果我一步一步地运行程序或我取消循环并按
一次又一次地按下按钮它再也不会挂起。
我想要一个无限循环,所以我的插座一直在听,直到我按下另一个按钮。

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 mcast IP = IPAddress.Parse(myip);

IPAddress localIP = IPAddress.Parse(myloc);
EndPoint localEnd =(EndPoint)新IPEndPoint(localIP,generalPort);
udpsock .SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress,1);
mysock.Bind(localEnd);
mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembershi p,
新的MulticastOption(mcastIP,localIP));

textBox1.AppendText(" Begin datagram:" +" \\\\ n");

int n = mysock.Receive(data,mydata.Length,SocketFlags.None);

String s = System.BitConverter.ToString(data,0);

textBox1.AppendText(s);
textBox1.AppendText(" \\\\\)&;; textBox1.AppendText(" End of Datagram" +" \\\\ n" );
mysock.Close();



似乎套接字缓冲区已满,所以挂起,但是。每次循环结束时,套接字
都会关闭。欢迎任何帮助。
感谢。

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.AddMembershi p,
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.





< br>



" William Stacey [MVP]" < ST *********** @ mvps.org> ha scritto nel messaggio

新闻:eB ************** @ TK2MSFTNGP12.phx.gbl ...

"William Stacey [MVP]" <st***********@mvps.org> ha scritto nel messaggio
news:eB**************@TK2MSFTNGP12.phx.gbl...
你为什么在使用SocketOptionName.ReuseAddress?这有点像
可能是问题。
另外,为什么你在循环中一遍又一遍地创建套接字?如果您想要一个监听器,请创建一次套接字并循环接收。

-
William Stacey,MVP
Why are you using SocketOptionName.ReuseAddress? This smells like it could be the issue.
Also, why are you creating the socket over and over again in the loop? If
you want a listener, create the socket once and loop on receive.

--
William Stacey, MVP



谢谢,

我正在使用重用地址,因为另一个程序从同一个端口接收,

没有它我的引发异常。

现在我会尝试你的第二个提示.Bye。


Thanks,
i''m using Reuse Address because another program receiving from same port,
without it mine raises exception.
Now i will try your second hint.Bye.





除了Stacey之外,我会稍微更改一下代码意见我

强烈建议你做两件事:


1-在工作线程中执行所有套接字操作,不要使用UI线程。

2-你真的需要ReUseAddress和多播吗?


该计划的目的是什么?


干杯,


-

Ignacio Machin,

ignacio.machin at dot.state.fl.us

佛罗里达州交通局


" feel52" < FE ********************* @ europeonline.com>在消息中写道

新闻:Bq ********************* @ news3.tin.it ...
Hi,

I would change the code a little, in addition to Stacey''s opinion I
strongly suggest you two things:

1- Do ALL sockets operations in a worker thread, do not use the UI thread.
2- Do you really need to ReUseAddress and multicast?

What is the intention of the program?

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"feel52" <fe*********************@europeonline.com> wrote in message
news:Bq*********************@news3.tin.it...

下面你会找到我正在处理的代码。
这是一个按钮点击例程,挂起后有3或4个有时5个循环完成,可能在sock.receive(....)。
一些代码在这里找到(在谷歌我的意思),但惊人的(至少对我来说)是如果我一步一步地运行程序或我取消循环并按
一次又一次地按下按钮它再也不会挂起。
我想要一个无限循环,所以我的插座一直在听,直到我按下另一个按钮。

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 mcast IP = IPAddress.Parse(myip);

IPAddress localIP = IPAddress.Parse(myloc);
EndPoint localEnd =(EndPoint)新IPEndPoint(localIP,generalPort);
udpsock .SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress,1);
mysock.Bind(localEnd);
mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembershi p,
新的MulticastOption(mcastIP,localIP));

textBox1.AppendText(" Begin datagram:" +" \\\\ n");

int n = mysock.Receive(data,mydata.Length,SocketFlags.None);

String s = System.BitConverter.ToString(data,0);

textBox1.AppendText(s);
textBox1.AppendText(" \\\\\)&;; textBox1.AppendText(" End of Datagram" +" \\\\ n" );
mysock.Close();



似乎套接字缓冲区已满,所以挂起,但是。每次循环结束时,套接字
都会关闭。欢迎任何帮助。
感谢。

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.AddMembershi p,
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆