套接字.发送和接收.我该如何继续
本文关键字:何继续 继续 套接字 | 更新日期: 2023-09-27 18:29:09
我必须编写一个程序来检查文件中是否存在任何随机字符串。我这样做了……但现在我被要求使用sockets.send-And-receive方法。我已经创建了一个连接并编写了代码,直到这里。。我该如何继续?我想不通。。第一个程序是我在服务器端的尝试程序。第二个是我的实际程序,从文件中搜索字符串。有人能帮我在实际程序中使用套接字的代码吗?非常感谢…:)
class Program
{
static void Main(string[] args)
{
TcpListener serversocket = new TcpListener(8888);
int requestcount = 0;
TcpClient clientsocket = default(TcpClient);
serversocket.Start();
Console.WriteLine(">> Server Started");
clientsocket = serversocket.AcceptTcpClient();
Console.WriteLine("Accept Connection From Client");
requestcount = 0;
while ((true))
{
try
{
requestcount = requestcount + 1;
NetworkStream networkstream = clientsocket.GetStream();
byte[] bytesFrom = new byte[10025];
networkstream.Read(bytesFrom, 0, (int)clientsocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> Data from client - " + dataFromClient);
string serverResponse = "Server response " + Convert.ToString(requestcount);
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkstream.Write(sendBytes, 0, sendBytes.Length);
networkstream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
clientsocket.Close();
serversocket.Stop();
Console.WriteLine(" >> exit");
Console.ReadLine();
}
}
这是我想在上面的程序中使用的程序。
班级计划{
static void Main(string[] args)
{
if (File.Exists("C://myfile2.txt"))
{
var text = File.ReadAllText("C://myfile2.txt");
foreach (var word in new[] { "and", "so", "not", "c", "to", "by", "has", "do", "behavior", "dance", "france", "ok","thast", "please","hello","system","possible","impossible","absolutely","sachin","bradman","schumacher","http","console","application" })
{
var w = word;
new Thread(() => Console.WriteLine("{0}: {1}", w, text.Contains(w) ? "Present" : "Not Present")).Start();
}
}
else
Console.WriteLine("File Does not exist");
Console.ReadLine();
}
}
这是我在没有IDE的情况下写的一个快速而肮脏的想法(---我还没有测试过它---Edit刚刚用netcat测试过它,它工作得很好):
-
请注意,它使用了一个正则表达式。如果单词的查找表变得足够大,那么最好将单词存储在
HashSet<string>
中,并将输入拆分为单词。然后,您可以有效地执行.IntersectWith
,以查看是否有任何单词匹配。 -
请注意,套接字的构造函数是不推荐使用的(应该明确指定要绑定到的IPAddress)
-
原始代码不要求匹配项是单独的单词(
candy
同时匹配c
和and
)。你可能想修复 -
原始"grep"片段中效率低下的部分:
- ReadAllText(无法缩放大文件)
- 在循环中执行多个
.Contains
调用的效率远低于使用(预编译的)正则表达式 - 究竟为什么会在那里产生线索?这实际上只会增加运行时开销,并可能由于对
Console.Out
的访问不同步而导致问题
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Net.Sockets;
class Program
{
private static Regex _regex = new Regex("and|so|not|c|to|by|has|do|behavior|dance|france|ok|thast|please|hello|system|possible|impossible|absolutely|sachin|bradman|schumacher|http|console|application", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
static void Main(string[] args)
{
TcpListener serversocket = new TcpListener(8888);
TcpClient clientsocket = default(TcpClient);
serversocket.Start();
Console.WriteLine(">> Server Started");
clientsocket = serversocket.AcceptTcpClient();
Console.WriteLine("Accept Connection From Client");
try
{
using (var reader = new StreamReader(clientsocket.GetStream()))
{
string line;
int lineNumber = 0;
while (null != (line = reader.ReadLine()))
{
lineNumber += 1;
foreach (Match match in _regex.Matches(line))
{
Console.WriteLine("Line {0} matches {1}", lineNumber, match.Value);
}
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
}
clientsocket.Close();
serversocket.Stop();
Console.WriteLine(" >> exit");
Console.ReadLine();
}
}