我正在编写TCP / IP客户端服务器,正在分析词法分析器以检查Pascal的过程。请
告诉我如何重新制作程序,以便使用池线程处理来自客户端的查询?
客户端。代码如下:
using System;
using System.Net.Sockets;
using System.Text;
namespace Client
{
class Program
{
static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
socket.Connect("127.0.0.1", 904);
string message = "proc1('2',2);";//Console.ReadLine();
byte[] buffer = Encoding.UTF8.GetBytes(message);
socket.Send(buffer);
Console.ReadLine();
}
}
}
服务器。代码如下:
using System;
using System.Net.Sockets;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
namespace Server
{
class Program
{
static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
socket.Bind(new IPEndPoint(IPAddress.Any,904));
socket.Listen(10);
Socket client = socket.Accept();
Console.WriteLine("Connected");
byte[] buffer = new byte[256];
client.Receive(buffer);
string s = Encoding.UTF8.GetString(buffer);
Regex regex = new Regex(@"^[_a-zA-Z]\w*\((?:(?:'[\d\w_]+'|""[\d\w_]*""|[\d\w_]+),?)*\);");
MatchCollection matches = regex.Matches(s);
if (matches.Count > 0)
{
foreach (Match match in matches)
Console.WriteLine("true: {0}",match.Value);
}
else
{
Console.WriteLine("false");
}
Console.ReadLine();
}
}
}