大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> 一个server基类

一个server基类

时间: 2021-07-31 作者:daque

 常用的搜集server如http/ftp/pop3/smtp等基础上的处事道理都差不离,监听一个端口,等候存户端的socket贯穿,而后同存户端通信,结束后封闭贯穿。即使要实行的比拟完备,那就须要多线程,而且须要处置那些线程。底下给出一个server笼统类,运用一个关照线程来处置贯穿线程,即使要写一个server那只须要接受这个类,并实行其笼统本领就不妨了。using system;using system.threading ;using system.io;using system.net;using system.net.sockets;using system.collections ;using system.text ;using system.diagnostics ;namespace bigeagle.net.server{/// <summary>/// <br>server类</br>/// <br>author: bigeagle@163.net</br>/// <br>date :2002/02/04</br>/// <br>history: 2002/02/04 finished</br>/// <br>todo: 此刻的类构造不尽有理,未来商量运用delgates再不同winform绑定。</br>/// </summary>/// <remarks>/// <br>笼统类,用来少许罕见的搜集server的基类,如http,ftp,mail等。</br>/// <br>重要功效是监听一个端口,而后运用线程池起规则数目的线程等候存户端贯穿/// ,同声有一个保护线程监视野程运用情景,即使一切线程都在处事,则起一个清闲线程/// 连接等候存户端的贯穿。/// </br>/// <br>子类接受后必需实行其oncommunication()笼统本领来实行本人要做的处事。</br>/// </remarks>public abstract class server{#region 恒量/// <summary>/// 保护线程处事功夫间隙/// </summary>/// <remarks>每隔几何毫秒保护线程处事一次</remarks>const int watching_interval = 10000 ;#endregion#region 静态变量/// <summary>/// 暂时起的线程数/// </summary>static int intthreadcount ;/// <summary>/// 正在处事的线程数/// </summary>static int intworkthreadcount ;#endregion#region 分子变量设置/// <summary>/// 线程旗号/// </summary>private autoresetevent myevent ;private bool m_bisinitok ;/// <summary>/// server名/// </summary>protected string m_strservername ;/// <summary>/// 退出标记/// </summary>protected bool m_bcanexit ;/// <summary>/// 监听端标语/// </summary>protected int m_intport ;/// <summary>/// 端口监听/// </summary>protectedtcplistener m_objlistener ;/// <summary>/// 保护线程/// </summary>protected thread m_objwatchthread ;/// <summary>/// 最小监听线程数/// </summary>protected int m_intminthreadscount ;/// <summary>/// 最大监听线程数/// </summary>protected int m_intmaxthreadscount ;/// <summary>/// 源代码办法/// </summary>protected system.text.encoding m_objencoding ;#endregion#region 属性设置public string servername{get{return this.m_strservername ;}set{this.m_strservername = value ;}}//end property/// <summary>/// 存取端口的属性/// </summary>public int port{get{return this.m_intport ;}set{this.m_intport = value ;}}//end property/// <summary>/// 存取最小处事线程数的属性/// </summary>/// <remarks>server发端时启用的线程数,必需在1-999之间</remarks>public int minthreadscount{get{return this.m_intminthreadscount ;}set{if(value > 0 && value < 1000){this.m_intminthreadscount = value ;}}}//end property/// <summary>/// 存取最大处事线程数的属性/// </summary>/// <remarks>server最大能启用的线程数,必需在1-999之间,而且不许小于minthreadscounts</remarks>public int maxthreadscount{get{return this.m_intmaxthreadscount ;}set{if(value > 0 && value < 1000){this.m_intmaxthreadscount = value ;}}}//end property/// <summary>/// server默许源代码办法/// </summary>public system.text.encoding encoding{get{return this.m_objencoding ;}set{this.m_objencoding = value ;}}//end property#endregion#region 结构因变量/// <summary>/// 结构因变量/// </summary>public server(){this.m_strservername= "bigelage general server" ;this.m_bcanexit= false ;this.m_intport= 0 ;this.m_objwatchthread= new thread(new threadstart(onwatch)) ;this.m_objencoding= system.text.encoding.default ;this.m_intmaxthreadscount= 10 ;this.m_intminthreadscount= 1 ;this.myevent= new autoresetevent(false) ;this.m_bisinitok= false ; }//end method/// <summary>/// 重载结构因变量/// </summary>/// <param name="a_strservername">server名</param>/// <param name="a_intport">监听端标语</param>public server(string a_strservername , int a_intport){this.m_strservername= a_strservername ;this.m_bcanexit= false ;this.m_intport= a_intport ;this.m_objwatchthread= new thread(new threadstart(onwatch)) ;this.m_objencoding= system.text.encoding.default ;this.m_intmaxthreadscount= 10 ;this.m_intminthreadscount= 1 ;this.myevent= new autoresetevent(false) ;this.m_bisinitok= false ; }//end method/// <summary>/// 析构因变量/// </summary>~server(){try{if(this.m_objwatchthread.threadstate == system.threading.threadstate.running){this.m_objwatchthread.interrupt() ;this.m_objwatchthread = null ;}this.m_objlistener.stop() ;this.m_objlistener = null ;}catch(exception e){#if debugconsole.writeline("中断缺点:{0}" , e.tostring()) ;#endif//debug}}#endregion#region 独占因变量/// <summary>/// 保护线程处事因变量/// </summary>private void onwatch(){#if debugconsole.writeline("watching thread now begin...") ;#endif//debugwhile(!this.m_bcanexit){#if debugconsole.writeline("now {0} threads is started , now {1} thread is working" , intthreadcount , intworkthreadcount) ;#endif//debug///即使一切线程都在处事,起一个清闲线程来监听if(this.m_bisinitok){if(intworkthreadcount > 0 && intworkthreadcount >= intthreadcount && intthreadcount < this.m_intmaxthreadscount){threadpool.queueuserworkitem(new waitcallback(startlisten) , this.myevent) ;}}else{#if debugconsole.writeline("server is initialize now ...") ;#endif//debug}thread.sleep(watching_interval) ;}}//end method#endregion#region 养护因变量protected void initialize() {this.m_objwatchthread.start() ;this.m_objwatchthread.isbackground = true ;//this.m_objwatchthread.join() ;console.write("\r\n{0} is initializing...." , this.m_strservername) ;#if debugdebug.assert(this.m_intport != 0 , "未指定端口!") ;#endif//debug//绑定监听端口this.m_objlistener = new tcplistener(this.m_intport) ;this.m_bisinitok = true ;}//end method/// <summary>/// 监听线程处事因变量/// </summary>/// <param name="state">autoresetevent东西</param>protected void startlisten(object state){#if debugconsole.writeline("thread{0} is waiting..." , thread.currentthread.gethashcode()) ;#endif//debug//线程计数加一interlocked.increment(ref intthreadcount) ;try{while(!this.m_bcanexit){//等候接受一个新贯穿socket mysocket = this.m_objlistener.acceptsocket() ;#if debugconsole.writeline("thread {0} begin working..." , thread.currentthread.gethashcode()) ;#endif//debugif(mysocket.connected){//处事线程计数加一interlocked.increment(ref intworkthreadcount) ;//同存户端交互docommunication(mysocket) ;//结束后处事线程计数减一interlocked.decrement(ref intworkthreadcount) ;mysocket.close() ;#if debugconsole.writeline("thread {0} finished work" , thread.currentthread.gethashcode()) ;#endif//debugif(intworkthreadcount < intthreadcount - 1 && intthreadcount > this.m_intminthreadscount){#if debugconsole.writeline("too many not working thread , thread {0} will exit." , thread.currentthread.gethashcode()) ;#endif//debuginterlocked.decrement(ref intthreadcount) ;return ;}}}//end while}catch(socketexception se){#if debugconsole.writeline("展示搜集缺点,处事线程封闭。缺点{0}" , se.tostring()) ;#endif//debuginterlocked.decrement(ref intthreadcount) ;interlocked.decrement(ref intworkthreadcount) ;}catch(exception e){#if debugconsole.writeline("展示缺点:{0}" , e.message) ;#endif//debuginterlocked.decrement(ref intthreadcount) ;}//if(intworkthreadcount == 0)//{//((autoresetevent)state).set() ;//}}//end method/// <summary>/// 笼统本领,同存户端通信/// </summary>/// <param name="a_objsocket">暂时套接字</param>protected abstract void docommunication(socket a_objsocket) ;#endregion#region 大众本领/// <summary>/// 运转/// </summary>public void run(){initialize() ;console.writeline("{0} is running..." , this.m_strservername) ;try{//发端监听端口this.m_objlistener.start() ;//起最小处事线程数目的端口监听线程for(int i = 0 ; i < this.m_intminthreadscount ; i ++){threadpool.queueuserworkitem(new waitcallback(startlisten) , myevent) ;}myevent.waitone() ;#if debugconsole.writeline("all thread exit") ;#endif}catch (socketexception socketerror){if (socketerror.errorcode == 10048){console.writeline("connection to this port failed. " + " there is another server is listening on this port.");}else{console.writeline("a socket error occours:{0}" ,socketerror.message) ;}}catch(exception e){console.writeline("an error occours:{0}" , e.message) ; }}//end methodpublic void stop(){console.writeline("{0} try to stop . " , this.m_strservername) ;//发出退出旗号this.m_bcanexit = true ;//等候一切处事的线程中断工作,500毫秒查看一次while(intworkthreadcount != 0){thread.sleep(500) ;}//发出线程中断旗号,报告主步调不妨退出this.myevent.set() ;}#endregion}//end class}//end namespace

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.