服务器之家

服务器之家 > 正文

C#多线程数组模拟socket

时间:2022-01-12 13:19     来源/作者:北冥冰皇

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

代码如下

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

多线程组模拟socket发送

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

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

标签:

相关文章

热门资讯

蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部