傳送結構體:
tmydata = record
id:integer;
name:array[0..20] of char;
***:array[0..10] of char;
age:byte;
address:array[0..256] of char;
updatetime:double;
end;
//傳送結構體:
procedure tform2.button2click(sender: tobject);
varsenddata:tmydata;
begin
senddata.id:=10;
strpcopy(senddata.name,'wyatt');
strpcopy(senddata.***,'男');
senddata.age:=25;
strpcopy(senddata.address,'江蘇省');
senddata.updatetime:=now;
idtcpclient1.iohandler.write(#99);//接收時便於區分接收的資料型別 自定義
idtcpclient1.iohandler.write(rawtobytes(senddata,sizeof(senddata)));
end;
//傳送tstrings型別
procedure tform2.button3click(sender: tobject);
varslist:tstrings;
i:integer;
begin
slist := tstringlist.create;
for i :=0 to 30 do
begin
slist.add('資料index' + inttostr(i));
end;
idtcpclient1.iohandler.write(#111);//接收時便於區分接收的資料型別 自定義
idtcpclient1.iohandler.write(slist.count);
idtcpclient1.iohandler.write(tobytes(slist.text,tidtextencoding.utf8));
end;
//傳送一行字串資料
procedure tform2.button4click(sender: tobject);
begin
idtcpclient1.iohandler.write(#12);//接收時便於區分接收的資料型別 自定義
idtcpclient1.iohandler.write('文星',tidtextencoding.utf8);//中文要指定編碼,接收時也要進行相應的轉換,否則中文會顯示成?號
end;
接收:procedure tform1.idtcpserver1execute(acontext: tidcontext);
varreaddata:tmydata;
buf:tidbytes;
scmd:char;
slist:tstrings;
i:integer;
listcount:integer;
begin
scmd := acontext.connection.iohandler.readchar;
if scmd = #99 then //接收結構體
begin
acontext.connection.iohandler.readbytes(buf,sizeof(readdata));
bytestoraw(buf, readdata, sizeof(readdata));
with memo1.lines do
begin
add('id:'+inttostr(readdata.id));
add('name:'+strpas(readdata.name));
add('***:'+readdata.***);
add('age:'+inttostr(readdata.age));
add('updatetime:'+datetimetostr(readdata.updatetime));
end;
endelse if scmd = #111 then //接收 tstrings
begin
listcount := acontext.connection.iohandler.readlongint;
slist := tstringlist.create;
tryacontext.connection.iohandler.readstrings(slist,listcount,tidtextencoding.utf8);
for i :=0 to slist.count-1 do
begin
memo1.lines.add(slist.strings[i]);
end;
finally
slist.free;
end;
endelse if scmd = #12 then
begin
memo1.lines.add(acontext.connection.iohandler.readstring(acontext.connection.iohandler.inputbuffer.size,tidtextencoding.utf8)
endelse
acontext.connection.iohandler.inputbuffer.clear;//清除不能識別的命令
);end;
indy 10 5 7的資料傳送接收的用法
tmydata record id integer name array 0.20 ofchar array 0.10 ofchar age byte address array 0.256 ofchar updatetime double end 傳送結構體 procedure tform2.bu...
INDY流的傳送和接收
傳送 trys hello world stream tstringstream.create s idtcpclient1.openwritebuffer idtcpclient1.writeinteger stream.size 注意這裡 要先寫入流的長度,在讀取的時候如果使用 athread....
socket資料的接收和傳送
linux 不區分套接字檔案和普通檔案,使用 write 可以向套接字中寫入資料,使用 read 可以從套接字中讀取資料。前面我們說過,兩台計算機之間的通訊相當於兩個套接字之間的通訊,在伺服器端用 write 向套接字寫入資料,客戶端就能收到,然後再使用 read 從套接字中讀取出來,就完成了一次通...