C # Implementation of simple access to sweep code gun information Code _c# tutorial

Source: Internet
Author: User

A sweep code gun follows the TCP protocol and can obtain the information scanned by the scanning code gun by changing the code; (There is a serial server);

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Net;
Using System.Net.Sockets;
Using System.Threading;
Using System.Diagnostics;
Using System.Net;
    namespace Demo_net {//Ben for service end/afternoon plus one to judge whether the network is connected; Class Program {static Socket msock;
        static void Main (string[] args) {//First determine if ping pass: string ips = "10.18.14.111";
        String str = Netconnect (IPS);
        Console.WriteLine (str);
    Console.ReadLine ();
      //Ping to determine whether to connect; private static string Netconnect (string IP) {Process p = new process ();
      p.StartInfo.FileName = "cmd.exe";
      P.startinfo.useshellexecute = false;
      P.startinfo.redirectstandarderror = true;
      P.startinfo.redirectstandardinput = true;
      P.startinfo.redirectstandardoutput = true;
      P.startinfo.createnowindow = false;
      String Pingstr;
      P.start ();
      P.standardinput.writeline ("Ping-n 1" + IP); P.standArdinput.writeline ("Exit");
      String strrst = P.standardoutput.readtoend ();
 
        if (Strrst.indexof ("(0% Lost)")!=-1) {pingstr = "successful Connection"; Defines the native IP and corresponding port required for the socket connection; msock = new Socket (addressfamily.internetwork, SocketType.Stream, Protocoltyp
        E.TCP);
        var localip = new IPEndPoint (Ipaddress.parse ("10.18.14.23"), 10001); Msock.
        Bind (Localip); Define the maximum number of network connections yourself msock.
        Listen (10);
        New thread processing, thread th = new Thread (delegate () {Rec ();
        }); Th.
        IsBackground = true; Th.
      Start ();
      else {pingstr = "connection Timeout";
      } p.close ();
    return pingstr; //Listen for links, new thread handling static void Rec () {do {Socket s = msock.
        Accept ();
        Thread th = new Thread (delegate () {Parse (s);
        }); Th.
        IsBackground = true; Th.
        
      Start ();
    } while (true); ///When link is processed get information static void Parse (Socket s) {do {byte[] b = new byte[1000];
        int L = s.receive (b); b = B.take (l).
        ToArray (); String rs = string.
        Empty; for (int i = 0; i < b.length i++) {rs = rs + b[i].
        ToString ();
      ///Decode Console.WriteLine (Encoding.ASCII.GetString (b, 0, L));
      
    } while (true);
 }
  }
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.