高手請過來看看,包裝ServerSocket的問題(50分)

  • 主题发起人 主题发起人 hgptm
  • 开始时间 开始时间
H

hgptm

Unregistered / Unconfirmed
GUEST, unregistred user!
我代碼如下:

unit DataInterFace;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ScktComp;

type
TDummy = class

class Procedure OnGetThread(Sender: TObject; ClientSocket: TServerClientWinSocket; var SocketThread: TServerClientThread);
end;

TSerSck = Class
private

public
SerSckt: TServerSocket;

constructor Create;
destructor Destroy; override;
end;

implementation

class Procedure TDummy.OnGetThread(Sender: TObject; ClientSocket: TServerClientWinSocket; var SocketThread: TServerClientThread);
begin
SHowmessage('hellow');
end;

constructor TSerSck.Create;
begin
inherited Create;

SerSckt.Active := false; //問題在這里(執行總是出錯)..[:(]
SerSckt.Port := 8888;
SerSckt.ServerType := stThreadBlocking;
SerSckt.Active := true;
end;

destructor TSerSck.Destroy;
begin
//
inherited;

end;

end.
 
这种基本的错误我是很就没见过了!

constructor TSerSck.Create;
begin
inherited Create;
//你还没将TServerSocket事例化啊 要先创建类才可使用类啊!
SerSckt := TServerSocket.Create(Self);
SerSckt.Active := false; //問題在這里(執行總是出錯)..
SerSckt.Port := 8888;
SerSckt.ServerType := stThreadBlocking;
SerSckt.Active := true;
end;
 
To:miros兄

constructor TSerSck.Create;
begin
inherited Create;

//加入實例化后,編譯通不過,因為當前我創建的是沌unit (非帶form的單元,
//如果是帶form的單元,執行是沒問題的,請見如下,我的問題是不想建立form,在我自定義的Class中封裝TserverSocket,不知道可行不??最好有代碼,謝謝).
SerSckt := TServerSocket.Create(self);
SerSckt.Active := false;
SerSckt.Port := 8888;
SerSckt.ServerType := stThreadBlocking;
SerSckt.Active := true;
end;


帶form的單元實例TserverSocket代碼:
unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, IdFingerServer, IdBaseComponent, IdComponent,
IdTCPServer, StdCtrls;

type
Tabc = class

class Procedure OnGetThread(Sender: TObject; ClientSocket: TServerClientWinSocket; var SocketThread: TServerClientThread);
end;

type
TForm1 = class(TForm)
Button1: TButton;
ClientSocket1: TClientSocket;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses DataInterFace;

{$R *.dfm}

var
//gScket : TSerSck;

aSerSckt: TServerSocket;


class Procedure Tabc.OnGetThread(Sender: TObject; ClientSocket: TServerClientWinSocket; var SocketThread: TServerClientThread);
var str: string;
ii: integer;
cc: array[0..5] of char;
begin
//str:= ClientSocket.ReceiveText ;

ii:= ClientSocket.ReceiveLength;
fillchar(cc,sizeof(Cc),' ');
ClientSocket.ReceiveBuf(cc,ii);

SHowmessage(inttostr(ii));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//gScket:= TserSck.Create ;


aSerSckt:= TServerSocket.Create(self);
aSerSckt.Active := false;
aSerSckt.Port := 8888;
aSerSckt.ServerType := stThreadBlocking;
aSersckt.OnGetThread := Tabc.OnGetThread;
aSerSckt.Active := true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ClientSocket1.Active := true;

ClientSocket1.Socket.SendText('Ok');
end;

end.


多謝不吝賜教!!!!
 
如果分少,稍后加補!!!!!
急切需求解決!!!
我听說用Com可以封裝,可令鄙人對Com不是了解!!
 
是我害了你 我写错了。。。。
constructor TSerSck.Create;
begin
inherited Create;
SerSckt := TServerSocket.Create(nil);
//这就不用了 SerSckt.Active := false; //問題在這里(執行總是出錯)..
SerSckt.Port := 8888;
SerSckt.ServerType := stThreadBlocking;
SerSckt.Active := true;
end;

destructor TSerSck.Destroy;
begin
if SerSckt <> nil then
begin
SerSckt.Active := False;
FreeAndnil(SerSckt);
end;
inherited;

end;
 
十萬分感謝!!!!
:>
 
后退
顶部