socket的recervetext的值为什么不能赋给一个string型数组?(50分)

  • 主题发起人 主题发起人 natha
  • 开始时间 开始时间
N

natha

Unregistered / Unconfirmed
GUEST, unregistred user!
我用clientsocket.sendtext向serversocket发一个字符串,将serversocket.receivetext
赋值给一个string型数组t,但是发现serversocket.receive接收到了字符串,但t
仍为空,请高手指教如何解决?
 
把赋值的代码贴出来看看
 
i:=1;
t:=serversocket.receivetext;
......
receivetext的内容为'CS0003',但t[1]内容为空。
 
你弄个中间变量试试?
i:=1;
s:=serversocket.receivetext;
t:=s;

 
s:=serversocket.receivetext;这个应该是
s:=serversocket.socket.recvivetext吧?
 
谢谢hua8hua指正,我是从上面copy下来的,看都没看。
 

不要用serversocket.socket来做接收、发送工作,
这仅可以作为管理属下的真正的和client相连的sock的工作。

应该在 OnClientRead 中用传入的 Socket 参数来接收
 
onedolph,你说的方法我试了,可还是不行啊.
 
Pipi,你能说得再清楚一点吗?最好是能有例子.
谢谢!!!
 
就是在onclientread事件中用t:=socket.receivetext。
 
yzlee,我正是像你那样做的,可是不行。
 
弄个判断,当s<>''时,才赋值。
 
5分钟写的程序,可别见笑:delphi 5 下编译通过,windows 2000下运行无问题。
客户端:
unit UClient;

interface

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

type
TForm1 = class(TForm)
ClientSocket: TClientSocket;
sendtxt: TEdit;
Label1: TLabel;
sendButton: TButton;
MainMenu1: TMainMenu;
clientmenu: TMenuItem;
connect1: TMenuItem;
disconnect1: TMenuItem;
exit1: TMenuItem;
procedure exit1Click(Sender: TObject);
procedure connect1Click(Sender: TObject);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure sendButtonClick(Sender: TObject);
procedure disconnect1Click(Sender: TObject);
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.exit1Click(Sender: TObject);
begin
Application.Terminate;
end;

procedure TForm1.connect1Click(Sender: TObject);
begin
if ClientSocket.Active then
ClientSocket.Close;
ClientSocket.Address:='204.111.111.53';
ClientSocket.Port:=5678;
ClientSocket.Open;
end;

procedure TForm1.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Label1.Caption:='connect to :'+Socket.RemoteAddress;
end;

procedure TForm1.sendButtonClick(Sender: TObject);
var
sendstr:string;
begin
if not ClientSocket.Active then
ShowMessage('no connect');

sendstr:=sendtxt.Text;
ClientSocket.Socket.SendText(sendstr);
end;

procedure TForm1.disconnect1Click(Sender: TObject);
begin
if ClientSocket.Active then
ClientSocket.Close;
end;

procedure TForm1.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Label1.Caption:='disconnect to :'+Socket.RemoteAddress;
end;

end.
------------------------------------------------
服务端:
unit UServer;

interface

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

type
TForm2 = class(TForm)
MainMenu1: TMainMenu;
ServerSocket: TServerSocket;
system1: TMenuItem;
start1: TMenuItem;
stop1: TMenuItem;
exit1: TMenuItem;
Label1: TLabel;
procedure exit1Click(Sender: TObject);
procedure start1Click(Sender: TObject);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.exit1Click(Sender: TObject);
begin
if ServerSocket.Active then
ServerSocket.Close;
Application.Terminate;
end;

procedure TForm2.start1Click(Sender: TObject);
begin
if ServerSocket.Active then
ServerSocket.Close;
ServerSocket.Port:=5678;
ServerSocket.Open;
end;

procedure TForm2.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
strarray:array[0..4] of string;
icount :integer;
begin
icount:=0;
strarray[icount]:=Socket.ReceiveText;
Label1.Caption:=strarray[icount];
end;

end.
 
接受答案了.
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
899
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部