C#:一个增强的TcpListener(*)服务器演示程序及源代码下载

增强功能:

  1. 独立线程侦听连接请求
  2. 线程池处理连接通信
  3. AES 256位数据加密传输

下载地址:

TcpClientPlus.zip

演示程序界面:

演示程序源代码:

using System;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using Splash.Net.Sockets;
using Splash.Windows.Forms;

namespace RunServer
{
    public partial class Form1 : Form
    {
        private TcpListenerPlus Server;

        public Form1()
        {
            InitializeComponent();
        }       

        private void button_Start_Click(object sender, EventArgs e)
        {
            if (Server == null)
            {
                try
                {
                    Server = new TcpListenerPlus(IPAddress.Parse(textBox_IP.Text), Convert.ToInt32(textBox_Port.Text));
                    Server.OnThreadTaskRequest += new TcpListenerPlus.ThreadTaskRequest(GetAnswer);
                    ((Button)sender).Text = "关闭服务器";
                }

                catch (Exception)
                {
                    if (Server != null)
                    {
                        Server.Stop();
                        Server = null;
                    }
                    MessageBoxPlus.Show(this, "启动服务器失败!", "信息");
                }
            }
            else
            {
                Server.Stop();
                Server = null;
                ((Button)sender).Text = "启动服务器";
            } 
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            textBox_Notes.Clear();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Server != null)
            {
                Server.Stop();
            }
        }

        private void GetAnswer(object sender, EventArgs e)
        {
            TcpClient tcpClient = (TcpClient)sender;
            using (NetworkStreamPlus Stream = new NetworkStreamPlus(tcpClient.GetStream()))
            {   // 调整接收缓冲区大小
                Stream.ReceiveBufferSize = tcpClient.ReceiveBufferSize;
                Stream.SecretKey = GetSecretKey();  // 加密密钥            
                while (true)
                {
                    try
                    {
                        // 获取查询内容
                        String Question;
                        Stream.Read(out Question);

                        // 返回查询结果
                        String Answer = Question.ToUpper();
                        Stream.Write(Answer);

                        SetText(Question + "\r\n");
                        SetText(Answer + "\r\n\r\n");
                    }

                    catch (Exception ex)
                    {
                        Type type = ex.GetType();
                        if (type == typeof(TimeoutException))
                        {   // 超时异常,不中断连接
                            SetText("数据超时失败!\r\n\r\n");
                        }                        
                        else
                        {   // 仍旧抛出异常,中断连接
                            SetText("中断连接异常原因:" + type.Name + "\r\n\r\n");
                            throw ex;   
                        }
                    }
                }
            }
        }

        // 对 Windows 窗体控件进行线程安全调用
        private String GetSecretKey()
        {
            if (textBox_SecretKey.InvokeRequired)
            {
                return (String)textBox_SecretKey.Invoke(new Func<String>(() => { return textBox_SecretKey.Text; }));
            }
            else
            {
                return textBox_SecretKey.Text;
            }
        }

        // 对 Windows 窗体控件进行线程安全调用
        private void SetText(String text)
        {
            if (textBox_Notes.InvokeRequired)
            {
                textBox_Notes.BeginInvoke(new Action<String>((msg) =>
                {
                    textBox_Notes.AppendText(msg);
                }), text);
            }
            else
            {
                textBox_Notes.AppendText(text);
            }
        }
    }
}


 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页