求 开源TCP Server
有没有开源好用的用C#开发的 TCP Server服务端组件或 类库, .net 自带的TCP类库太弱智了,要
达到实用效果还要处理很多东西,如不能多客户同时连接,不能判断异常断开的处理等...我看了一
些第三方库 如 LumiSoft.Net 等,都有封装好的FTP HTTP SMTP 等服务器组件,却没有TCP组件,这
些服务都是基于TCP的怎么就没有一个现成好用的TCP组件 难道非得自己封装不可么?
------解决方案--------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace QQServer
{
   public partial class Form1 : Form
   {
       Hashtable socketTable = new Hashtable();
       ArrayList list = new ArrayList();
       public Form1()
       {
           InitializeComponent();
           setListBoxCallback = new SetListBoxCallback(SetListBox);
       }
       private delegate void SetListBoxCallback(string str);
       public Socket Server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
       public Socket Listening;
       public NetworkStream NetStream;
       public StreamReader sr ;
       public StreamWriter sw ;
       private SetListBoxCallback setListBoxCallback;
       public void SetListBox(string str)
       {
           //比较调用SetListBox方法的线程和创建listBox1的线程是否同一个线程
           //如果不是,则listBox1的InvokeRequired为true
           if (listbox.InvokeRequired)
           {
               //结果为true,则通过代理执行else中的代码,并传入需要的参数
               listbox.Invoke(setListBoxCallback, str);
           }
           else
           {
               //结果为false,直接执行
               listbox.Items.Add(str);
               listbox.SelectedIndex = listbox.Items.Count - 1;
               listbox.ClearSelected();
           }
       }
       private void button1_Click(object sender, EventArgs e)
       {
           Server.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"),8080));
           Server.Listen(1);
           Thread listens = new Thread(new ThreadStart(Listen) );
           listens.Start();
       }
       private void Listen()
       {
           while (true)
           {
               Listening = Server.Accept();
               NetStream = new NetworkStream(Listening);
               sw = new StreamWriter(NetStream);
               sr = new StreamReader(NetStream);
               SetListBox(Listening.RemoteEndPoint.ToString());              
               Thread Receive = new Thread(new ThreadStart(ReceiveMsg));
               Receive.Start();
           }
       }
       private void ReceiveMsg()
       { bool exitWhile = false;
       while (exitWhile == false)
       {
           string receiveString = null;
           string[] splitString = null;
           try
           {
               receiveString = sr.ReadLine();
           }
           catch
           {
               SetListBox("接收失败");
           }
           if (receiveString == null)
           {
               break;
           }
          splitString = receiveString.Split('|');
           switch (splitString[0])
           {
               case "LOGIN":
                   SetListBox(splitString[1].ToString());
                   if (!socketTable.Contains(splitString[1].ToString()))
                   {
                       socketTable.Add(splitString[1].ToString(), Listening);
                   }
                   list.Add(splitString[1].ToString());
                   sw.WriteLine("LOGINTRUE