Delphi中TServerSocket和TClientSocket通信,流或二进制的发送与接收问题 本帖最后由 changpingcao 于 2013-01-29 20:05:40 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用Move或CopyMemory将buf中的内容放到string中 SetLength(str, Size);copymemory(@str[1], @src, size) 能说下具体用法吗?我CopyMemory(pchar,buff,length(buff));str:=strPas(pchar);这样不行 客户端发送: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; ph:PChar; readstr:string;begin iLength:= Socket.ReceiveLength; while iLength>0 do begin SetLength(buff, iLength); iReceived := Socket.ReceiveBuf(buff[0], iLength); iLength := iLength - iReceived; end; ph:= StrAlloc(length(buff)); CopyMemory(ph,buff,length(buff)); readstr:=strPas(ph); msgList.Lines.Add(readstr); StrDispose(ph); buff:= nil;end;服务器接收到的都乱码了,字母、数字也乱了 ,大师们指导下吧 感谢 这玩意要看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; 求助啊,为什么我用什么方式接收,TServerSocket显示出来的都是一个字母h啊 非常奇怪,我也是用这种方式发送的, 但是到了客户端,收到的数据就变了. 比如,我发的是'A5 A5',到客户端就变成'D4 A3',再重发一次,又变成'60 A5'了!我的代码:var pStr: string; s : String; i : Integer; buff: array of byte; str: string; ph: PChar;begin str := Trim(edtSendCommand.Text); str := HexStrToStr(str); //把十六进制字符串转成字符串 'A5 A5'--> ASC值为165的两个字符 SetLength(buff, Length(str)+1); ph:= PChar(str); CopyMemory(buff,ph,Length(str)+1); if b_Client and edtListSelected.Checked[i] then ClientSocket.Socket.SendBuf(buff,length(buff)) else ServerSocket.Socket.Connections[i].SendBuf(buff,Length(buff));end;然后在客户端方法:var iLength: Integer; len : word; buf : array of byte; msg, newmsg : string;begin try iLength:= Socket.ReceiveLength; fillchar(buf, sizeof(buf),' '); len := socket.ReceiveBuf(buf,iLength); msg := strpas(@buf); newmsg := COPY(trim(msg),1,len); msg := strToHexStr(newmsg); if len>0 then memResult.Lines.Add(socket.RemoteAddress+' Received:'+msg); except showmessage('transfer error!'); end;end;有没有人碰到过同样情况或知道是什么原因的? API关闭2003远程主机的问题。 求cxgrid中职员信息的demo 怎样获取firefox正在访问的URL? actionmanager设计菜单的问题 如何快速作到客户端查询服务器数据? 请问一个Tcanvas划线的问题 新发布的共享软件,反正不是为了赚钱做广告。。。。。。 关于数据模块的问题! 急,急,急,高分相送 如何用enter代替tab键!!!! 定义EXCEL里面单元格的格式 delphi2010如何获取硬盘物理序列号? PageControl 清除tabsheet
能说下具体用法吗?我CopyMemory(pchar,buff,length(buff));
str:=strPas(pchar);这样不行
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;
ph:PChar;
readstr:string;
begin
iLength:= Socket.ReceiveLength;
while iLength>0 do
begin
SetLength(buff, iLength);
iReceived := Socket.ReceiveBuf(buff[0], iLength);
iLength := iLength - iReceived;
end;
ph:= StrAlloc(length(buff));
CopyMemory(ph,buff,length(buff));
readstr:=strPas(ph);
msgList.Lines.Add(readstr);
StrDispose(ph); buff:= nil;
end;服务器接收到的都乱码了,字母、数字也乱了 ,大师们指导下吧 感谢
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;
我的代码:
var
pStr: string;
s : String;
i : Integer;
buff: array of byte;
str: string;
ph: PChar;
begin
str := Trim(edtSendCommand.Text);
str := HexStrToStr(str); //把十六进制字符串转成字符串 'A5 A5'--> ASC值为165的两个字符
SetLength(buff, Length(str)+1);
ph:= PChar(str);
CopyMemory(buff,ph,Length(str)+1); if b_Client and edtListSelected.Checked[i] then
ClientSocket.Socket.SendBuf(buff,length(buff))
else
ServerSocket.Socket.Connections[i].SendBuf(buff,Length(buff));
end;然后在客户端方法:
var
iLength: Integer;
len : word;
buf : array of byte;
msg, newmsg : string;
begin
try
iLength:= Socket.ReceiveLength;
fillchar(buf, sizeof(buf),' ');
len := socket.ReceiveBuf(buf,iLength);
msg := strpas(@buf);
newmsg := COPY(trim(msg),1,len);
msg := strToHexStr(newmsg); if len>0 then
memResult.Lines.Add(socket.RemoteAddress+' Received:'+msg);
except
showmessage('transfer error!');
end;
end;
有没有人碰到过同样情况或知道是什么原因的?