首页 新闻 会员 周边 捐助

动态创建TClientSocket出现问题,大神赐教一下。

0
悬赏园豆:100 [已解决问题] 解决于 2015-08-16 14:07

编译Client代码的时候出现这个错误

之前可以编译,但是又连不上服务器,发不了消息,这是为什么,大神能帮我看看代码哪里出了问题吗?

 1 unit Unit1;
 2  
 3 interface
 4  
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls, ScktComp;
 8 
 9 type
10   TForm1 = class(TForm)
11     mmo1: TMemo;
12     btn1: TButton;
13     btn2: TButton;
14     procedure FormCreate(Sender: TObject);
15     procedure FormDestroy(Sender: TObject);
16     procedure btn2Click(Sender: TObject);
17   private
18     FClient: TClientSocket;
19     procedure OnSocketRead(Sender: TObject; Socket: TCustomWinSocket);
20     procedure OnSocketConnect(Sender: TObject; Socket: TCustomWinSocket);
21   public
22     { Public declarations }
23   end;
24  
25 var
26   Form1: TForm1;
27 
28 implementation
29 
30 {$R *.dfm}
31  
32 procedure TForm1.btn2Click(Sender: TObject);
33 begin
34    FClient.Host := '127.0.0.1';
35   FClient.Port := 5200;
36   FClient.Open;
37 end;
38 
39 procedure TForm1.FormCreate(Sender: TObject);
40 begin
41   FClient := TClientSocket.Create(nil);
42   FClient.OnRead := OnSocketRead;
43   FClient.OnConnect := OnSocketConnect;
44 end;
45 
46 procedure TForm1.FormDestroy(Sender: TObject);
47 begin
48   FClient.Free;
49 end;
50 
51 procedure TForm1.OnSocketRead(Sender: TObject; Socket: TCustomWinSocket);
52 begin
53   mmo1.Lines.Add(Socket.ReceiveText);
54 end;
55 
56 
57 procedure TForm1.OnSocketConnect(Sender: TObject; Socket: TCustomWinSocket);
58 begin
59   Socket.SendText('hello');
60 end;
61  
62 end.
 1 //server
 2 
 3 unit Unit1;
 4 
 5 interface
 6 
 7 uses
 8   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 9   Dialogs, StdCtrls, ScktComp;
10 
11 type
12   TForm1 = class(TForm)
13     mmo1: TMemo;
14     btn1: TButton;
15     srvrsckt1: TServerSocket;
16 
17     procedure OnSocketConnect(Sender : TObject; Socket : TCustomWinSocket);
18     procedure OnSocketRead(Sender : TObject; Socket : TCustomWinSocket);
19     procedure OnSocketWrite(Sender : TObject; Socket : TCustomWinSocket);
20     procedure btn1Click(Sender: TObject);
21 
22   end;
23 
24 var
25   Form1: TForm1;
26 
27 implementation
28 
29 {$R *.dfm}
30 
31 procedure TForm1.btn1Click(Sender: TObject);
32 begin
33   if not srvrsckt1.Active then
34   begin
35     srvrsckt1.Active := True;
36     srvrsckt1.Port := 5200;
37   end
38   else
39     mmo1.Lines.Add('server has been open');
40 end;
41 
42 procedure TForm1.OnSocketConnect(Sender: TObject; Socket: TCustomWinSocket);
43 begin
44   Socket.SendText('收到连接请求');
45 end;
46 
47 procedure TForm1.OnSocketRead(Sender: TObject; Socket: TCustomWinSocket);
48 begin
49   mmo1.Lines.Add(Socket.ReceiveText);
50 end;
51 
52 procedure TForm1.OnSocketWrite(Sender: TObject; Socket: TCustomWinSocket);
53 begin
54   mmo1.Lines.Add('调用write');
55 end;
56 
57 end.
ChenYao_Freedom的主页 ChenYao_Freedom | 初学一级 | 园豆:5
提问于:2015-08-16 13:05
< >
分享
最佳答案
0

代码没有问题,只是自己脑残了。

ChenYao_Freedom | 初学一级 |园豆:5 | 2015-08-16 14:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册