1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Collections;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Runtime.InteropServices;
  10.  
  11. public class ClientHandler
  12. {
  13. private static int nbFiles;
  14. private static int nbMatchFiles;
  15.  
  16. private const String dirPath = @"C:\Users\Romario\Desktop";
  17. private String pattern;
  18.  
  19. // StreamReader readerStream1;
  20. // NetworkStream writerStream;
  21.  
  22.  
  23.  
  24.  
  25. //-----------------------------------------------------------------------------------------------------
  26.  
  27. public TcpClient clientSocket;
  28. // Socket sock;
  29. public void SearchForStandard(string path, string text, bool recurse)
  30. {
  31.  
  32. foreach (String file in Directory.GetFiles(path, "*.txt"))
  33. {
  34. Interlocked.Increment(ref nbFiles);
  35. FindInFile(file, text);
  36.  
  37. }
  38. if (recurse)
  39. {
  40. foreach (String dir in Directory.GetDirectories(path))
  41. {
  42. SearchForStandard(dir, text, recurse);
  43. }
  44. }
  45. }
  46.  
  47. public void FindInFile(string csFilePath, string text)
  48. {
  49. try
  50. {
  51. NetworkStream writerStream = clientSocket.GetStream();
  52. if (File.ReadAllText(csFilePath).IndexOf(text) >= 0)
  53. {
  54. Interlocked.Increment(ref nbMatchFiles);
  55. string filename = Path.GetFileName(csFilePath);
  56. Console.WriteLine("________________________________________________________________________");
  57. Console.WriteLine(nbMatchFiles + "." + text + "\t\tName of File: " + filename);
  58. /* StreamReader sr = File.OpenText(csFilePath);
  59.   string s = "";
  60.   while ((s = sr.ReadLine()) != null)
  61.   {
  62.   Console.WriteLine(s);
  63.  
  64.   byte[] dataWrite = Encoding.ASCII.GetBytes(s);
  65.  
  66.   writerStream.Write(dataWrite, 0, dataWrite.Length);
  67.   }
  68.   */
  69. FileStream stream = new FileStream(csFilePath, FileMode.Open);
  70. StreamReader readerStream = new StreamReader(stream);
  71.  
  72. string s = readerStream.ReadToEnd();
  73.  
  74. byte[] dataWrite = Encoding.ASCII.GetBytes("________________________________________________________________________\n" + filename + "\n________________________________________________________________________\n" + s);
  75. writerStream.Write(dataWrite, 0, dataWrite.Length);
  76.  
  77. // stream.Close();
  78. //readerStream.Close();
  79.  
  80. Console.WriteLine("________________________________________________________________________\n" + s);
  81. // Console.WriteLine("\n________________________________________________________________________\n");
  82.  
  83. }
  84. // else
  85. // {
  86. // byte[] dataWrite = Encoding.ASCII.GetBytes("\r\n");
  87. // writerStream.Write(dataWrite, 0, dataWrite.Length);
  88.  
  89. // }
  90. }
  91. catch (Exception exp)
  92. {
  93. Console.WriteLine("Exception: " + exp.Message);
  94. }
  95.  
  96.  
  97. }
  98.  
  99. public void RunClient()
  100. {
  101. StreamReader readerStream = new StreamReader(clientSocket.GetStream());
  102. NetworkStream writerStream = clientSocket.GetStream();
  103.  
  104. string returnData = readerStream.ReadLine();
  105. string userName = returnData;
  106.  
  107. Console.WriteLine("Welcome " + userName + " to the Server");
  108.  
  109. while (true)
  110. {
  111. try
  112. {
  113. returnData = readerStream.ReadLine();
  114.  
  115. if (returnData.IndexOf("QUIT") > -1)
  116. {
  117. Console.WriteLine("Bye bye " + userName);
  118. break;
  119. }
  120. // Console.WriteLine(userName + " : " + returnData);
  121. // returnData += "\r\n";
  122. //-----------------------------------------------------------------------------------------------------
  123. pattern = returnData;
  124.  
  125. Console.WriteLine("Searching for : " + pattern);
  126.  
  127. nbFiles = 0;
  128. nbMatchFiles = 0;
  129. DateTime start = DateTime.Now;
  130.  
  131. Console.WriteLine("Search using Standard Processing");
  132. SearchForStandard(dirPath, pattern, true);
  133.  
  134. Console.WriteLine(string.Format("Total files : {0}", nbFiles));
  135. Console.WriteLine(string.Format("found in files : {0}", nbMatchFiles));
  136. Console.WriteLine(string.Format("Search Duration : {0}", DateTime.Now.Subtract(start).ToString()));
  137.  
  138.  
  139.  
  140. //-----------------------------------------------------------------------------------------------------
  141. // pattern = returnData;
  142. // returnData = "\r\n";
  143. // byte[] dataWrite = Encoding.ASCII.GetBytes(returnData);
  144. // writerStream.Write(dataWrite, 0, dataWrite.Length);
  145.  
  146. }
  147. catch (Exception exp)
  148. {
  149. Console.WriteLine("Exception: " + exp.Message);
  150. Console.ReadKey();
  151. }
  152. clientSocket.Close();
  153. }
  154.  
  155. }
  156. }
  157.  
  158.  
  159. public class MultiThreading_Server
  160. {
  161. const int ECHO_PORT = 4000;
  162. //IPAddress ip = IPAddress.Parse("192.168.0.1");
  163. public static int nClients = 0;
  164.  
  165. //---------------------------УСТАНОВКА ЗАГОЛОВКА ОКНА--------------------------
  166. [DllImport("kernel32.dll")]
  167. public static extern bool SetConsoleTitle(String lpConsoleTitle);
  168.  
  169. [STAThread]
  170. //-----------------------------------------------------------------------------
  171.  
  172. public static void Main(string[] arg)
  173. {
  174. SetConsoleTitle("END SERVER"); // УСТАНОВКА ЗАГОЛОВКА ОКНА
  175.  
  176. try
  177. {
  178.  
  179. //Связываем сервер с локальным портом
  180. // IPAddress localadr = IPAddress.Parse("192.168.0.1");
  181. // TcpListener clientListener = new TcpListener(localadr, ECHO_PORT);
  182. TcpListener clientListener = new TcpListener(ECHO_PORT);
  183. //Начинаем слушать
  184. clientListener.Start();
  185.  
  186. Console.WriteLine("Waiting for connections...");
  187.  
  188. while (true)
  189. {
  190.  
  191. //Даем согласие на соединение
  192. TcpClient client = clientListener.AcceptTcpClient();
  193.  
  194. ClientHandler cHandler = new ClientHandler();
  195.  
  196. //Передаем значение объекту ClientHandler
  197. cHandler.clientSocket = client;
  198.  
  199. //Создаем новый поток для клиент
  200. Thread clientThread = new Thread(new ThreadStart(cHandler.RunClient));
  201.  
  202. clientThread.Start();
  203. }
  204. clientListener.Stop();
  205. }
  206. catch (Exception exp)
  207. {
  208. Console.WriteLine("Exception: " + exp.Message);
  209. Console.ReadKey();
  210. }
  211. }
  212. }
  213.  
  214.  
  215.  
  216. using System;
  217. using System.IO;
  218. using System.Net;
  219. using System.Net.Sockets;
  220. using System.Collections.Generic;
  221. using System.Collections;
  222. using System.Linq;
  223. using System.Text;
  224.  
  225. public class EchoClient
  226. {
  227. const int ECHO_PORT = 4000;
  228.  
  229. public static void Main(string[] arg)
  230. {
  231. // Console.Write("Your UserName: ");
  232. // string userName = Console.ReadLine();
  233. string userName = "Client";
  234. Console.WriteLine("---Logged In---");
  235.  
  236. try
  237. {
  238. //Создаем соединение с ChatServer
  239. TcpClient eClient = new TcpClient("127.0.0.1", ECHO_PORT);
  240.  
  241. //Создаем классы потоков
  242. StreamReader readerStream = new StreamReader(eClient.GetStream());
  243. NetworkStream writerStream = eClient.GetStream();
  244.  
  245. string dataToSend;
  246.  
  247. dataToSend = userName;
  248. dataToSend += "\r\n";
  249.  
  250. //Отправляем имя пользователя на сервер
  251. byte[] data = Encoding.ASCII.GetBytes(dataToSend);
  252.  
  253. writerStream.Write(data,0,data.Length);
  254.  
  255. while (true)
  256. {
  257. Console.Write(userName + ":");
  258.  
  259. //Считываем строку с сервера
  260. dataToSend = Console.ReadLine();
  261. dataToSend += "\r\n";
  262.  
  263. data = Encoding.ASCII.GetBytes(dataToSend);
  264. writerStream.Write(data,0,data.Length);
  265.  
  266. //Если отправлена команда QUIT, то выходим из приложения
  267. if (dataToSend.IndexOf("QUIT") > -1)
  268. break;
  269.  
  270. string returnData;
  271.  
  272. //Получить ответ от сервера
  273. Console.WriteLine("Server: ");
  274.  
  275. while ((returnData = readerStream.ReadLine()) != null)
  276. {
  277. // if (string.IsNullOrEmpty(returnData)) break;
  278. Console.WriteLine(returnData);
  279. }
  280.  
  281. /* returnData = readerStream.ReadLine();
  282.   while (!readerStream.EndOfStream)
  283.   {
  284.   // if (returnData == "") break;
  285.   // if (string.IsNullOrEmpty(returnData)) break;
  286.   Console.WriteLine(returnData);
  287.   // returnData = readerStream.ReadLine();
  288.   }
  289.  */
  290. }
  291. //Закрыть TcpClient
  292. eClient.Close();
  293. }
  294. catch(Exception exp)
  295. {
  296. Console.WriteLine("Exception: " + exp.Message);
  297. Console.ReadKey();
  298. }
  299. }
  300.  
  301. }
  302.  
  303.  
  304.  
  305.  
  306.