-1

I made a chat app in C# using the UDP protocol. I don't want to use TCP. But it is only working within my own network/wifi. I made this for myself and my friend, or possibly anyone else who downloads it, and it isn't working. Do I have to portforward or something? To port forward doesn't make sense because all other programs I download don't ask me to portforward :P . How would I get this to work over a long distance? Here is the code I used:

Receiving the data:

    static UdpClient UdpReciever = new UdpClient(PORT);
    static byte[] data = new byte[512];

    static void Main(string[] args)
    {
        while (true)
        {
            IPEndPoint EP = new IPEndPoint(IPAddress.Any, PORT);
            data = UdpReciever.Receive(ref EP);
            Otherclass oc = new Parser();
            Otherclass.Parse(data);

        }
    }

    public class Otherclass
    {
        public static void Parse(Byte[] data)
        {
            string received = Encoding.ASCII.GetString(data);
            Console.WriteLine(received);
        }
    }

For sending the data I'm using this code:

            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(this.Ip), port);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            client.SendTo(ex.packetdata, ep);
Nanerz
  • 1
  • 1

2 Answers2

1

You can't send data onto any computer in the internet. Your clients need to have a public ip and forwarded port you are using for it to their computers. If they don't have a public ip then you need a server to process messages.

LukAss741
  • 771
  • 1
  • 7
  • 24
  • They have a public IP. But that doesn't make sense, how do games like battlefield on the pc work if I don't portforward? Battlefield still receives data. – Nanerz Jan 25 '15 at 15:42
  • All online games and chat probrams use servers. – LukAss741 Jan 25 '15 at 15:45
  • Can you explain how that would work, LukASS741? I'm new to networking in general. – Nanerz Jan 25 '15 at 15:46
  • In example if you move in a game then information about your movement is sent to server. Other players download that information from server, their game processes it and makes you move on their screens. Definitely better solution than sending such information to each of like 10 players who are online. If you are going to use this chat only with 1 other person and both of you have public ips then peer to peer solution may be suitable. If that doesn't work then open and forward port it uses on your router(s) to your computers. – LukAss741 Jan 25 '15 at 16:00
  • Ok, so how would I get It to work without portforwarding. How do I code it so other clients can download the information from the server? I'm not sure what code I would use to make that happen. I'm new to this, thanks in advance, LukAss741. :) – Nanerz Jan 25 '15 at 16:05
0

There are two things to be aware of:

  1. The EP endpoint you create has an IP address of your local network that is not accessible from the outside world. Print it to verify.

  2. UDP packets are not necessarily received completely in the same way they are sent. On local network you may not spot the difference but some testing on a larger network will. Take a close look at the guarantees the UDP protocol offers. If you try to correct for errors you will soon be reinventing TCP, so you might as well use TCP in the first place.

Emile
  • 2,200
  • 27
  • 36
  • Thanks, I'm aware of the disadvantages of UDP. But how would I get the endpoint outside of my local network? – Nanerz Jan 25 '15 at 15:46
  • You have to have a fixed IP address to the outside world for others to find your server. On your router forward traffic on the specific port to your machine. – Emile Jan 25 '15 at 19:13