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. How to write a Async UDP socket code?

How to write a Async UDP socket code?

Scheduled Pinned Locked Moved C#
questiontutorial
5 Posts 2 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.
  • F Offline
    F Offline
    fu0
    wrote on last edited by
    #1

    I want to create a Listening UDP socket in Async Mode. here is my code: private void button7_Click(object sender, System.EventArgs e) { sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); iep = new IPEndPoint(IPAddress.Any,20100); sock.Bind(iep); sock.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock); } private void ReceiveData(IAsyncResult iar) { Socket remote = (Socket)iar.AsyncState; int recv = remote.EndReceive(iar); Console.WriteLine("Recv {0}:",recv); remote.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock); } ////////////////////////////// i want the sock to keep listening. However, once i send a udp message with UDPClient from another pc, the ReceiveData(IAsyncResult iar) method turns into a loop keeping writeline. what is wrong ?

    H 1 Reply Last reply
    0
    • F fu0

      I want to create a Listening UDP socket in Async Mode. here is my code: private void button7_Click(object sender, System.EventArgs e) { sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); iep = new IPEndPoint(IPAddress.Any,20100); sock.Bind(iep); sock.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock); } private void ReceiveData(IAsyncResult iar) { Socket remote = (Socket)iar.AsyncState; int recv = remote.EndReceive(iar); Console.WriteLine("Recv {0}:",recv); remote.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock); } ////////////////////////////// i want the sock to keep listening. However, once i send a udp message with UDPClient from another pc, the ReceiveData(IAsyncResult iar) method turns into a loop keeping writeline. what is wrong ?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Is it the same data (assuming that you're sending unique data in the UDP datagrams)?

      Microsoft MVP, Visual C# My Articles

      F 1 Reply Last reply
      0
      • H Heath Stewart

        Is it the same data (assuming that you're sending unique data in the UDP datagrams)?

        Microsoft MVP, Visual C# My Articles

        F Offline
        F Offline
        fu0
        wrote on last edited by
        #3

        i send the udp datagrams only once using this code: UdpClient udp = new UdpClient(); byte[] data = new byte[]{0x11,0x22,0x33}; udp.Send(data,3,ip,port); at the server, Console keep showing this: recv :16 recv :18 recv :16 recv :16 ... it seems there ocurrs a loop in the ReceiveData(Async iar) method. i dont know what happened.

        H 1 Reply Last reply
        0
        • F fu0

          i send the udp datagrams only once using this code: UdpClient udp = new UdpClient(); byte[] data = new byte[]{0x11,0x22,0x33}; udp.Send(data,3,ip,port); at the server, Console keep showing this: recv :16 recv :18 recv :16 recv :16 ... it seems there ocurrs a loop in the ReceiveData(Async iar) method. i dont know what happened.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Assuming that the code you posted is the exact same code (people sometimes post sample code that is nothing like their actual code and doesn't contain the problem, so I just have to say it), I see no loop. What I would check out is the other machine's code. Perhaps it's looping infinitely and your client code is working correctly (seeing each UDP packet as it arrives).

          Microsoft MVP, Visual C# My Articles

          F 1 Reply Last reply
          0
          • H Heath Stewart

            Assuming that the code you posted is the exact same code (people sometimes post sample code that is nothing like their actual code and doesn't contain the problem, so I just have to say it), I see no loop. What I would check out is the other machine's code. Perhaps it's looping infinitely and your client code is working correctly (seeing each UDP packet as it arrives).

            Microsoft MVP, Visual C# My Articles

            F Offline
            F Offline
            fu0
            wrote on last edited by
            #5

            Thank for your help. While i test my code again, there comes out correct result. i am confused for my code being not changed at all.

            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