如何用C#实现多线程TCP协议的服务器端程序
用C#实现多线程TCP协议的服务器端程序: // /// Tcp客户线程类(服务端),ThreadServerProcessor 线程产生的客户连接,用该线程读写/// public class ThreadClientProcessor{//Tcp连接实例private TcpClienttcpClient;//消息框,本来想写日志用private System.Windows.Forms.ListBox MessageList;private string Password; //该连接登陆密码private string Cmd1Echo;private string Cmd2Echo;private bool ClientLogOn;//客户是否登陆private bool TcpClose;public ThreadClientProcessor(){}//构造函数,参数解释:Tcp客户,消息框,该服务密码(password命令后的参数) ,命令回应串 1,2 ******************public ThreadClientProcessor(TcpClient client , ListBox listBox,string LogonText ,string cmd1echo,string cmd2echo){ClientList.Add(this);//把当前实例加入一个列表中,方便以后控制this.tcpClient=client;this.MessageList=listBox;this.Password=LogonText;this.Cmd1Echo=cmd1echo;this.Cmd2Echo=cmd2echo;this.ClientLogOn=false;this.TcpClose=false;}public static char[] CmdSplit={' '}; //读来的串由' ' 进行分离,命名+' '+参数//public const string[] Cmd=new string[] { "password","cmd1","cmd2","echo","bye"};//该函数由你自己写,这个只是给一个例子,//功能:命令处理器,给个命令串,返回该命令处理结果,把命令和处理结果放在一个文本文件里,便于系统升级public stringTcpCmd(string s){string result;try{string cmdarg=s.Trim();string[] args=cmdarg.Split(CmdSplit);string cmd=args[0].ToLower();switch(cmd ){case "password" :if (args.Length>1){ClientLogOn= Password.Equals(args[1].Trim());result=ClientLogOn? "登陆成功":"密码不正确,未登陆";}else result= "登陆时候,没有输入密码";break;case "cmd1":result=ClientLogOn?this.Cmd1Echo:"该命令无权执行,请先登陆";break;case "cmd2":result=ClientLogOn?this.Cmd2Echo:"该命令无权执行,请先登陆";break;case "echo":result=string.Format("服务器回应:n {0}",s);break;case "bye":this.TcpClose=true;result="DisConnected";break;default:result="不可识别的命令";break;}}catch{result="解析命令发生错误,你输入的是狗屁命令,TMD*^* ";}return result;} //end cmd//定义一个线程,该线程对应的函数是 void start()(不是Start())********************************//一下程序主要是操作该线程public System.Threading.Thread tcpClientThread;//启动客户连接线程 *************************************************************publicvoid Start(){tcpClientThread=new Thread(new ThreadStart(start));tcpClientThread.Priority=ThreadPriority.BelowNormal;tcpClientThread.Start();}//断开该当前实例连接,终止线程 **************************************************************public void Abort(){if (this.tcpClientThread!=null){//tcpClientThread.Interrupt();tcpClientThread.Abort();//一定要等一会儿,以为后边tcpClient.Close()时候,会影响NetWorkStream的操作Thread.Sleep(TimeSpan.FromMilliseconds(100));tcpClient.Close();}}//静态列表,包含了每个连接实例(在构造实例时候使用了 ArrayList.Add( object))private static System.Collections.ArrayList ClientList=new ArrayList();//断开所有的Tcp客户连接,静态方法*************************************************************public static void AbortAllClient(){for(int j=0 ;j< ClientList.Count;j++){//从实例列表中取一个对象,转化为ThreadClientProcessor对象ThreadClientProcessor o=(ThreadClientProcessor ) ClientList[j];//调用ThreadClientProcessor 对象的停止方法o.Abort();}//清除连接列表ClientList.Clear();}//读写连接的函数,用于线程//*******************************************************************privatevoidstart(){byte[] buf=new byte[1024*1024]; //预先定义1MB的缓冲int Len=0; //流的实际长度NetworkStream networkStream=tcpClient.GetStream(); //建立读写Tcp的流try{byte[] p=Encoding.UTF8.GetBytes(" 欢迎光临,请输入密码" );//向Tcp连接写 欢迎消息if (!this.ClientLogOn )networkStream.Write(p,0,p.Length);//开始循环读写tcp流while (!TcpClose){//如果当前线程是在其它状态,(等待挂起,等待终止.....)就结束该循环if (Thread.CurrentThread.ThreadState!=ThreadState.Running)break;//判断Tcp流是否有可读的东西if ( networkStream.DataAvailable){//从流中读取缓冲字节数组Len=networkStream.Read(buf,0,buf.Length);//转化缓冲数组为串string cmd=Encoding.UTF8.GetString(buf,0,Len);this.MessageList.Items.Add("客户机:"+cmd);//处理该缓冲的串(分析命令),分析结果为res串string res=TcpCmd(cmd);//把命令的返回结果res 转化为字节数组byte[]result=Encoding.UTF8.GetBytes(res);//发送结果缓冲数组给客户端networkStream.Write(result,0,result.Length);this.MessageList.Items.Add("服务器回应:"+res);}else{//Thread.Sleep(TimeSpan.FromMilliseconds(200d));//this.MessageList.Items.Add("客户机无命令");//如果当前Tcp连接空闲,客户端没有写入,则当前线程停止200毫秒Thread.Sleep(TimeSpan.FromMilliseconds(200d));} }

tcp多线程收包,应用程序收包乱序
看源代码会比较好。 #define TCP_A_RETRANSMISSION0x0001#define TCP_A_OUT_OF_ORDER0x0200/* RETRANSMISSION/FAST RETRANSMISSION/OUT-OF-ORDER* If the segment contains data (or is a SYN or a FIN) and* if it does not advance the sequence number, it must be one* of these three.* Only test for this if we know what the seq number should be* (tcpd->fwd->nextseq)** Note that a simple KeepAlive is not a retransmission*//* If there were >=2 duplicate ACKs in the reverse direction* (there might be duplicate acks missing from the trace)* and if this sequence number matches those ACKs* and if the packet occurs within 20ms of the last* duplicate ack* then this is a fast retransmission*//* If the segment came <3ms since the segment with the highest* seen sequence number and it doesn't look like a retransmission* then it is an OUT-OF-ORDER segment.* (3ms is an arbitrary number)*/这个大概是说序号有跳跃,就是原来的序号+片段长度<新序号,而且不是快速重传,那就是乱序/* Check for spurious retransmission. If the current seq + segment length* is less then the receivers lastask, the packet contains duplicated* data and may be considered spurious. */

VC中如何实现多线程TCP连接(一个服务端对多个客户端)?一有新连接就新建一条新线程处理这个新建的连...
使用完成端口(IOCP来实现); 搜索关键词 完成端口 或 IOCP即可.

tcpchannel是不是多线程
线程是计算机任务执行的最小单位,多线程也就是说一台计算机同时可以干好几件事,例如同时打字和听音乐,而单线程就是打字时只能打字,其他的干不了。 udp和tcp是两种协议,网络协议是分层的,他们都是传输层协议。所以协议就是一组约定的规则,

求助大神,QT多线程实现TCP通信
QT界面中,点击开始键。 开启thread线程,建立TCP连接。 并在主线程中运行thread对象中的sendData函数,实现TCP发送数据。

本文由 在线网速测试 整理编辑,转载请注明出处。