新年贺礼

大家新年好!

学习.net的好去处:

http://dotnet.blyct.com/QUICKSTARTV20/aspnet/default.aspx 

阅读更多

****新年贺礼----TListenSocket*****

01-29

rn新年好,新年进步,给大家献上新年礼物,我写的一个类似:Borland Socket Service功能的类,并请大家指出错误。rn本想藏私,不过,没有交流,就没有进步,所以大家进步,哈哈,rn用法简单:rnuses ListenSocket;rn SH:TListenSocket;rnrnSH:=TListenSocket.Create(Self);rnSH.ListPort:=8888;rnSH.Open;rn//OK.你的(SERVER)程序变成一个侦听程序了。ohrnrnrnrnunit ListenSocket;rnrninterfacernrnusesrn Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,rn SConnect,ScktComp,SvcMgr, ActiveX,MidConst,winsock,MyConst;rnrnvar FClientCount:integer;rn FClientThreads:TStringList;rntypern TSocketDispatcherThread = class(TServerClientThread, ISendDataBlock)rn privatern FRefCount: Integer;rn FInterpreter: TDataBlockInterpreter;rn FTransport: ITransport;rn FLastActivity: TDateTime;rn FTimeout: TDateTime;rn FRegisteredOnly: Boolean;rn procedure AddClient;rn procedure RemoveClient;rn protectedrn function CreateServerTransport: ITransport; virtual;rn procedure AddClient;rn procedure RemoveClient; rn IUnknown rn function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;rn function _AddRef: Integer; stdcall;rn function _Release: Integer; stdcall;rn ISendDataBlock rn function Send(const Data: IDataBlock; WaitForResult: Boolean): IDataBlock; stdcall;rn publicrn constructor Create(CreateSuspended: Boolean; ASocket: TServerClientWinSocket;rn const InterceptGUID: string; Timeout: Integer; RegisteredOnly: Boolean);rn procedure ClientExecute; override;rn end;rnrntype MyServerSocket=Class(TServerSocket)rn privatern procedure GetThread(Sender: TObject; ClientSocket: TServerClientWinSocket;var SocketThread: TServerClientThread);rn publicrn constructor Create(AOwner: TComponent); override;rnend;rnrntypern TListenSocket = class(TObject)rn privatern FActive:Boolean;rn FListPort :integer;rn FCacheSize :integer;rn SH:MyServerSocket;rn FItemIndex :integer;rn procedure SetActiveState(Value:boolean);rn function GetClientCount :integer;rn Private declarations rn publicrn property CacheSize :integer read FCacheSize write FCacheSize;rn property ListPort:integer read FListPort write FListPort;rn property Active :boolean read FActive write SetActiveState;rn property ClientCount:integer read GetClientCount;rn publicrn constructor Create(AOwner :TComponent);rn destructor Destroy;override;rn class procedure AddClientThread(Thread :TSocketDispatcherThread);rn class procedure RemoveClientThread(Thread:TSocketDispatcherThread);rn procedure Open;rn procedure Close;rn end;rnrnimplementationrnrnfunction TListenSocket.GetClientCount :integer;rnbeginrn Result:=FClientCount;rnend;rnrnconstructor TListenSocket.Create(AOwner :TComponent);rnbeginrn LoadWinSock2;rn FActive:=False;rn FClientCount:=0;rn FCacheSize :=10;rn FClientThreads:=TStringList.Create;rn SH:=MyServerSocket.Create(nil);rn inherited Create;rnend;rnrndestructor TListenSocket.Destroy;rnbeginrn SetActiveState(False);rn FClientThreads.Free;rn inherited Destroy;rnend;rnrnprocedure TListenSocket.Open;rnbeginrn SetActiveState(True);rnend;rnrnprocedure TListenSocket.Close;rnbeginrn SetActiveState(False);rnend;rnrnclass procedure TListenSocket.AddClientThread(Thread :TSocketDispatcherThread);rnbeginrn Inc(FClientCount);rn FClientThreads.AddObject(Thread.ClientSocket.RemoteHost,Thread);rnend;rnrnclass procedure TListenSocket.RemoveClientThread(Thread :TSocketDispatcherThread);rnvar i:integer;rnbeginrn for i:=0 to FClientThreads.Count -1 dorn beginrn if TSocketDispatcherThread(FClientThreads.Objects[i])=Thread thenrn beginrn FClientThreads.Delete(i);rn Dec(FClientCount);rn end;rn end;rnend;rnrnprocedure TListenSocket.SetActiveState(Value:boolean);rnvar i:integer;rnbeginrn if Value thenrn beginrn SH.Close;rn SH.Port :=ListPort;rn SH.ThreadCacheSize :=CacheSize;rn SH.Open;rn end elsern if not Value thenrn SH.Close;rn FActive:=Value;rnend;rnrnMyServerSocket Classrnprocedure MyServerSocket.GetThread(Sender: TObject; ClientSocket: TServerClientWinSocket;rn var SocketThread: TServerClientThread);rnbeginrn SocketThread:=TSocketDispatcherThread.Create(false,ClientSocket,'',0,false);rnend;rnrnconstructor MyServerSocket.Create(AOwner: TComponent);rnbeginrn inherited Create(AOwner);rn ServerType := stThreadBlocking;rn OnGetThread := GetThread;rnend;rnMyServerSocket Class overrnrnTSocketDispatcherThread classrnfunction TSocketDispatcherThread.CreateServerTransport: ITransport;rnvarrn SocketTransport: TSocketTransport;rnbeginrn SocketTransport := TSocketTransport.Create;rn SocketTransport.Socket := ClientSocket;rn Result := SocketTransport as ITransport;rnend;rnrnconstructor TSocketDispatcherThread.Create(CreateSuspended: Boolean; ASocket: TServerClientWinSocket;rn const InterceptGUID: string; Timeout: Integer; RegisteredOnly: Boolean);rnbeginrn FTimeout:=EncodeTime(Timeout div 60, Timeout mod 60, 0, 0);rn FRegisteredOnly:=RegisteredOnly;rn FLastActivity:=Now;rn inherited Create(CreateSuspended, ASocket);rnend;rnrnfunction TSocketDispatcherThread.Send(const Data:IDataBlock; WaitForResult:Boolean):IDataBlock;rnbeginrn FTransport.Send(Data);rn if WaitForResult thenrn while True dorn beginrn Result := FTransport.Receive(True, 0);rn if Result = nil then break;rn if (Result.Signature and ResultSig) = ResultSig thenrn break elsern FInterpreter.InterpretData(Result);rn end;rnend;rnrnprocedure TSocketDispatcherThread.AddClient;rnbeginrn TListenSocket.AddClientThread(Self);rnend;rnrnprocedure TSocketDispatcherThread.RemoveClient;rnbeginrn TListenSocket.RemoveClientThread(Self);rnend;rnrnprocedure TSocketDispatcherThread.ClientExecute;rnvarrn Data: IDataBlock;rn msg: TMsg;rn Obj: ISendDataBlock;rn Event: THandle;rn WaitTime: DWord;rnbeginrn CoInitialize(nil);rn tryrn Synchronize(AddClient);rn FTransport := CreateServerTransport;rn tryrn Event := FTransport.GetWaitEvent;rn PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);rn GetInterface(ISendDataBlock, Obj);rn if FRegisteredOnly thenrn FInterpreter := TDataBlockInterpreter.Create(Obj, SSockets) elsern FInterpreter := TDataBlockInterpreter.Create(Obj, '');rn tryrn Obj := nil;rn if FTimeout = 0 thenrn WaitTime := INFINITE elsern WaitTime := 60000; //MAXIMUM_WAIT_OBJECTSrn while not Terminated and FTransport.Connected dorn tryrn case MsgWaitForMultipleObjects(1, Event, False, WaitTime, QS_ALLEVENTS) ofrn WAIT_OBJECT_0:rn beginrn WSAResetEvent(Event);rn Data := FTransport.Receive(False, 0);rn if Assigned(Data) thenrn beginrn FLastActivity := Now;rn FInterpreter.InterpretData(Data);rn Data := nil;rn FLastActivity := Now;rn end;rn end;rn WAIT_OBJECT_0 + 1:rn while PeekMessage(msg, 0, 0, 0, PM_REMOVE) dorn DispatchMessage(msg);rn WAIT_TIMEOUT:rn if (FTimeout > 0) and ((Now - FLastActivity) > FTimeout) thenrn FTransport.Connected := False;rn end;rn exceptrn FTransport.Connected := False;rn end;rn finallyrn FInterpreter.Free;rn FInterpreter := nil;rn end;rn finallyrn FTransport := nil;rn end;rn finallyrn CoUninitialize;rn Synchronize(RemoveClient);rn end;rnend;rnrnfunction TSocketDispatcherThread.QueryInterface(const IID: TGUID; out Obj): HResult;rnbeginrn if GetInterface(IID, Obj) then Result := 0 else Result := E_NOINTERFACE;rnend;rnrnfunction TSocketDispatcherThread._AddRef: Integer;rnbeginrn Inc(FRefCount);rn Result := FRefCount;rnend;rnrnfunction TSocketDispatcherThread._Release: Integer;rnbeginrn Dec(FRefCount);rn Result := FRefCount;rnend;rnTSocketDispatcherThread class overrnrnend.

