udp廣播示例
unix套接字可靠傳輸
只能客戶端到服務端,服務端無法sendto
接收端recvfrom介紹
本地程序間通訊方式的一種,unix可靠傳輸
接收端rec.c
#include #include /* see notes */
#include #include #include #include #include #define local_keyevent_path "/tmp/key_event.socket"
void receivekeyevent(int eventdata)
; int len;
if ((fd = socket(af_unix, sock_dgram, 0)) < 0)
memset(&un, 0, sizeof(un));
un.sun_family = af_unix;
strncpy(un.sun_path, local_keyevent_path, sizeof(local_keyevent_path));
//addr_len = sizeof(un.sun_family) + strlen(un.sun_path);
addr_len = sizeof(un);
printf("addr len is %d\n", addr_len);
printf("fd is %d\n", fd);
#if 0
if(setsockopt(fd, sol_socket, so_reuseaddr, &on, sizeof(int)) != 0)
#endif
//need to remove for can not bind
sprintf(buf, "rm %s -rf", local_keyevent_path);
system(buf);
printf("%s\n", buf);
printf("bind here~~~\n");
ret = bind(fd, (struct sockaddr *)&un, addr_len);
if(ret < 0)
//must need here,other wize will cause receive error
len = sizeof(un);
while(1)
printf("ret:%d key:%d len:%d\n", ret, key, len);
}close(fd);
printf("exit here\n");
}int main(void)
return 0;
}
傳送端
send.c
#include #include /* see notes */
#include #include #include #include #include #define local_keyevent_path "/tmp/key_event.socket"
void sendkeyevent(int eventdata)
printf("fd is %d\n", fd);
memset(&un, 0, sizeof(un));
un.sun_family = af_unix;
strncpy(un.sun_path, local_keyevent_path, sizeof(local_keyevent_path));
//addr_len = sizeof(un.sun_family) + strlen(un.sun_path);
addr_len = sizeof(struct sockaddr_un);
printf("len;%d\n", addr_len);
if ((ret = sendto(fd, &eventdata, sizeof(int), 0, (struct sockaddr *)&un, addr_len)) < 0) else
close(fd);
}int main(void)
return 0;
}
udp廣播互動
客戶端發現裝置
//udp 傳送廣播資訊
#include #include #include #include #include #include #include #include #include #define header_finder ""
void *localprint_udp_handle(void *arg)
#if 0
ret = setsockopt(sockfd, sol_socket, so_broadcast, (char*)&opt, sizeof(opt));
if(ret == -1)
#endif
ret = bind(sockfd,(struct sockaddr *)&(from), sizeof(struct sockaddr_in));
if(ret == -1)
while(1) else
}}int main(int argc, char *argv)
; int port;
int ret;
pthread_t pthread_udp;
if(argc == 2) else if(argc == 3) else
ret = pthread_create(&pthread_udp, null, localprint_udp_handle, &port);
if( port<1025 || port>65535 )//0~1024一般給系統使用,一共可以分配到65535
//1.建立udp socket
int udp_socket_fd = socket(af_inet, sock_dgram, 0);
if (udp_socket_fd == -1)
//2.開啟傳送廣播資料功能
int on = 1; //開啟
ret = setsockopt(udp_socket_fd, sol_socket, so_broadcast, &on, sizeof(on));
if(ret < 0)
//3.設定當前網段的廣播位址
struct sockaddr_in bro_addr = ;
bro_addr.sin_family = af_inet;
bro_addr.sin_port = htons(port);
bro_addr.sin_addr.s_addr = inet_addr(brocastaddr); //設定為廣播位址
char rec_buf[1024] = ;
char send_buf[1024] = ;//訊息緩衝區
int *size = (int*)&send_buf[2];
*size = strlen(header_finder);
strcpy(&send_buf[6], header_finder);
struct sockaddr_in addr_cli = ;
socklen_t len;
//4 傳送資料
while(1)
//5.關閉網路通訊
close(udp_socket_fd);
return 0;
}
UDP實現可靠傳輸
如何讓udp實現可靠傳輸 自定義通訊協議,在應用層定義一些可靠的協議,比如檢測包的順序,重複包等問題,如果沒有收到對方的ack,重新發包 udp沒有delievery garuantee,也沒有順序保證,所以如果你要求你的資料傳送與接受既要高效,又要保證有序,收包確認等,你就需要在udp協議上構建自...
UDP可靠傳輸那些事
有空來論壇走走,發現討論udp可靠傳輸又熱了起來,有人認為udp高效率,有人認為udp丟包重傳機制容易控制,還有朋友搞極限測試,當然也有人推銷自己的東西,這裡寫一點我個人的看法。udp可靠傳輸其實非常非常的簡單,我最開始接觸udp可靠傳輸大約是在2005年,因為那時候開發ftpanywhere,由於...
怎樣實現UDP可靠傳輸
相對於tcp,由於udp是面向無連線 不可靠的傳輸協議。如果我們要用udp去實現可靠的傳輸,則需要解決兩個問題 丟包和後發先至 包的順序 解決方法 1 給資料報編號,按照包的順序接收並儲存 2 接收端接收到資料報後傳送確認資訊給傳送端,傳送端接收確認資料以後再繼續傳送下乙個包,如果接收端收到的資料報...