You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

51 lines
1.2 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace JT808.DotNetty.Core
  8. {
  9. internal class JT808SimpleTcpClient
  10. {
  11. private TcpClient tcpClient;
  12. public JT808SimpleTcpClient(IPEndPoint remoteAddress)
  13. {
  14. tcpClient = new TcpClient();
  15. tcpClient.Connect(remoteAddress);
  16. Task.Run(()=> {
  17. while (true)
  18. {
  19. try
  20. {
  21. byte[] buffer = new byte[100];
  22. tcpClient.GetStream().Read(buffer, 0, 100);
  23. Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " " + string.Join(" ", buffer));
  24. }
  25. catch
  26. {
  27. }
  28. Thread.Sleep(1000);
  29. }
  30. });
  31. }
  32. public void WriteAsync(byte[] data)
  33. {
  34. tcpClient.GetStream().WriteAsync(data, 0, data.Length);
  35. }
  36. public void Down()
  37. {
  38. tcpClient.Close();
  39. }
  40. }
  41. }