dimanche 25 janvier 2015

c# chat app only working on local network


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 portforward doesn't make sense because all other programs I download don't ask me to portforward :P . For example: How does the battlefield servers communicate with the clients if their clients don't portforward? How would I get this to work over a long distance in C#? Please show me some example code for doing this, I have no idea how. Here is the code I used:


Receiving:



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);
}
}


Sending:



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

client.SendTo(ex.packetdata, ep);


I am aware of the disadvantages of UDP, but I want to use it.





Aucun commentaire:

Enregistrer un commentaire