DotnetSocketClient.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Threading;
  9. namespace StressClient
  10. {
  11. class DotnetSocketClient
  12. {
  13. private string _clientId = "";
  14. private string _svr_ip = "127.0.0.1";
  15. private int _svr_port = 8081;
  16. System.Net.Sockets.Socket _socket;
  17. string _last_req_time = "";
  18. public DotnetSocketClient(string clientId)
  19. {
  20. _clientId = clientId;
  21. }
  22. // 实例化socket
  23. private void InitConnection()
  24. {
  25. try
  26. {
  27. _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  28. IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(_svr_ip), _svr_port);
  29. _socket.Connect(ipEndPoint);
  30. }
  31. catch (Exception)
  32. {
  33. }
  34. }
  35. public void Run()
  36. {
  37. try
  38. {
  39. string message = string.Format("{0} 客户端开始运行", _clientId);
  40. System.Console.WriteLine(message);
  41. while (true)
  42. {
  43. // 发送数据
  44. //_socket.Send(Encoding.Default.GetBytes("dddd"));
  45. System.Threading.Thread.Sleep(2000);
  46. }
  47. //_socket.Shutdown(SocketShutdown.Both);
  48. // _socket.Close();
  49. }
  50. catch (Exception)
  51. { }
  52. }
  53. }
  54. }