【发布时间】:2015-01-25 15:26:03
【问题描述】:
我使用 UDP 协议在 C# 中制作了一个聊天应用程序。我不想使用 TCP。但它只适用于我自己的网络/wifi。我为自己和我的朋友,或者可能其他任何下载它的人制作了这个,但它不起作用。我必须转发还是什么?移植没有意义,因为我下载的所有其他程序都不会要求我移植 :P 。我怎样才能让它长距离工作?这是我使用的代码:
接收数据:
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);
}
}
为了发送我正在使用此代码的数据:
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(this.Ip), port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.SendTo(ex.packetdata, ep);
【问题讨论】: