我编写了这段代码来检查文件中的特定字符串。现在它检查字符串。但是,如何向客户发送回复说“它存在”?服务器端程序应该包含所有代码。它还接受多个客户端。
该计划的程序如下
基本上,如果客户想检查文件中是否存在特定的字符串(单词),他会通过telnet上的端口连接此代码。他键入了他想要搜索的字符串(在telnet上)并将其发送到服务器端。此服务器端程序从文件中检查它。如果它存在,它会向客户端发送一条消息,说“字符串存在于文件中”如果不存在,它应该发送一条消息“它不是”。
搜索字符串(“hello”)在此程序中。如何让客户端从客户端(telnet)搜索它? 这是我到过的地方,有很多帮助和教程。有人可以帮助我吗?
已编辑 - 我已更改代码,以便将回复发送回客户端。我现在需要知道的是,我如何通过客户端(telnet)启用客户端搜索(键入他想要搜索的单词)?任何帮助将非常感激。 我也更新了我的代码。
谢谢。
class Program
{
static void Main(string[] args)
{
IPAddress ipad = IPAddress.Parse("127.0.0.1");
TcpListener serversocket = new TcpListener(ipad, 8888);
TcpClient clientsocket = default(TcpClient);
Byte[] bytes = new Byte[256];
serversocket.Start();
Console.WriteLine(">> Server Started");
while(true)
{
clientsocket = serversocket.AcceptTcpClient();
Console.WriteLine("Accepted Connection From Client");
LineMatcher lm = new LineMatcher(clientsocket);
Thread thread = new Thread(new ThreadStart(lm.Run));
thread.Start();
Console.WriteLine("Client connected");
}
Console.WriteLine(" >> exit");
Console.ReadLine();
clientsocket.Close();
serversocket.Stop();
}
}
public class LineMatcher
{
public string fileName = "c:/myfile2.txt";
private TcpClient _client;
public LineMatcher(TcpClient client)
{
_client = client;
}
public void Run()
{
byte[] data = new byte[256];
NetworkStream strm = _client.GetStream();
try
{
using (var r = new StreamReader("c:/myfile2.txt"))
{
string line = "";
bool done = false;
int lineNumber = 0;
String s = r.ReadToEnd();
ASCIIEncoding encoder = new ASCIIEncoding();
while (String.IsNullOrEmpty(s))
{
data = encoder.GetBytes("There is no data in the file.");
Console.WriteLine("There is no data in the file.");
}
if (s.IndexOf("hello", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
data = encoder.GetBytes("It is Present.");
}
else
{
data = encoder.GetBytes("It is not Present");
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
}
strm.Write(data, 0, data.Length);
strm.Flush();
Console.WriteLine("Closing client");
_client.Close();
}
}
答案 0 :(得分:1)
而不是if (s==null)
,您应该检查字符串是否包含单词。非常有创意,我们可以像这样检查单词“word”:if (s.IndexOf("word") >= 0)
,它会在s
中搜索“单词”的位置并返回索引。在C#中,索引始终从0开始。如果字符串“word”未包含在文件字符串中,则返回-1。因此,如果包含该字词,则if
语句将返回true
,如果不包含,则返回false
。
将if
视为仅包含一个参数的语句。该参数可以是true
或false
。 (s==null)
是一个表达式,它返回值true
或false
,然后由if语句使用。
但是,这不起作用,例如,如果文件读取:THIS IS A WORD
,因为“word”不等于“WORD”。你可以通过使用不区分大小写的比较来解决这个问题:
if(s.IndexOf("word", StringComparison.CurrentCultureIgnoreCase) >= 0) {
// contains "word"
} else {
// does not contain "word"
}
请参阅以下内容以供参考
答案 1 :(得分:1)
您的客户端应用程序只能搜索一次。这是因为执行搜索后,您将关闭连接。
Console.WriteLine("Closing client");
_client.Close();
如果您希望连接保持打开状态,则需要包含一个循环,以确保返回到LineMatcher
类的开头重新搜索。
我只是简单地使用IndexOf
方法,而不是检查Contains
这个字符串。虽然IndexOf
旨在查找子字符串在字符串中的位置,但Contains
的构建仅用于检查子字符串是否存在的特定目的。请注意,不不区分大小写。
else if (s.Contains("HTTP"))
{
我强烈建议您首先使用搜索应用程序作为独立应用程序,然后编写启动原始应用程序的telnet服务器。这是两个独立的功能,您会发现单独测试它们要容易得多。
答案 2 :(得分:1)
我解决了。 :)这就是我做到的。有关改进的建议吗?
namespace ServerSideApplication
{
class Program
{
static void Main(string[] args)
{
TcpListener socketListener = new TcpListener(8888);
TcpClient netClient = default(TcpClient);
StreamReader sr;
StringBuilder sb = new StringBuilder();
socketListener.Start();
sr = new StreamReader("c:\\test.txt");
sb.Append(sr.ReadToEnd());
while (true)
{
netClient = socketListener.AcceptTcpClient();
Console.WriteLine("Accepted Connection From Client" + Environment.NewLine + "Client connected");
ServerSide ss = new ServerSide();
ss.startServerSide(netClient, sb);
}
socketListener.Stop();
}
}
class ServerSide
{
TcpClient netClient;
StringBuilder sb;
public void startServerSide(TcpClient netClient, StringBuilder sb)
{
this.netClient = netClient;
this.sb = sb;
Thread thread = new Thread(processRequest);
thread.Start();
}
private void processRequest()
{
byte[] data = new byte[4096];
int bytesRead;
NetworkStream strm = netClient.GetStream();
bytesRead = 0;
try
{
NetworkStream ns = netClient.GetStream();
string clientChar = "", s = "";
do
{
bytesRead = ns.Read(data, 0, (int)data.Length);
clientChar = Encoding.ASCII.GetString(data).Replace("\0", "");
s += clientChar;
} while (clientChar != Environment.NewLine);
s = s.Trim();
ASCIIEncoding encoder = new ASCIIEncoding();
if (String.IsNullOrEmpty(s))
{
data = encoder.GetBytes("There is no data in the file.");
Console.WriteLine("There is no data in the file.");
}
if (sb.ToString().Contains(s))
{
data = encoder.GetBytes("It Is Present In The File.");
}
else
{
data = encoder.GetBytes("It Is Not Present In The File.");
}
strm.Write(data, 0, data.Length);
strm.Flush();
Console.WriteLine("Closing client");
netClient.Close();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
}
}
}
}