查看: 2010|回复: 0

[.NET开发] C#多线程数组模拟socket

发表于 2018-2-9 08:00:00

本文实例为大家分享了C#多线程数组模拟socket的具体代码,供大家参考,具体内容如下

代码如下

  1. //实例化线程组
  2. Thread[] clientThreads = new Thread[numThread];
  3. for (int i = 0; i < numThread; i++)
  4. {
  5. clientThreads[i] = new Thread(new ParameterizedThreadStart(SocketClient));
  6. clientThreads[i].Start(i);
  7. }
复制代码

多线程组模拟socket发送

  1. static void SocketClient(object s)
  2. {
  3. //本机地址字符串
  4. string strAddress = (13010001000 + Convert.ToInt32(s)).ToString() + "0";
  5. IPHostEntry ipHost = Dns.Resolve(ipServer);
  6. IPAddress ipAddress = ipHost.AddressList[0];
  7. IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, portServer);
  8. //实例化套接字
  9. Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  10. try
  11. {
  12. socketClient.Connect(ipEndPoint);
  13. WriteLine(string.Format("Successfully connected to {0}! Socket Client 小贝", socketClient.RemoteEndPoint, strAddress));
  14. byte[] byteAddr = new byte[6];
  15. byteAddr[0] = Convert.ToByte(strAddress.Substring(0, 2), 0x10);
  16. byteAddr[1] = Convert.ToByte(strAddress.Substring(2, 2), 0x10);
  17. byteAddr[2] = Convert.ToByte(strAddress.Substring(4, 2), 0x10);
  18. byteAddr[3] = Convert.ToByte(strAddress.Substring(6, 2), 0x10);
  19. byteAddr[4] = Convert.ToByte(strAddress.Substring(8, 2), 0x10);
  20. byteAddr[5] = Convert.ToByte(strAddress.Substring(10, 2), 0x10);
  21. Packet pkt = new Packet();
  22. byte[] heartpkt = pkt.SendHeartPkt(byteAddr, addrTarget);
  23. int times = timerdata / timerheart;
  24. Random rd = new Random();
  25. while (true)
  26. {
  27. for (int i = 0; i < times - 1; i++)
  28. {
  29. socketClient.Send(heartpkt);
  30. WriteLine(string.Format("Send: {0}! \r\n Socket Client 小贝", pkt.BytesToStr(heartpkt), strAddress));
  31. System.Threading.Thread.Sleep(1000 * timerheart);
  32. }
  33. UInt16 i1 = (UInt16)rd.Next(100);
  34. UInt16 i2 = (UInt16)rd.Next(100);
  35. UInt16 i3 = (UInt16)rd.Next(100);
  36. UInt16 i4 = (UInt16)rd.Next(100);
  37. UInt16 i5 = (UInt16)rd.Next(100);
  38. byte[] byte1 = BitConverter.GetBytes(i1);
  39. byte[] byte2 = BitConverter.GetBytes(i2);
  40. byte[] byte3 = BitConverter.GetBytes(i3);
  41. byte[] byte4 = BitConverter.GetBytes(i4);
  42. byte[] byte5 = BitConverter.GetBytes(i5);
  43. byte[] databyte = new byte[10];
  44. databyte[0] = byte1[1];
  45. databyte[1] = byte1[0];
  46. databyte[2] = byte2[1];
  47. databyte[3] = byte2[0];
  48. databyte[4] = byte3[1];
  49. databyte[5] = byte3[0];
  50. databyte[6] = byte4[1];
  51. databyte[7] = byte4[0];
  52. databyte[8] = byte5[1];
  53. databyte[9] = byte5[0];
  54. byte[] datapkt = pkt.SendDataPkt(byteAddr, addrTarget, databyte);
  55. socketClient.Send(datapkt);
  56. WriteLine(string.Format("Send: {0}! \r\n Socket Client 小贝", pkt.BytesToStr(datapkt), strAddress));
  57. System.Threading.Thread.Sleep(1000 * timerheart);
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. WriteLine(string.Format("Exception:{0} \r\n Socket Client 小贝", ex.ToString(), strAddress));
  63. }
  64. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

  • C#多线程ThreadPool线程池详解
  • C#多线程之线程控制详解
  • C#多线程之Thread类详解
  • C#多线程及同步示例简析
  • C#多线程爬虫抓取免费代理IP的示例代码
  • C# 文件上传下载(Excel导入,多线程下载)功能的实现代码
  • C#多线程之Semaphore用法详解
  • 大家应该掌握的多线程编程


回复

使用道具 举报