udp可靠傳輸的一種

https://blog.csdn.net/guxch/article/details/7041052

https://blog.csdn.net/sinat_20184565/article/details/79567057

https://blog.csdn.net/ace_fei/article/details/6412069

unix套接字可靠傳輸

只能客戶端到服務端,服務端無法sendto

接收端

recvfrom介紹

https://blog.csdn.net/mm7758521/article/details/1750170?locationNum=7&fps=1

本地進程間通訊方式的一種,unix可靠傳輸

接收端

rec.c

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>

#define LOCAL_KEYEVENT_PATH "/tmp/key_event.socket"

void  receivekeyevent(int eventdata)
{
    int fd;
    struct sockaddr_un un;
    int ret;
    int key;
    int addr_len;
    int on = 1;
    char buf[512] = {0};
    int len;

    if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
        perror("socket error");
        return ;
    }

    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) {
        perror("set socket error"); 
        return; 
    }
#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) {
        perror("bind error");
				exit(-1);
    }
		//must need here,other wize will cause receive error
    len = sizeof(un);
    while(1) {
        ret = recvfrom(fd, &key, sizeof(key), 0, (struct sockaddr*)&un, &len);

        if(ret < 0) {
            printf("ret %d len:%d\n", ret, len);
            perror("receive error");
            break;
        }
        printf("ret:%d key:%d len:%d\n", ret, key, len);
  
        
    }
    close(fd);
    printf("exit here\n");

}


int main(void)
{
    int fd;
    
    printf("send here~~~\n");
    while(1) {
     receivekeyevent(3);
     printf("slleep 5\n");
     sleep(5);
    }
    return 0;
}

發送端

send.c

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>

#define LOCAL_KEYEVENT_PATH "/tmp/key_event.socket"


void  sendkeyevent(int eventdata)
{
    int fd = -1;
    struct sockaddr_un un;
    int addr_len = -1;
    int ret;

    if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
        perror("socket error");
        return ;
    }

    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) {
        perror("sendkeyevent failed");
    }else{
        printf("sendkeyevent ok ret:%d\n", ret);
    }

    close(fd);
}


int main(void)
{
    int fd;
    int count = 0; 


    printf("send here~~~\n");


    while(1) {
        sendkeyevent(count++);
        sleep(1);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章