首页 > 解决方案 > TCP / IP客户端-服务器,c#中的词法分析器

问题描述

我正在编写一个 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();
        }
    }
}

标签: c#tcpthreadpooltcpclienttcp-ip

解决方案


推荐阅读