UDP network programming for Unix domain sockets

Source: Internet
Author: User

For UDP network programming of Unix domain sockets, the server side is as follows:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <sys/types.h>#include <sys/un.h>#define SA struct sockaddr#define PATHNAME "/tmp/unixudp"void err_sys(const char *errmsg);int main(void){    int sockfd;    struct sockaddr_un servaddr, cliaddr;    char buf[BUFSIZ];    ssize_t n;    socklen_t len = sizeof(cliaddr);    if ((sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)        err_sys("socket");    bzero(&servaddr, sizeof(servaddr));    unlink(PATHNAME);    servaddr.sun_family = AF_LOCAL;    strncpy(servaddr.sun_path, PATHNAME, sizeof(servaddr.sun_path) - 1);    if (bind(sockfd, (SA *)&servaddr, sizeof(servaddr)) == -1)        err_sys("bind");    for(;;){        n = recvfrom(sockfd, buf, sizeof(buf), 0, (SA *)&cliaddr, &len);        if (n == -1)            err_sys("recvfrom");        else            printf("%s\n", buf);        if (sendto(sockfd, buf, n, 0, (SA *)&cliaddr, sizeof(cliaddr)) != n)            err_sys("sendto");    }    exit(0);}void err_sys(const char *errmsg){    perror(errmsg);    exit(1);}

Client Program:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <sys/types.h>#include <sys/un.h>#define SA struct sockaddr#define PATHNAME "/tmp/unixudp"void err_sys(const char *errmsg);int main(void){    int sockfd;    struct sockaddr_un servaddr, localaddr;    char buf[BUFSIZ], s[] = "hello china";    ssize_t n;    if ((sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)        err_sys("socket");    bzero(&localaddr, sizeof(localaddr));    localaddr.sun_family = AF_LOCAL;    strncpy(localaddr.sun_path, tmpnam(NULL), sizeof(localaddr.sun_path) - 1);    if (bind(sockfd, (SA *)&localaddr, sizeof(localaddr)) == -1)        err_sys("bind");    bzero(&servaddr, sizeof(servaddr));    servaddr.sun_family = AF_LOCAL;    strncpy(servaddr.sun_path, PATHNAME, sizeof(servaddr.sun_path) - 1);    for(;;){        if (sendto(sockfd, s, strlen(s), 0, (SA *)&servaddr, sizeof(servaddr)) == -1)            err_sys("sendto");        if ((n = read(sockfd, buf, sizeof(buf))) == -1)            err_sys("read");        else             printf("%s\n", buf);        usleep((rand() % 10) * 10000);    }    exit(0);}void err_sys(const char *errmsg){    perror(errmsg);    exit(1);}


UDP network programming for Unix domain sockets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.