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

增强功能:

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

下载地址:

TcpClientPlus.zip

演示程序界面:

演示程序源代码:

[csharp]  view plain  copy
  1. using System;  
  2. using System.Net;  
  3. using System.Net.Sockets;  
  4. using System.Windows.Forms;  
  5. using Splash.Net.Sockets;  
  6. using Splash.Windows.Forms;  
  7.   
  8. namespace RunServer  
  9. {  
  10.     public partial class Form1 : Form  
  11.     {  
  12.         private TcpListenerPlus Server;  
  13.   
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }         
  18.   
  19.         private void button_Start_Click(object sender, EventArgs e)  
  20.         {  
  21.             if (Server == null)  
  22.             {  
  23.                 try  
  24.                 {  
  25.                     Server = new TcpListenerPlus(IPAddress.Parse(textBox_IP.Text), Convert.ToInt32(textBox_Port.Text));  
  26.                     Server.OnThreadTaskRequest += new TcpListenerPlus.ThreadTaskRequest(GetAnswer);  
  27.                     ((Button)sender).Text = "关闭服务器";  
  28.                 }  
  29.   
  30.                 catch (Exception)  
  31.                 {  
  32.                     if (Server != null)  
  33.                     {  
  34.                         Server.Stop();  
  35.                         Server = null;  
  36.                     }  
  37.                     MessageBoxPlus.Show(this"启动服务器失败!""信息");  
  38.                 }  
  39.             }  
  40.             else  
  41.             {  
  42.                 Server.Stop();  
  43.                 Server = null;  
  44.                 ((Button)sender).Text = "启动服务器";  
  45.             }   
  46.         }  
  47.   
  48.         private void button_Clear_Click(object sender, EventArgs e)  
  49.         {  
  50.             textBox_Notes.Clear();  
  51.         }  
  52.   
  53.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  54.         {  
  55.             if (Server != null)  
  56.             {  
  57.                 Server.Stop();  
  58.             }  
  59.         }  
  60.   
  61.         private void GetAnswer(object sender, EventArgs e)  
  62.         {  
  63.             TcpClient tcpClient = (TcpClient)sender;  
  64.             using (NetworkStreamPlus Stream = new NetworkStreamPlus(tcpClient.GetStream()))  
  65.             {   // 调整接收缓冲区大小  
  66.                 Stream.ReceiveBufferSize = tcpClient.ReceiveBufferSize;  
  67.                 Stream.SecretKey = GetSecretKey();  // 加密密钥              
  68.                 while (true)  
  69.                 {  
  70.                     try  
  71.                     {  
  72.                         // 获取查询内容  
  73.                         String Question;  
  74.                         Stream.Read(out Question);  
  75.   
  76.                         // 返回查询结果  
  77.                         String Answer = Question.ToUpper();  
  78.                         Stream.Write(Answer);  
  79.   
  80.                         SetText(Question + "\r\n");  
  81.                         SetText(Answer + "\r\n\r\n");  
  82.                     }  
  83.   
  84.                     catch (Exception ex)  
  85.                     {  
  86.                         Type type = ex.GetType();  
  87.                         if (type == typeof(TimeoutException))  
  88.                         {   // 超时异常,不中断连接  
  89.                             SetText("数据超时失败!\r\n\r\n");  
  90.                         }                          
  91.                         else  
  92.                         {   // 仍旧抛出异常,中断连接  
  93.                             SetText("中断连接异常原因:" + type.Name + "\r\n\r\n");  
  94.                             throw ex;     
  95.                         }  
  96.                     }  
  97.                 }  
  98.             }  
  99.         }  
  100.   
  101.         // 对 Windows 窗体控件进行线程安全调用  
  102.         private String GetSecretKey()  
  103.         {  
  104.             if (textBox_SecretKey.InvokeRequired)  
  105.             {  
  106.                 return (String)textBox_SecretKey.Invoke(new Func<String>(() => { return textBox_SecretKey.Text; }));  
  107.             }  
  108.             else  
  109.             {  
  110.                 return textBox_SecretKey.Text;  
  111.             }  
  112.         }  
  113.   
  114.         // 对 Windows 窗体控件进行线程安全调用  
  115.         private void SetText(String text)  
  116.         {  
  117.             if (textBox_Notes.InvokeRequired)  
  118.             {  
  119.                 textBox_Notes.BeginInvoke(new Action<String>((msg) =>  
  120.                 {  
  121.                     textBox_Notes.AppendText(msg);  
  122.                 }), text);  
  123.             }  
  124.             else  
  125.             {  
  126.                 textBox_Notes.AppendText(text);  
  127.             }  
  128.         }  
  129.     }  
  130. }  

原文转自:http://blog.csdn.net/jhqin/article/details/7552886
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值