有没有开源好用的用C#开发的 TCP Server服务端组件或 类库, .net 自带的TCP类库太弱智了,要达到实用效果还要处理很多东西,如不能多客户同时连接,不能判断异常断开的处理等...我看了一些第三方库 如 LumiSoft.Net 等,都有封装好的FTP HTTP SMTP 等服务器组件,却没有TCP组件,这些服务都是基于TCP的怎么就没有一个现成好用的TCP组件 难道非得自己封装不可么?
调试欢乐多
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|连接成功");
sw.Flush();
SendToAll(list); break;
case "SendTo":
SendToMessage(splitString[1].ToString(),splitString[2].ToString(),splitString[3].ToString());
break;
case "LIST":
List();
break; }
}
}
private void SendToAll(ArrayList al)
{ string Friendlist="FLIST|";
for (int i = 0; i < al.Count;i++)
{
Friendlist += al[i].ToString() + "|";
}
NetworkStream ns=null;
StreamWriter ww=null;
for (int i = 0; i < al.Count; i++)
{
ns = new NetworkStream((Socket)(socketTable[al[i]]));
ww = new StreamWriter(ns);
ww.WriteLine(Friendlist);
ww.Flush(); }
ww.Dispose();
ns.Dispose(); }
private void SendToMessage(string SendUserName,string ReceiveUserName,string msg)
{
Socket sendto = (Socket)(socketTable[ReceiveUserName]);
NetworkStream sendtotream = new NetworkStream(sendto);
sw = new StreamWriter(sendtotream);
sw.WriteLine("MSG|"+SendUserName+"|"+ReceiveUserName+"|"+msg);
sw.Flush();
}
private void List()
{
string userName ="LIST";
for (int i =0; i < list.Count; i++)
{
userName+= "|"+list[i].ToString();
}
sw.WriteLine(userName);
sw.Flush(); }
private void login()
{
} private void Form1_Load(object sender, EventArgs e)
{
}
}
}
让你看看,能得分不oh Ya
http://dev.csdn.net/develop/article/65/65798.shtm昨天研究到凌晨5点 可是除了能连接,并不能成功发送数据,晕死了
http://www.codeproject.com/KB/IP/ChatAsynchTCPSockets.aspxhttp://www.codeproject.com/KB/IP/testingsocketservers.aspxhttp://www.codeproject.com/KB/IP/TCPIPChat.aspxhttp://www.codeproject.com/KB/IP/tcpserverall.aspx
{
public partial class Form1 : Form 看到这2句就看不下去了