Author Topic: C# UDP Sockets  (Read 1906 times)

0 Members and 1 Guest are viewing this topic.

Offline Disc330

  • Contributor
  • New TCC Member
  • *
  • Posts: 15
  • Currently focusing on completing my own projects.
  • Field: Software Development
  • Real Name: Tony Fisher
  • Favorite OS: Windows
  • Programming Language: C#
C# UDP Sockets
« on: August 03, 2010, 07:06:22 PM »
I have been trying for so long now to understand UDP sockets in C#, but I just can't seem to grasp it.

I have recently created an application that requires the use of a UDP server and client, and so I found some examples online and they worked fine. I implemented them into my application via copy and paste... I was amazed when it gave me different results.

I have googled around a bit to find that most peoples problems occur with their router or firewalls, which I thought could not be the case because the examples worked fine on this computer before moving them to the other application...  :o

I am quite clueless now whats going on. I decided to fire up a packet sniffer to see where packets where going, because they are sent... just not received...
The results came back that from source address "0.0.0.0:0" a packet was sent to destination address ":0" (yes, no IP at all... :crazy:).

I have of course coded in the IP and port I WISH to use, and previously when I was using a TCP system that worked fine using the port and IP addresses.

This is the code I use to connect to the server on the client side:
Code: [Select]
this.InnerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


// I have literally no idea what this is meant to do. I have tried a few IP addressed and port 0, all failed to work.
this.TmpRemots = (EndPoint)new IPEndPoint(IPAddress.Any, Port);


this.Server = new IPEndPoint(IPAddress.Parse(IP), Port);

byte[] data = new byte[6] { 0, 0, 0, 0, 0, 0 };

// For some reason, this line fixed the examples.
// Just by sending data before trying to recieve stopped the exception about needing to bind before recieving.
this.InnerSocket.SendTo(data, data.Length, SocketFlags.None, this.Server);

this.RecvThread = new System.Threading.Thread(this.RecvLoop);
this.RecvThread.Start();

And the code for the server to start up:
Code: [Select]

                this.Listener = new IPEndPoint(IPAddress.Any, this._port);
                this.sender = (EndPoint)new IPEndPoint(IPAddress.Any, 0);

                this.InnerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                this.InnerSocket.Bind(this.Listener);

                this.RecvThread = new System.Threading.Thread(this.RecvLoop);
                this.RecvThread.Start();

The receive loops are very similar:

Client Receive loop
Code: [Select]
try
                {
                    BytesRecieved = this.InnerSocket.ReceiveFrom(RawBuffer, ref TmpRemots);
                }
                catch (SocketException Exc)
                {
                    switch (Exc.ErrorCode)
                    {
                        case 10054:
                            {
                                this.Disconnect();
                                break;
                            }
                    }
                }
                if (BytesRecieved > 0)
                {Process();}

And server:
Code: [Select]
int BytesRecieved = this.InnerSocket.ReceiveFrom(this.RawBuffer, ref this.sender);
                if (BytesRecieved > 0)
                {Process();}

As I was saying, the client ends up with some funky destination and the server never receives a packet to reply to.

If you have any ideas on what might be causing this, please do let me know. I don't want to have to start these classes from scratch again. UDP is making me sick.
If it ain't broke... It doesn't have enough features yet. ;D

Offline Disc330

  • Contributor
  • New TCC Member
  • *
  • Posts: 15
  • Currently focusing on completing my own projects.
  • Field: Software Development
  • Real Name: Tony Fisher
  • Favorite OS: Windows
  • Programming Language: C#
Re: C# UDP Sockets
« Reply #1 on: August 03, 2010, 07:13:13 PM »
Oh, and I forgot to mention:
The port I am trying to use is port 9001, but this is what comes up in the packet sniffer:




The port its come from (3220) is wrong. the port its going to is wrong and the IP addresses are defiantly wrong. The data in the packets is accurate though.  :-\
Too confusing for me. I don't even understand the concept of Bind though.  :lol:
If it ain't broke... It doesn't have enough features yet. ;D

Offline Ambercroft

  • Expert
  • Full Member
  • *
  • Posts: 305
  • Tea & Coffee & Code - Xi Robert Technologies
  • Field: Software Development
  • Real Name: Bob Wickson
  • Favorite OS: Linux
  • Programming Language: C
Re: C# UDP Sockets
« Reply #2 on: August 22, 2010, 07:36:51 PM »
It seems from the sniffed data that you are not actually setting the IP and port #s you want to use.

Offline RLondon

  • Randy London
  • Charter Member
  • Full Member
  • *
  • Posts: 167
  • Field: -
  • Real Name: Randy P. L. London
  • Favorite OS: Windows
  • Programming Language: -
Re: C# UDP Sockets
« Reply #3 on: August 24, 2010, 02:32:41 AM »
It's been over a year since I opened a C# IDE, and I never used UDP - but I'll take a stab at it (and probably end up learning something new, instead of helping, but oh well :-/).

Where is the actual code that sets the IP and port to send it to?

Also, in this code:
Code: C# [Select]
  1.  
  2.                 this.Listener = new IPEndPoint(IPAddress.Any, this._port);
  3.                 this.sender = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
  4.  
  5.                 this.InnerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  6.  
  7.                 this.InnerSocket.Bind(this.Listener);
  8.  
  9.                 this.RecvThread = new System.Threading.Thread(this.RecvLoop);
  10.                 this.RecvThread.Start();
  11.  

Do you possibly need to set the IP in [inline=csharp] IPEndPoint(IPAddress.Any, this._port);[/inline]??

Like Ambercroft said, because the data in the packets is right, it seems like the IP and Port just aren't being set somewhere...
« Last Edit: August 24, 2010, 02:35:02 AM by RLondon »

Offline Disc330

  • Contributor
  • New TCC Member
  • *
  • Posts: 15
  • Currently focusing on completing my own projects.
  • Field: Software Development
  • Real Name: Tony Fisher
  • Favorite OS: Windows
  • Programming Language: C#
Re: C# UDP Sockets
« Reply #4 on: August 28, 2010, 12:35:32 AM »
Ahh, I'll regret to inform that I actually lost the original code for it. I got too frustrated and decided to use a UDP client instead which was a lot smoother than a socket. I think You may both be right though... I can't recall assigning the IP or port anywhere.

I was probably attempting to make the socket listen and send on mixed up end points.
If it ain't broke... It doesn't have enough features yet. ;D