The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
Server
Posted by: Romario | April 30, 2010 @ 4:39am
C# Code
[
Download
]
using System; using System.IO; using System.Threading; using System.Net; using System.Net.Sockets; using System.Collections; using System.Linq; using System.Text; using System.Runtime.InteropServices; public class ClientHandler { private static int nbFiles; private static int nbMatchFiles; private const String dirPath = @"C:\Users\Romario\Desktop"; private String pattern; // StreamReader readerStream1; // NetworkStream writerStream; //----------------------------------------------------------------------------------------------------- public TcpClient clientSocket; // Socket sock; public void SearchForStandard(string path, string text, bool recurse) { foreach (String file in Directory.GetFiles(path, "*.txt")) { Interlocked.Increment(ref nbFiles); FindInFile(file, text); } if (recurse) { foreach (String dir in Directory.GetDirectories(path)) { SearchForStandard(dir, text, recurse); } } } public void FindInFile(string csFilePath, string text) { try { NetworkStream writerStream = clientSocket.GetStream(); if (File.ReadAllText(csFilePath).IndexOf(text) >= 0) { Interlocked.Increment(ref nbMatchFiles); string filename = Path.GetFileName(csFilePath); Console.WriteLine("________________________________________________________________________"); Console.WriteLine(nbMatchFiles + "." + text + "\t\tName of File: " + filename); /* StreamReader sr = File.OpenText(csFilePath); string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); byte[] dataWrite = Encoding.ASCII.GetBytes(s); writerStream.Write(dataWrite, 0, dataWrite.Length); } */ FileStream stream = new FileStream(csFilePath, FileMode.Open); StreamReader readerStream = new StreamReader(stream); string s = readerStream.ReadToEnd(); byte[] dataWrite = Encoding.ASCII.GetBytes("________________________________________________________________________\n" + filename + "\n________________________________________________________________________\n" + s); writerStream.Write(dataWrite, 0, dataWrite.Length); // stream.Close(); //readerStream.Close(); Console.WriteLine("________________________________________________________________________\n" + s); // Console.WriteLine("\n________________________________________________________________________\n"); } // else // { // byte[] dataWrite = Encoding.ASCII.GetBytes("\r\n"); // writerStream.Write(dataWrite, 0, dataWrite.Length); // } } catch (Exception exp) { Console.WriteLine("Exception: " + exp.Message); } } public void RunClient() { StreamReader readerStream = new StreamReader(clientSocket.GetStream()); NetworkStream writerStream = clientSocket.GetStream(); string returnData = readerStream.ReadLine(); string userName = returnData; Console.WriteLine("Welcome " + userName + " to the Server"); while (true) { try { returnData = readerStream.ReadLine(); if (returnData.IndexOf("QUIT") > -1) { Console.WriteLine("Bye bye " + userName); break; } // Console.WriteLine(userName + " : " + returnData); // returnData += "\r\n"; //----------------------------------------------------------------------------------------------------- pattern = returnData; Console.WriteLine("Searching for : " + pattern); nbFiles = 0; nbMatchFiles = 0; DateTime start = DateTime.Now; Console.WriteLine("Search using Standard Processing"); SearchForStandard(dirPath, pattern, true); Console.WriteLine(string.Format("Total files : {0}", nbFiles)); Console.WriteLine(string.Format("found in files : {0}", nbMatchFiles)); Console.WriteLine(string.Format("Search Duration : {0}", DateTime.Now.Subtract(start).ToString())); //----------------------------------------------------------------------------------------------------- // pattern = returnData; // returnData = "\r\n"; // byte[] dataWrite = Encoding.ASCII.GetBytes(returnData); // writerStream.Write(dataWrite, 0, dataWrite.Length); } catch (Exception exp) { Console.WriteLine("Exception: " + exp.Message); Console.ReadKey(); } clientSocket.Close(); } } } public class MultiThreading_Server { const int ECHO_PORT = 4000; //IPAddress ip = IPAddress.Parse("192.168.0.1"); public static int nClients = 0; //---------------------------������� ������� ���-------------------------- [DllImport("kernel32.dll")] public static extern bool SetConsoleTitle(String lpConsoleTitle); [STAThread] //----------------------------------------------------------------------------- public static void Main(string[] arg) { SetConsoleTitle("END SERVER"); // ������� ������� ��� try { //������� ������ � ������� ������ // IPAddress localadr = IPAddress.Parse("192.168.0.1"); // TcpListener clientListener = new TcpListener(localadr, ECHO_PORT); TcpListener clientListener = new TcpListener(ECHO_PORT); //������� ����� clientListener.Start(); Console.WriteLine("Waiting for connections..."); while (true) { //���� �������� �� ���������� TcpClient client = clientListener.AcceptTcpClient(); ClientHandler cHandler = new ClientHandler(); //�������� ������� ������ ClientHandler cHandler.clientSocket = client; //������� ���� ����� �� ������ Thread clientThread = new Thread(new ThreadStart(cHandler.RunClient)); clientThread.Start(); } clientListener.Stop(); } catch (Exception exp) { Console.WriteLine("Exception: " + exp.Message); Console.ReadKey(); } } } using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; public class EchoClient { const int ECHO_PORT = 4000; public static void Main(string[] arg) { // Console.Write("Your UserName: "); // string userName = Console.ReadLine(); string userName = "Client"; Console.WriteLine("---Logged In---"); try { //������� ���������� � ChatServer TcpClient eClient = new TcpClient("127.0.0.1", ECHO_PORT); //������� ����� ������� StreamReader readerStream = new StreamReader(eClient.GetStream()); NetworkStream writerStream = eClient.GetStream(); string dataToSend; dataToSend = userName; dataToSend += "\r\n"; //��������� �� ���������� �� ������ byte[] data = Encoding.ASCII.GetBytes(dataToSend); writerStream.Write(data,0,data.Length); while (true) { Console.Write(userName + ":"); //������� ������ � ������� dataToSend = Console.ReadLine(); dataToSend += "\r\n"; data = Encoding.ASCII.GetBytes(dataToSend); writerStream.Write(data,0,data.Length); //���� ���������� ������� QUIT, �� ����� �� ��������� if (dataToSend.IndexOf("QUIT") > -1) break; string returnData; //������ ����� �� ������� Console.WriteLine("Server: "); while ((returnData = readerStream.ReadLine()) != null) { // if (string.IsNullOrEmpty(returnData)) break; Console.WriteLine(returnData); } /* returnData = readerStream.ReadLine(); while (!readerStream.EndOfStream) { // if (returnData == "") break; // if (string.IsNullOrEmpty(returnData)) break; Console.WriteLine(returnData); // returnData = readerStream.ReadLine(); } */ } //����� TcpClient eClient.Close(); } catch(Exception exp) { Console.WriteLine("Exception: " + exp.Message); Console.ReadKey(); } } }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.50 / 8
72 Votes
http://codebin.yi.org/623
page generated in 0.01 seconds