高分求助:: ServerSocket控件的问题! ( 积分: 200 )

  • 主题发起人 主题发起人 lovenuo
  • 开始时间 开始时间
L

lovenuo

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是服务器端的代码,用客户端连接服务器端,发送数据A时,发生错误!

unit U_Server;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
ServerSocket1: TServerSocket;
StatusBar1: TStatusBar;
procedure Button4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
Application.Terminate;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Enabled:=False;
Button2.Enabled:=True;
Button3.Enabled:=True;
ServerSocket1.Port:=8098;
ServerSocket1.Active:=True;
StatusBar1.SimpleText:='服务器已成功启动!'
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Button1.Enabled:=True;
Button2.Enabled:=False;
ServerSocket1.Active:=False;
StatusBar1.SimpleText:='服务器已成功停止!'
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
ss:string;
begin
sfengbao:=ServerSocket1.Socket.ReceiveText;
if pos('A',ss)<>0 then
begin
ServerSocket1.Socket.SendText('22');
end;
end;

end.

下面是错误的图片,知道的,加我QQ 55754344
0.jpg
 
下面是服务器端的代码,用客户端连接服务器端,发送数据A时,发生错误!

unit U_Server;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
ServerSocket1: TServerSocket;
StatusBar1: TStatusBar;
procedure Button4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
Application.Terminate;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Enabled:=False;
Button2.Enabled:=True;
Button3.Enabled:=True;
ServerSocket1.Port:=8098;
ServerSocket1.Active:=True;
StatusBar1.SimpleText:='服务器已成功启动!'
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Button1.Enabled:=True;
Button2.Enabled:=False;
ServerSocket1.Active:=False;
StatusBar1.SimpleText:='服务器已成功停止!'
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
ss:string;
begin
sfengbao:=ServerSocket1.Socket.ReceiveText;
if pos('A',ss)<>0 then
begin
ServerSocket1.Socket.SendText('22');
end;
end;

end.

下面是错误的图片,知道的,加我QQ 55754344
0.jpg
 
有什么问题吗?没有发现;
唯一的问题可能是窗口退出时要记得关闭SOCKET;
if ServerSocket1.Active=True then
ServerSocket1.Close;
另外,检测连接没有可写:
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Inc(LinkCount);
ShowMessage('新建连接');
end;
 
下面是错误的图片,知道的,加我QQ 55754344
0.jpg
 
上面是错误的图片,知道的,加我QQ 55754344,我可以把原程序,发过去~!
 
ServerSocket1.Socket.SendText('22');
改为
Socket.SendText('22');
 
谢谢,结帐!
 
谢谢,结帐!同时接受答案!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
751
import
I
后退
顶部