首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

Delphi中TServerSocket和TClientSocket通信,流或二进制的发送与接收有关问题

2013-02-24 
Delphi中TServerSocket和TClientSocket通信,流或二进制的发送与接收问题本帖最后由 changpingcao 于 2013-

Delphi中TServerSocket和TClientSocket通信,流或二进制的发送与接收问题
本帖最后由 changpingcao 于 2013-01-29 20:05:40 编辑 我目前的做法不知道对不对,大家看看:

客户端发送消息:
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,请指导!

如果我的做法不对,那么麻烦给个新的方式!
希望能贴出实际代码,刚接触这个,说理论我怕无法理解,非常感谢!

总之我需要做的就是客户端发送流或者二进制到服务器 ,然后服务器接收这样子的一个功能!!! delphi tserversocket tclientsocket tcp 通信
[解决办法]
SetLength(str, Size);

copymemory(@str[1], @src, size)
[解决办法]
这玩意要看Delphi的版本的,分界线是D2009。估计你是在D2009及以上的版本中编辑的吧
[解决办法]
客户端发送:
procedure Tfrm_SocketClient.btn_SendDataClick(Sender: TObject);
var
  str: string;
begin
  str:= mem_SendData.Text;
  clntsckt1.Socket.SendBuf(str[1],Length(str));
  mem_SendData.Clear;
end;


服务器接收:
var
  iLength: Integer;
  readstr:string;
begin
   iLength:= Socket.ReceiveLength;
   SetLength(readstr,iLength);
   Socket.ReceiveBuf(readstr[1],iLength);
   msgList.Lines.Add(readstr);
end;

热点排行