WsClient.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Srez
  9. {
  10. public class WsClient
  11. {
  12. private static byte[] bytes = new byte[1024];
  13. private static IPHostEntry iPHostEntry = Dns.GetHostEntry("localhost");
  14. private static IPAddress iPAddress = iPHostEntry.AddressList[0];
  15. private static IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, 11000);
  16. private static Socket socket = null;
  17. public static void WsClientConnect()
  18. {
  19. socket = new Socket(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  20. socket.Connect(iPEndPoint);
  21. }
  22. public static void WsClientSendMessage(string data)
  23. {
  24. socket.Send(Encoding.UTF8.GetBytes(data));
  25. }
  26. public static string WsClientResponseFromWss()
  27. {
  28. return Encoding.UTF8.GetString(bytes, 0, socket.Receive(bytes));
  29. }
  30. public static void WsClientClose()
  31. {
  32. socket.Shutdown(SocketShutdown.Both);
  33. socket.Close();
  34. }
  35. }
  36. }