Sending UDP Packet in C#

柔情痞子 提交于 2019-12-17 15:37:44

问题


I have a game server (WoW). I want my players to download my custom patches to the game. I've done a program that checks for update/downloading things. I want my program to send a packet to my game server if player have all my patches. I dont need any response from the server, it will handle it, but its another story.

So I want to know, how to send a packet to a server.

Thank you!


回答1:


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

IPAddress serverAddr = IPAddress.Parse("192.168.2.255");

IPEndPoint endPoint = new IPEndPoint(serverAddr, 11000);

string text = "Hello";
byte[] send_buffer = Encoding.ASCII.GetBytes(text );

sock.SendTo(send_buffer , endPoint);



回答2:


static void SendUdp(int srcPort, string dstIp, int dstPort, byte[] data)
{
    using (UdpClient c = new UdpClient(srcPort))
        c.Send(data, data.Length, dstIp, dstPort);
}

Usage:

SendUdp(11000, "192.168.2.255", 11000, Encoding.ASCII.GetBytes("Hello!"));


来源:https://stackoverflow.com/questions/2637697/sending-udp-packet-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!