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);
            }
        }
    }
}


 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值