H
h_o_u_l_i
Unregistered / Unconfirmed
GUEST, unregistred user!
我照书做了个SOCKET的程序,分为客户端和服务端,他们可以相互收发文本信息。
下面语句为客户端Tclientsocket的onread事件:
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
rechar;
sbuf:string;
relong:integer;
begin
re:=nil;
while socket.ReceiveLength>0 do
begin
re:=allocmem(socket.ReceiveLength);//问题1:去掉此句可否?
try
relong:=socket.ReceiveLength;
socket.ReceiveBuf(re^,socket.ReceiveLength);//问题2:re是pchar,已是指针为什么还加^,这是什么意思?不成了指针的指针了?
//为什么有时不加^也行?(测试成功)
sbuf:=strpas(re); //问题3:此句改为sbuf:=re;后测试不行,收到的信息结尾处总是有乱码?
setlength(sbuf,relong); //问题4:此句写上有什么意义?可是不写收到的信息结尾处总是有乱码?
totallen:=totallen+relong;
statusbar1.Panels.Items[1].Text:='总共接收:'+inttostr(totallen);
statusbar1.Update;
finally
freemem(re);
end;
memo1.Lines.Add(sbuf);
end;
end;
您可以会几个就帮忙回答几个先谢......谢.......了!
下面语句为客户端Tclientsocket的onread事件:
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
rechar;
sbuf:string;
relong:integer;
begin
re:=nil;
while socket.ReceiveLength>0 do
begin
re:=allocmem(socket.ReceiveLength);//问题1:去掉此句可否?
try
relong:=socket.ReceiveLength;
socket.ReceiveBuf(re^,socket.ReceiveLength);//问题2:re是pchar,已是指针为什么还加^,这是什么意思?不成了指针的指针了?
//为什么有时不加^也行?(测试成功)
sbuf:=strpas(re); //问题3:此句改为sbuf:=re;后测试不行,收到的信息结尾处总是有乱码?
setlength(sbuf,relong); //问题4:此句写上有什么意义?可是不写收到的信息结尾处总是有乱码?
totallen:=totallen+relong;
statusbar1.Panels.Items[1].Text:='总共接收:'+inttostr(totallen);
statusbar1.Update;
finally
freemem(re);
end;
memo1.Lines.Add(sbuf);
end;
end;
您可以会几个就帮忙回答几个先谢......谢.......了!