Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Udp client Programming

Udp client Programming

Scheduled Pinned Locked Moved C#
help
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jimmanuel

    I had a similar situation not too long ago. To solve the problem I used the following snippet in a separate thread; to close the port just set UDPrunning to false, wait for the thread to exit and then you can close the port. I'm not sure if it's the best solution, but it is a solution that worked in my situation.

            while (UDPrunning == true)
            {
                if (udpSocket.Available > 0)
                {
                    try
                    {
                        IPEndPoint msgSenderInfo = new IPEndPoint(IPAddress.Any, 0);
                        Byte\[\] incomingData = udpSocket.Receive(ref msgSenderInfo);
                        // do something with the data
                    }
                    catch (SocketException e)
                    {
                        // do something to handle the exception
                    }
                }
                else
                {
                    // Stop the thread from using too much processor time
                    Thread.Sleep(100);
                }
            }
    
    R Offline
    R Offline
    ramdil
    wrote on last edited by
    #3

    Hi thanks for the reply But i have doubt regarding one of ur suggestions. You comment was "to close the port just set UDPrunning to false, wait for the thread to exit and then you can close the port" here the variable UDP running i have set to false in the close event of my application.and after seting the variable i am closing the port..is this the what you meant...if it is then no problem but i got confused that you were speaking abt the wait for the thread how should i wait for thread.. can you pls explain.

    Regards DilipRam

    J 1 Reply Last reply
    0
    • R ramdil

      Hi thanks for the reply But i have doubt regarding one of ur suggestions. You comment was "to close the port just set UDPrunning to false, wait for the thread to exit and then you can close the port" here the variable UDP running i have set to false in the close event of my application.and after seting the variable i am closing the port..is this the what you meant...if it is then no problem but i got confused that you were speaking abt the wait for the thread how should i wait for thread.. can you pls explain.

      Regards DilipRam

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #4

      That's exactly what I meant! To stop listening all you have to so is set that var to false and continue on your way. The comment about waiting for the thread is because I was running that method in a separate Thread from the gui thread. When I was shutting down my gui I wanted to make sure that the listener thread exited so that it wouldn't hang up my application. The Thread.Join() method waits for a thread to exit, so my socket shutdown code looked like this:

      UDPrunning = false;
      udpSocketThread.Join();
      udpSocket.Close();

      L R 2 Replies Last reply
      0
      • J Jimmanuel

        That's exactly what I meant! To stop listening all you have to so is set that var to false and continue on your way. The comment about waiting for the thread is because I was running that method in a separate Thread from the gui thread. When I was shutting down my gui I wanted to make sure that the listener thread exited so that it wouldn't hang up my application. The Thread.Join() method waits for a thread to exit, so my socket shutdown code looked like this:

        UDPrunning = false;
        udpSocketThread.Join();
        udpSocket.Close();

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #5

        Hi, I would not like a Join in my closing code, it might sit and wait forever. Isnt the essence of the problem that you want the UDP listener thread to be a background thread, one that does not prevent your app from exiting ? :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        R J 2 Replies Last reply
        0
        • L Luc Pattyn

          Hi, I would not like a Join in my closing code, it might sit and wait forever. Isnt the essence of the problem that you want the UDP listener thread to be a background thread, one that does not prevent your app from exiting ? :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          R Offline
          R Offline
          ramdil
          wrote on last edited by
          #6

          Hi can you bit clear please as i am new this socket programming and any way i am calling the receive method in a thread...

          Regards DilipRam

          1 Reply Last reply
          0
          • J Jimmanuel

            That's exactly what I meant! To stop listening all you have to so is set that var to false and continue on your way. The comment about waiting for the thread is because I was running that method in a separate Thread from the gui thread. When I was shutting down my gui I wanted to make sure that the listener thread exited so that it wouldn't hang up my application. The Thread.Join() method waits for a thread to exit, so my socket shutdown code looked like this:

            UDPrunning = false;
            udpSocketThread.Join();
            udpSocket.Close();

            R Offline
            R Offline
            ramdil
            wrote on last edited by
            #7

            Thanks for the reply.. just want to confirm that the join method will not get into any hang mode. because i am calling the stop method by creating a object of dll and i dont have a thread in my application but the thread is present in dll where i have written the recieve method...

            Regards DilipRam

            J 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, I would not like a Join in my closing code, it might sit and wait forever. Isnt the essence of the problem that you want the UDP listener thread to be a background thread, one that does not prevent your app from exiting ? :)

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              J Offline
              J Offline
              Jimmanuel
              wrote on last edited by
              #8

              That essentially was the idea but there are 2 reasons that I didn't set udpSocketThread.IsBackground = true; and forget about it: 1) The worker function is coded to avoid a blocking situation where the thread would hang so I wasn't worried about it waiting forever and 2) I didn't previously know about the Background vs. Foreground Thread property. That's a pretty nifty Property to have, thanks for the info! :)

              L 1 Reply Last reply
              0
              • R ramdil

                Thanks for the reply.. just want to confirm that the join method will not get into any hang mode. because i am calling the stop method by creating a object of dll and i dont have a thread in my application but the thread is present in dll where i have written the recieve method...

                Regards DilipRam

                J Offline
                J Offline
                Jimmanuel
                wrote on last edited by
                #9

                See my reply to Lucs post. I just found this[^] article explaining background vs. foreground threads, so apparently if you set udpSocketThread.IsBackground = true; then you won't ever have to worry about Joining the thread.

                R 1 Reply Last reply
                0
                • J Jimmanuel

                  See my reply to Lucs post. I just found this[^] article explaining background vs. foreground threads, so apparently if you set udpSocketThread.IsBackground = true; then you won't ever have to worry about Joining the thread.

                  R Offline
                  R Offline
                  ramdil
                  wrote on last edited by
                  #10

                  Thanks again and sorry for troubling again.just to ensure that instead of doing UdpListnerThread.Join(); in my stop method i need to set udpSocketThread.IsBackground = true.is that it means thanks

                  Regards DilipRam

                  L J 2 Replies Last reply
                  0
                  • J Jimmanuel

                    That essentially was the idea but there are 2 reasons that I didn't set udpSocketThread.IsBackground = true; and forget about it: 1) The worker function is coded to avoid a blocking situation where the thread would hang so I wasn't worried about it waiting forever and 2) I didn't previously know about the Background vs. Foreground Thread property. That's a pretty nifty Property to have, thanks for the info! :)

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #11

                    Yes, but my point is when the listener is in its Receive() method, it needs another packet to resume its course, and only after that it could exit the loop, so your Join() is actually waiting for another UDP packet to arrive (which may never happen). :)

                    Luc Pattyn


                    try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                    J 1 Reply Last reply
                    0
                    • R ramdil

                      Thanks again and sorry for troubling again.just to ensure that instead of doing UdpListnerThread.Join(); in my stop method i need to set udpSocketThread.IsBackground = true.is that it means thanks

                      Regards DilipRam

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      That's how I would do it anyway. :)

                      Luc Pattyn


                      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                      R 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Yes, but my point is when the listener is in its Receive() method, it needs another packet to resume its course, and only after that it could exit the loop, so your Join() is actually waiting for another UDP packet to arrive (which may never happen). :)

                        Luc Pattyn


                        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                        J Offline
                        J Offline
                        Jimmanuel
                        wrote on last edited by
                        #13

                        In my situation I wasn't worried about the Ethernet cable being pulled. It was just a little test app connecting to a server on the local machine as a proof-of-concept of another piece of code. That's why I said in my original post "I'm not sure if it's the best solution, but it is a solution that worked in my situation." Using IsBackground would have been a wiser choice had I known about it.

                        1 Reply Last reply
                        0
                        • R ramdil

                          Thanks again and sorry for troubling again.just to ensure that instead of doing UdpListnerThread.Join(); in my stop method i need to set udpSocketThread.IsBackground = true.is that it means thanks

                          Regards DilipRam

                          J Offline
                          J Offline
                          Jimmanuel
                          wrote on last edited by
                          #14

                          Yup, Listen to Luc

                          1 Reply Last reply
                          0
                          • L Luc Pattyn

                            That's how I would do it anyway. :)

                            Luc Pattyn


                            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                            R Offline
                            R Offline
                            ramdil
                            wrote on last edited by
                            #15

                            Thanks for replies..finally my stop method will be having the following code since i am using two udps and thread name is udpManagerbroadCast.Close();///..udp UdpListnerThread.IsBackground = true;///..thread FlagVal = true;///flag value soUdp.Close();///..udp and hope this is what you all were suggesting.Hope this fit the standards which you were suggesting

                            Regards DilipRam

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups