UdpClientTest.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ConsoleApp1.UDP;
  9. class UdpClientTest
  10. {
  11. public static void Test()
  12. {
  13. Socket socket = new Socket(socketType: SocketType.Dgram, protocolType: ProtocolType.Udp);
  14. EndPoint endPoint = new DnsEndPoint("ChargingVerify01.taiwanebus.net", 14011);
  15. socket.Connect(endPoint);
  16. var sendResult = socket.Send(
  17. buffer: new byte[] { 1, 2, 3 },
  18. size: 3,
  19. socketFlags: SocketFlags.None
  20. );
  21. var buffer = new byte[100];
  22. var receivedData = socket.Receive(buffer);
  23. Console.WriteLine(receivedData);
  24. UdpClient udpClient = new UdpClient();
  25. udpClient.Connect("ChargingVerify01.taiwanebus.net", 14011);
  26. udpClient.ReceiveAsync().ContinueWith(UdpClientResultHandler);
  27. var tt = udpClient.Send(
  28. dgram: new byte[] { 1, 2, 3 },
  29. bytes: 3
  30. );
  31. Console.WriteLine(tt);
  32. Task.Delay(30_000).Wait();
  33. }
  34. private static void UdpClientResultHandler(Task<UdpReceiveResult> task)
  35. {
  36. }
  37. }