我正在为客户端和服务器使用 tidudpserver。下面是给出的服务器和客户端代码。
unit udp_server;
procedure TForm4.serverUDPRead(AThread: TIdUDPListenerThread;
AData: array of Byte; ABinding: TIdSocketHandle);
var
buffer:tbytes;
buffer2:tbytes;
i:integer;
ex_port:word;
i_ip,ex_ip:string;
t: string;
begin
ex_ip:=abinding.PeerIP; //public_ip
ex_port:=abinding.PeerPort; //public_port
//byte to string
setlength(i_ip,length(adata) div 2);
// extract private ip from array of byte
move(Adata[0],i_ip[1],length(adata));
// client profile {object of tclient }
user:=tclient.create(ex_ip,ex_port,i_ip,0,'');
//copying public and private endpoints to tclient object
setlength(buffer,sizeof(user));
move(user,buffer[0],sizeof(user)); // copying object to byte array
server.Sendbuffer(abinding.PeerIP,abinding.PeerPort,buffer);
// send client info to all connected clients(is this correct way of sending buffer(byte array)?)
end;
这是给定的客户端代码。我无法读取服务器在onudpread
程序中发送的数据。如何将字节数组(包含字符串和单词类型)转换为可读文本?
unit udp_client;
procedure TForm4.server2UDPRead(AThread: TIdUDPListenerThread;
AData: array of Byte; ABinding: TIdSocketHandle);
var
dest:string;
user2:tclient;// i had a tclient class in a separate file
buffer:tbytes;
begin
setlength(dest,length(adata));
move(Adata[0],dest[1],length(adata)); // problem is over here ,is this correct?
end;
从客户端发送的私有 ip
ip:=getipaddress; //call private ip function taken from (http://delphi.about.com/od/networking/l/aa103100a.htm)
setlength(i_ip,length(ip)*2);
move(ip[1],i_ip[0],length(i_ip));
server2.Sendbuffer('host',port,i_ip);
我想做 udp 打孔,因为客户端需要将他们的信息(公共/私有 ip/端口)发送到服务器,然后服务器将交换客户端信息,然后他们应该能够聊天。我很困惑,因为 adata 数组正在接收所有数据ip和短信会互相混合吗?目前我收到垃圾值,就像中文一样。:-/