以廣播的方式列印日誌

2021-08-22 19:18:24 字數 2681 閱讀 3509

test.c用於測試以廣播的方式傳送列印資訊(客戶端)

#include 

#include

#include

#include

#include

#include

#include

#include

int broadcast_log(int port, char *log)

myport = port;

//初始化socket

sender_socket = socket(pf_inet, sock_dgram, ipproto_udp);

if(sender_socket <= 0)

memset(&broadcast_addr, 0, sizeof(broadcast_addr));

broadcast_addr.sin_family = af_inet;

broadcast_addr.sin_addr.s_addr = htonl(inaddr_broadcast);

broadcast_addr.sin_port = htons(myport);

printf("\033[32m[%s:%d]\033[36m s_addr = %d, sin_port = %d\033[0m\n", __function__, __line__, broadcast_addr.sin_addr.s_addr,

broadcast_addr.sin_port);

//設定socket可選項,因為預設生成的會阻止廣播,所以要將可選項中的so_broadcast標誌置為1

setsockopt(sender_socket, sol_socket, so_broadcast, (void *)&opt_so_broadcast, sizeof(opt_so_broadcast));

memset(buff, 0, sizeof(buff));

snprintf(buff, sizeof(buff), "%s", log);

//將資料傳送到多播組

ret = sendto(sender_socket, buff, strlen(buff), 0, (struct sockaddr *)&broadcast_addr, sizeof(broadcast_addr));

printf("\033[32m[%s:%d]\033[36m ret = %d\033[0m\n", __function__, __line__, ret);

close(sender_socket);

return0;}

void send_log(char *fmt, ...)

#define debug(fmt, arg...) send_log("==>[%s:%d]"fmt"\n", __function__, __line__, ##arg)

int main(int argc , char *argv)

recv.c接收廣播的資訊,輸出列印資訊(服務端)

#include

#include

#include

#include

#include

#include

int main(int argc , char *argv)

//初始化receiver_socket

receiver_socket = socket(af_inet,sock_dgram, ipproto_udp);

if(receiver_socket <= 0)

memset(&receiver_addr,0,sizeof(receiver_addr));

receiver_addr.sin_family = af_inet;

#if 0

if(!inet_aton("12.12.12.30", &receiver_addr.sin_addr))

#else

receiver_addr.sin_addr.s_addr = htonl(inaddr_any);

//receiver_addr.sin_addr.s_addr = htonl(inaddr_broadcast);

#endif

receiver_addr.sin_port = htons(atoi(argv[1]));

printf("\033[32m[%s:%d]\033[36m s_addr = %d, sin_port = %d\033[0m\n", __function__, __line__, receiver_addr.sin_addr.s_addr, receiver_addr.sin_port);

//繫結位址

if(bind(receiver_socket,(struct sockaddr *)&receiver_addr,sizeof(receiver_addr)) == -1)

setvbuf(stdout, null, _ionbf, 0);

while(1)

buff[sizeof(buff) - 1] = 0;

for(iindex = 0; iindex < str_len; iindex++)

//printf("\n");

}close(receiver_socket);

return

0;}

以符合人類閱讀的方式列印php陣列

在程式開發過程中 列印資料進行檢視除錯是非常頻繁的 如果沒有一種易於閱讀的樣式那是相當痛苦的 先定義乙個陣列 12 3456 789 array array t0 test0 t1 test1 t3 array tt0 test0 tt1 test1 tt3 test3 如果用php系統函式prin...

定向廣播的幾種方式

在android系統中sendbroadcast和broadcastreceiver。只要broadcastreceiver指定的action和sendbroadcast action一致就可以就行訊息接收。但是我們有這樣的需求即我傳送的廣播不允許所有應用都可以接收廣播訊息,而是要經過傳送者允許的才...

兩種常用傳送廣播的方式,以及廣播傳值

傳送廣播一般用於相隔多個頁面傳送通知或者傳值的時候用 如果不了解建議先去看一下對廣播的介紹 1.第一種傳送廣播的方式 寫在需要傳送廣播的地方 如果不傳值兩行 就可以搞定,很簡單 string str 需要傳的內容 註冊並傳送廣播 寫在傳送廣播的地方 意圖 往mainactivity中傳送廣播並傳值,...