Delphi UPD通訊演示

2021-05-28 04:34:37 字數 3314 閱讀 1772

服務端:

1.  定義結構

type

tmyrecord = record

ip: array[0..16] of char;

port: integer;

data: array[0..1023] of char;

end;

2. 窗體建立的時候設定ip,埠

procedure tfrmserver.formcreate(sender: tobject);

begin

idpsrvr1.bindings.add;

idpsrvr1.bindings[0].ip := getlocalip;

idpsrvr1.bindings[0].port := 8098;

idpsrvr1.active := true;

caption := format('server: [%s:%d]', [idpsrvr1.bindings[0].ip,

idpsrvr1.bindings[0].port]);

mmo1.doublebuffered := true;

end;

3. 設定tidudpserver的onread事件。

procedure tfrmserver.idpsrvr1udpread(sender: tobject; adata: tstream;

abinding: tidsockethandle);

varreceivedata: tmyrecord;

senddata: tmyrecord;

begin

zeromemory(@receivedata, sizeof(receivedata));

zeromemory(@senddata, sizeof(senddata));

adata.read(receivedata, sizeof(receivedata));

strplcopy(senddata.ip, idpsrvr1.bindings[0].ip, length(senddata.ip));

senddata.port := idpsrvr1.bindings[0].port;

strlcopy(senddata.data, receivedata.data, length(senddata.data));

adddata(format('ip: %s port: %d  data: %s',

[receivedata.ip, receivedata.port, receivedata.data]));

idpsrvr1.sendbuffer(receivedata.ip, receivedata.port, receivedata,

sizeof(receivedata));

end;

獲取本機ip位址函式

function getlocalip: string; //獲取本機區域網ip

type

tapinaddr = array[0..10] of pinaddr;

papinaddr = ^tapinaddr;

varphe: phostent;

pptr: papinaddr;

buffer: array[0..63] of char;

i: integer;

ginitdata: twsadata;

begin

result := '';

wsastartup($101, ginitdata);

trygethostname(buffer, sizeof(buffer));

phe := gethostbyname(buffer);

if nil <> phe then

begin

pptr := papinaddr(phe^.h_addr_list);

i := 0;

while nil <> pptr^[i] do

begin

result := strpas(inet_ntoa(pptr^[i]^));

inc(i);

end;

end;

finally

wsacleanup;

end;

end;

客戶端:

1. 繫結ip

procedure tfrmclient.btnipclick(sender: tobject);

begin

idpsrvr1.bindings.add;

idpsrvr1.bindings[0].ip := getlocalip;

idpsrvr1.bindings[0].port := strtoint(cbb1.text);

idpsrvr1.active := true;

showmessage('ok');

end;

2. 傳送資料

procedure tfrmclient.btnsendclick(sender: tobject);

varmyrecord: tmyrecord;

datastr: string;

begin

//自己的ip位址

strplcopy(myrecord.ip, idpsrvr1.bindings[0].ip, length(myrecord.ip));

//自己的埠

myrecord.port := idpsrvr1.bindings[0].port;

datastr := 'client: ' + edtmsg.text;

strplcopy(myrecord.data, datastr, length(datastr));

idpsrvr1.sendbuffer(fserverip, fserverport, myrecord, sizeof(myrecord));

tmr1.enabled := true;

end;

3. 讀取資料

procedure tfrmclient.idpsrvr1udpread(sender: tobject; adata: tstream;

abinding: tidsockethandle);

varsenddata: tmyrecord;

begin

zeromemory(@senddata, sizeof(senddata));

adata.read(senddata, sizeof(senddata));

adddata(format('ip: %s port:%d  data: %s',

[senddata.ip, senddata.port, senddata.data]));

end;

Linux通訊管道演示

無名管道特性 只能用於有親緣關係的程序 父子程序 兄弟程序 資料之間單向流動 半雙工 管道中資料不儲存,資料被讀走後便丟失 管道並不屬於任何檔案系統,只存在於記憶體當中 原函式 include int pipe int pipefd 2 當使用pipe 函式時需建立包含兩個檔案描述符的陣列 父子程序...

firefly伺服器間通訊演示

命令列下輸入firefly admin.py createproject test distributed linux在終端輸入 firefly會在該目錄中建立乙個名為test distributed的工程 2.匯入工程 將工程匯入到eclipse中 3.配置引數 配置config.json中的相應...

Linux訊息佇列通訊及相關API演示

訊息佇列存在於linux核心中,可以使資料雙向流動 資料在核心中,即使程序結束資料依然存在 訊息佇列實際上是訊息鍊錶,每個佇列都有自己的識別符號 msgget 建立乙個訊息佇列 1.原函式 include include include int msgget key t key,int msg 2....