Asp.NetSocketマルチスレッド単純傍受ポート、データ取得

4915 ワード

前の文章に対して,コードの二次開発を経て線面のこの方法を得た.
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 public   void   CreatSocket()      {          try          {              int   port = 2000;              string   host = "127.0.0.1" ;
               /**/              /// (EndPoint)              IPAddress ip = IPAddress.Parse(host); // ip IPAddress              IPEndPoint ipe = new   IPEndPoint(ip, port); // ip IPEndPoint
               /**/              /// socket              Socket s = new   Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // socket , udp , SocketType.Dgram              s.Bind(ipe); // EndPoint (2000 ip )              s.Listen(0); //              Console.WriteLine( " " );
               /**/              /// client , socket,
               while   ( true ) // , N              {                  Socket temp = s.Accept(); // socket                  //Console.WriteLine(" ");                  string   recvStr = "" ;                  byte [] recvBytes = new   byte [1024];                  int   bytes;                  bytes = temp.Receive(recvBytes, recvBytes.Length, 0); //                  recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);                  DBHelper.ExecutCommand( "INSERT INTO [ShuJu]([strs_VAR]) VALUES('"   + recvStr + "')" ); //                  if   (temp != null )                      temp.Close();
               }
               /**/              /// client              //Console.WriteLine("server get message:{0}", recvStr);//              //string sendStr = "ok!Client send message successful!";              //byte[] bs = Encoding.ASCII.GetBytes(sendStr);              //temp.Send(bs, bs.Length, 0);//              //temp.Close();              s.Close();              //Console.ReadLine();          }          catch   (Exception ex)          {              string   s = ex.ToString();          }      }
次に、リスナー(Global.asax)を定義するWebサイトを定義します.
1
2
3
4
5 void   Application_Start( object   sender, EventArgs e)      {          //          ( new   System.Threading.Thread( new   System.Threading.ThreadStart( new   Class1().CreatSocket))).Start(); //       }
これでいい!N回の接続が受けられるようになりました!