小编Anu*_*N C的帖子

将try-catch放在循环中是不错的做法,直到try块中的所有语句都被执行而没有任何异常?

我正在尝试开发多播接收器程序并完成套接字初始化,如下所示:

    public void initializeThread()
    {
        statuscheckthread = new Thread(SetSocketOptions);
        statuscheckthread.IsBackground = true;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        rxsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        iep = new IPEndPoint(IPAddress.Any, 9191);
        rxsock.Bind(iep);
        ep = (EndPoint)iep;

        initializeThread();
        statuscheckthread.Start();
    }

    public void SetSocketOptions()
    {
         initializeThread(); //re-initializes thread thus making it not alive
         while (true)
         {
             if (NetworkInterface.GetIsNetworkAvailable())
             {
                 bool sockOptnSet = false;
                 while (!sockOptnSet)
                 {
                     try
                     {
                         rxsock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.50.50.50")));
                         rxsock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 64);
                         sockOptnSet = true;
                     }
                     catch
                     {
                         //Catch exception …
Run Code Online (Sandbox Code Playgroud)

c# sockets try-catch

8
推荐指数
1
解决办法
812
查看次数

标签 统计

c# ×1

sockets ×1

try-catch ×1