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