Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I've written a program that provides mIRC-like interface/textchat(using tcp socket) and also with audio-chat(using a UDP socket).

The program is the typical client-server setup.

The thing is, TCP packets are sent and received just fine both in my local network and from clients in the global network.

But when it comes to the UDP connection, only the local packet transfer works O_O

Its about 1000 lines, so I'll copy in the relevant UDP socket snippets :)

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

IPEndPoint clientEP = new IPEndPoint(IPAddress.Any,this.clientPort); 
this.serverEP = new IPEndPoint(IPAddress.Parse(this.textbox_connect.Text),serverPort); // the IP is correct 

this.audioSocket.BeginConnect(this.serverEP, new AsyncCallback(audioConnectCallback), this.audioSocket);
                                this.textbox_display.Text = ("sending sh*t to: " + serverEP.ToString());
StateObject state_audio = new StateObject();
                                state_audio.getWorkSocket().BeginReceive(state_audio.getBuffer(), 0, state_audio.getBufferSize(), 0, new AsyncCallback(audioReceive),state_audio);

private void audioReceive(IAsyncResult asyn)
            int readBytes = 0;
                StateObject state = (StateObject)asyn.AsyncState;
                readBytes = state.getWorkSocket().EndReceive(asyn);
                this.waveProvider.AddSamples(state.getBuffer(), 0, readBytes);
                this.audioSocket.BeginReceive(state.getBuffer(), 0, state.getBufferSize(), SocketFlags.None, new AsyncCallback(audioReceive), state);
            catch (Exception ez)
                MessageBox.Show("audioReceive: " + this.textbox_nick.Text + "        " +ez.ToString());

Not sure what else to say, or if this code says anything at all :( is there anything obvious that I am missing, that would make this work on a local network but not outside it?

Its really wierd because there is no exceptions or anything thrown(on any side) when running, nothing just gets received/sent from/to the client outside.

If there is any gaps just let me know!

Updated 7-Jan-21 7:16am
akaEcho 15-Nov-13 23:25pm
Hi DasKaiserJohan. I am facing exactly the same problem you had. Have you found a solution for it?

Perhaps the global address your trying to connect to is behind a router/firewall?
DasKaiserJohan 1-Dec-10 19:09pm
Does a firewall stop incomming UDP traffic like that? For example, if I fire up a game like Call of Duty, I havn't configured port forwarding for any steam/call of duty ports and yet my packets get sent/received.

Also, I keep mixing it up; If I send packets to a global address, does that global address also contain the local IP of the target machine or does the router resend the packet to any local client listening on that specfic port?
Like the others said, probably a firewall. Temporarily change your app to use port 53 on both ends. UDP 53 is normally used for DNS queries and allowed through most firewalls. gl
Dalek Dave 2-Dec-10 3:39am
Good Call.
DasKaiserJohan 2-Dec-10 6:17am
Gonna have to try that, but the problem is right now when a new client arrives, he is given his own UDP socket. When he sends data to that socket, it is sent through every other UDP socket except that one. Since you can only have one socket listening to a IP/port, how would this go about if the server listens on 53 for all its sockets?

Does all kind of data go through common sockets like that? Again, the example of video games. Are they all using that very port or how do they manage to send/receive?
ROGDEV 2-Dec-10 22:56pm
You can send any type of data, the port is just the "channel". Most firewalls that I'm aware of don't actually inspect the packet data, rather they examine the source/dest IP and/or the source/dest port numbers. Because DNS is such a fundamental part of how the internet works, nearly all firewalls will allow traffic through on that port figuring it's probably legit DNS queries. Video games, or any other app, will exhibit this problem if a device along the way is blocking traffic. The source/destination port can also indicate the type of application on both ends -- server apps will bind to ports 1-1024 whereas "user" apps will typically use higher numbers like 50000-60000. Some firewalls/admins exploit this by only allow traffic through if the dest port num is under 1024.

Also -- are you able to ping between your source and destination machines when trying to run in "global" mode? I ask this because if you're behind a router doing DHCP, at one or both ends of your pipe, you will not be able to directly "see" the remote machine without setting up port forwarding or DMZ access on the offending router. You mentioned video games -- your XBOX/PC usually won't work or accept incoming connections if you're behind a cable modem AND a router+switch unit -- until you configure "DMZ" on the router+switch unit.. My apologies if I'm preaching to the choir on this just wanted to throw it out there!

HTH if not post up again buddy.. Good luck
DasKaiserJohan 3-Dec-10 7:33am

I am using a modem and a router in my home network and I can play games without port forwarding. (Although I have uPnP; does that let games go through the router firewall?)

About pinging src and dst machines; do you simply mean pinging the client IP from my computer? If so yes, I have no problems with it.

If I send a packet to an outside client with IP/port, if he binds/listens on his local IP/port the data will be forwarded to him right? Because the Socket.bind specifically says binding a socket to the local IP/port.
Yes.., may be the problem with your router firewall

Some times may be the Antivirus blocks also.., may be the antivius may blocks the UPD port which is you are using..
Turning of your windows Firewall as others said.Hope this will solve your Problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900