欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#多线程数组模拟socket

程序员文章站 2023-11-29 11:44:04
本文实例为大家分享了c#多线程数组模拟socket的具体代码,供大家参考,具体内容如下 代码如下 //实例化线程组 thread[] clientthre...

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

代码如下

//实例化线程组
  thread[] clientthreads = new thread[numthread];

  for (int i = 0; i < numthread; i++)
  {
    clientthreads[i] = new thread(new parameterizedthreadstart(socketclient));
    clientthreads[i].start(i);
  }

多线程组模拟socket发送

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));
   }
  }

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