给您新年贺礼----4版块程序员记忆力调查结果!

01-06

========================原贴=============================rn我的记忆力在C左右,我问了几个同学,好像他们的记忆力都不怎么样,不知与鼻炎有不有关系??rn请选出你目前记忆力的状况:rnrn1、有鼻炎rnA、我的记性差极了rnB、常常觉得自己有些事情记不起来(比如:人名什么的)rnC、有过记不起来的感觉(对某几次类似的情况记记忆犹新)rnD、好像在C、E之间rnE、感觉自己记忆力还不错rnF、没说的,我的记忆力好极了rn相关链接:rnhttp://expert.csdn.net/Expert/topic/1275/1275527.xml?temp=.1426508rnhttp://expert.csdn.net/Expert/topic/1267/1267945.xml?temp=.5599481rnhttp://expert.csdn.net/Expert/topic/1275/1275559.xml?temp=.0819208rnhttp://expert.csdn.net/Expert/topic/1275/1275531.xml?temp=.4330866rn==========================相关网站================================rnhttp://www.biyanbing.com/kfbl.htm 疾病常识rnhttp://www.china-road.com/biyan/btt/4.htm我国鼻炎治疗技术取得重大突破 rnhttp://www.china-road.com/biyan/bycsh/INDEX.HTM 太多鼻炎知识rn==================================================================rn==========================调查结果=================================rnA B C D E F 1rn8 30 32 27 7 5 22 (人数)rnrn初步结论:rn大多数人的记忆力都在bcd之间,一般来说鼻炎在很大程度上影响记忆力。rn记忆力对程序员来说与智力、理解力一样重要,记忆力好,可以免去您很多的麻烦!rn大家可以去上面有关鼻炎的网站看看!rnrn谢谢大家的支持!rn2003/01/06 21:22

没有更多推荐了,返回首页