Delphi中TServerSocket和TClientSocket通信,流或二进制的发送与接收问题
我目前的做法不知道对不对,大家看看:
客户端发送消息:
procedure Tfrm_SocketClient.btn_SendDataClick(Sender: TObject);
var
buff: array of byte;
str: string;
ph: PChar;
begin
str:= mem_SendData.Text;//文本框的信息
SetLength(buff, Length(str)+1);
ph:= PChar(str);
CopyMemory(buff,ph,Length(str)+1);
clntsckt1.Socket.SendBuf(buff, length(buff));
mem_SendData.Clear;
end;
服务器接收:
var
iLength: Integer;
iReceived: Integer;
buff: array of byte;
showstr:string;
begin
iLength:= Socket.ReceiveLength;
while iLength>0 do
begin
SetLength(buff, iLength);
iReceived := Socket.ReceiveBuf(buff[0], iLength);
iLength := iLength - iReceived;
msgList.Lines.Add(IntToStr(iReceived));
end;
我不知道这样做对不对,我服务器接收后不知道怎么再转化为string,请指导!
如果我的做法不对,那么麻烦给个新的方式!
希望能贴出实际代码,刚接触这个,说理论我怕无法理解,非常感谢!
总之我需要做的就是客户端发送流或者二进制到服务器 ,然后服务器接收这样子的一个功能!!!