Показать сообщение отдельно

  #19  
Старый 30.04.2010, 12:19
bugrom256
Новичок
Регистрация: 14.04.2010
Сообщений: 14
Провел на форуме:
27397

Репутация: 0
По умолчанию

кто-нить может мне объяснить почему клиент не до конца данные принимает ????

вот СЕРВЕР:
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)
{

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

}

Заранее очень признателен ))))
 
Ответить с цитированием