Delphi最簡化異步選擇TCP服務器
阿新 • • 發佈:2018-06-21
erro pan FN 簡化 eat 發送 wap bool 服務器
網上Delphi的Socket服務器優良代碼,實在少見,索性寫個簡化的異步Socket服務器,雖然代碼較少,但卻該有的都有了,使用的是異步選擇WSAAsyncSelect,減少了編寫線程的繁瑣。可能會問,性能如何?當然使用窗體消息通知,占用的是主線程,偵聽、發送、多個客戶端的接收都一個線程,大量數據時,性能當然是差強人意的,編寫這個代碼目的也不在於此。但是在實際的項目中,大數據量的情況也不多,以下是代碼:(Delphi7編譯)
{ 最簡化的消息異步Socket 異步選擇WSAAsyncSelect, 沒有64的限制 } program SocketDemo; {$APPTYPE CONSOLE} uses Windows, WinSock; const ListenPort : Word = 12345; BufferSize : DWORD = 1024; type TConn = ^TConnData; TConnData = record FSocket: TSocket; FAddrIn: TSockAddr; Buffer : PChar; BufLen : Integer; end; procedure DoSocketData(Conn: TConn); var S: string; begin Writeln(Conn.Buffer);//這裏插入業務處理代碼 S:= ‘Server echo‘; send(Conn.FSocket, PChar(S)^, Length(S), 0); end; //--------- 以下不要修改 ----------- const wcName : PChar = ‘THrWndClass‘; WM_SOCKET = {WM_USER}$0400 + 101; // 自定義消息 var AddrInLen: Integer = SizeOf(TSockAddr); var FConns: array of TConn; function GetFreeConn: Integer;var i: Integer; begin Result:= -1; for i:=0 to High(FConns) do if FConns[i]=nil then begin Result:= i; Break; end; if Result<0 then begin Result:= Length(FConns); SetLength(FConns, Result+1); end; FConns[Result]:= New(TConn); GetMem(FConns[Result].Buffer, BufferSize+1); FConns[Result].BufLen:= BufferSize; end; function GetCltConn(S: TSocket): Integer; var i: Integer; begin for i:=0 to High(FConns) do if Assigned(FConns[i]) and (FConns[i].FSocket=S) then begin Result:= i; Break; end; end; procedure FreeConn(S: TSocket); var id: Integer; var Conn: TConn; begin id:= GetCltConn(S); Conn:= FConns[id]; if not Assigned(Conn) then Exit; FreeMem(Conn.Buffer); CloseSocket(Conn.FSocket); Dispose(Conn); FConns[id]:= nil; end; function WndProc(wnd, msg, sock, wm: DWORD): Integer; stdcall; var id, AddrLen: Integer; begin Result:= DefWindowProc(wnd, msg, sock, wm); if (msg<>WM_SOCKET) or (wm=0) then Exit; case LoWord(wm) of FD_ACCEPT: begin id:= GetFreeConn; with FConns[id]^ do begin FSocket:= Accept(sock, @FAddrIn, @AddrInLen); WSAAsyncSelect(FSocket, wnd, WM_SOCKET, FD_READ or FD_CLOSE); end; end; FD_READ: begin id:= GetCltConn(sock); with FConns[id]^ do begin BufLen:= Recv(sock, Buffer^, BufferSize, 0); if (BufLen<0) or (BufLen>Buflen) then FreeConn(sock) else begin Buffer[BufLen]:= #0; try DoSocketData(FConns[id]) except end; end; end; end; FD_CLOSE: FreeConn(sock); end; end; function MakeWndHandle(WndProc: Pointer; wcName: PChar): HWND; var wc: TWndClass; begin FillChar(wc, SizeOf(wc), 0); wc.lpfnWndProc := WndProc; wc.hInstance := HInstance; wc.lpszClassName:= wcName; Windows.RegisterClass(wc); Result:= CreateWindow(wcName,‘HrWnd‘,0,0,0,0,0,0,0,HInstance,nil); end; function SrvListen(Port: Word): Boolean; var Wnd: HWND; S: TSocket; Addr: TSockAddrIn; WSAData: TWSAData; begin WSAStartup($0202, WSAData); Addr.sin_family := AF_INET; Addr.sin_port := Swap(Port); Addr.sin_addr.S_addr := 0; S:= Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); Bind(S, Addr, AddrInLen); Wnd:= MakeWndHandle(@WndProc, wcName); WSAAsyncSelect(S, Wnd, WM_SOCKET, FD_ACCEPT or FD_CLOSE); //Writeln(SysErrorMessage(WSAGetLastError()), ‘ Wnd: ‘, Wnd); Listen(S, 5); end; procedure SysFina; begin Windows.UnregisterClass(wcName, HInstance); WSACleanup; end; procedure Stay; var msg: TMsg; begin while GetMessage(msg, 0, 0, 0) do begin TranslateMessage(msg); DispatchMessage (msg); end; PostQuitMessage(0); end; begin //if InitProc <> nil then TProcedure(InitProc); SrvListen(ListenPort); Stay; SysFina; Halt(0); end.
Delphi最簡化異步選擇TCP服務器