美文网首页
UDP Socket 编程

UDP Socket 编程

作者: louyang | 来源:发表于2017-11-23 10:55 被阅读56次

    UDP 协议是如此的简单,我们可以想象一下,下面是 UDP 包头,前面再加一个 IP 的包头,后面接着 UDP 的报文内容,这样一个简单 的UDP/IP 报文就形成了。

    源自 https://en.wikipedia.org/wiki/User_Datagram_Protocol

    UDP 是一个无连接状态的协议,每一个 UDP 报文都独立发出。理解了这点,我们再来看 UDP socket API 接口,就容易理解了。

    UDP socket 编程有几乎固定的格式和步骤,我们先归纳一下。编程的细节我们可以看之后的例子。

    服务器端:
    1 声明一个 struct sockaddr_in 型变量,用于存储监听地址和端口。
    2 声明一个 int 型变量,用于存储 UDP socket。
    3 使用 bind() API,把地址和 socket 绑定在一起。
    4 使用 recvfrom() 或 recvmsg() 接收 UDP 报文,得到客户端 IP 地址和端口。
    5 使用 sendto() 或 sendmsg() 给客户端发送回应。
    
    客户端:
    1 声明一个 struct sockaddr_in 型变量,用于存储服务器地址和端口。
    2 声明一个 int 型变量,存储 UDP socket。
    3 使用 sendto() 或 sendmsg() 给服务器端发送 UDP 报文。
    4 使用 recvfrom() 或 recvmsg() 接收服务器端回应。
    

    客户端比服务器端少了一个 bind() 的步骤,这个也容易理解,客户端的发送网口和端口由操作系统自动分配,不需要程序员指定。

    还有一点需要说明,虽然 sendto() 和 recvfrom() 的格式比较接近,而且 sendmsg() 和 recvmsg() 的格式比较接近,但是它们不是必须成对出现的。例如使用 sendto() 发送,并使用 recvmsg() 接收,是完全可以的。两者之间的区别是,sendmsg() 可以把要发送的内容存放在多块,相互不连续的内存中;而 sendto() 需要一块连续的内存。

    例子 1

    客户端接收用户输入的一串字符,并发送给服务器端。
    服务器端接收后,颠倒整串字符顺序,并将其发回客户端。
    这个例子中使用 sendto / recvfrom 接口。

    server.c :

    /*
     *  Simple udp server
     */
    #include <stdio.h>  //printf,perror
    #include <string.h> //memset,strlen
    #include <stdlib.h> //exit
    #include <unistd.h> //close
    #include <arpa/inet.h>
    #include <sys/socket.h>
    
    #define BUFLEN 512  //Max length of buffer
    #define PORT 8888   //The port on which to listen for incoming data
    
    void die(char *s)
    {
        perror(s);
        exit(1);
    }
    
    char * reverse_string(char * s)
    {
        if (s != NULL)
        {
            char temp;
            for (int i=0, len=strlen(s), k=len-1; i < (len/2); i++,k--)
            {
                temp = s[i];
                s[i] = s[k];
                s[k] = temp;
            }
        }
        return s;
    }
    
    int main(void)
    {
        struct sockaddr_in si_me, si_other;
        int s, i, slen=sizeof(si_me), recv_len;
        char buf[BUFLEN];
    
        //build my internet address
        memset((char *)&si_me, 0, slen);
        si_me.sin_family = AF_INET;
        si_me.sin_port = htons(PORT);
        si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    
        //create a UDP socket
        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
        {
            die("socket");
        }
    
        //bind socket to port
        if (bind(s, (struct sockaddr*)&si_me, slen) == -1)
        {
            die("bind");
        }
    
        //keep listening for data
        while(1)
        {
            memset(buf, 0, BUFLEN);
            //try to receive some data, this is a blocking call
            if ((recv_len=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)) == -1)
            {
                die("recvfrom()");
            }
    
            //print details of the client/peer and the data received
            printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
            printf("Data: %s\n" , buf);
    
            //now reply the client with the reversed data
            if (sendto(s, reverse_string(buf), recv_len, 0, (struct sockaddr*)&si_other, slen) == -1)
            {
                die("sendto()");
            }
        }
    
        close(s);
        return 0;
    }
    

    client.c :

    /*
     *  Simple udp client
     */
    
    #include <stdio.h>  //printf,fgets
    #include <string.h> //memset,strlen
    #include <stdlib.h> //exit
    #include <unistd.h> //close
    #include <arpa/inet.h>
    #include <sys/socket.h>
    
    #define SERVER "127.0.0.1"
    #define BUFLEN 512  //Max length of buffer
    #define PORT 8888   //The port on which to send data
    
    void die(char *s)
    {
        perror(s);
        exit(1);
    }
    
    int main(void)
    {
        struct sockaddr_in si_other;
        int s, i, slen=sizeof(si_other), len;
        char buf[BUFLEN];
        char message[BUFLEN];
    
        //build server internet address
        memset((char *) &si_other, 0, sizeof(si_other));
        si_other.sin_family = AF_INET;
        si_other.sin_port = htons(PORT);
        if (inet_aton(SERVER, &si_other.sin_addr) == 0)
        {
            die("inet_aton");
        }
    
        //create a UDP socket
        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
        {
            die("socket");
        }
    
        while(1)
        {
            printf("Enter message : ");
            if (fgets(message, BUFLEN-1, stdin) == NULL)
            {
                die("gets");
            }
            len = strlen(message);
            if (len > 0 && message[len-1] == '\n')
            {
                message[len-1] = '\0';
            }
    
            //send the message
            if (sendto(s, message, strlen(message), 0, (struct sockaddr *)&si_other, slen) == -1)
            {
                die("sendto()");
            }
    
            //receive a reply and print it
            //clear the buffer by filling null, it might have previously received data
            memset(buf,'\0', BUFLEN);
            //try to receive some data, this is a blocking call
            if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen) == -1)
            {
                die("recvfrom()");
            }
    
            printf("Receive: %s\n", buf);
        }
    
        close(s);
        return 0;
    }
    
    # gcc server.c -o server && ./server
    Received packet from 127.0.0.1:56190
    Data: abcdef
    
    # gcc client.c -o client && ./client
    Enter message : abcdef
    Receive: fedcba
    
    例子 2

    要实现的功能,和编译运行方法,与例子1完全一样;但是使用了 sendmsg() 和 recvmsg() 接口。

    server.c

    /*
     *  Simple udp server
     */
    #include <stdio.h>  //printf, perror
    #include <string.h> //memset, strlen
    #include <stdlib.h> //exit
    #include <unistd.h> //close
    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <sys/uio.h> //recvmsg
    
    #define BUFLEN 512  //Max length of buffer
    #define PORT 8888   //THe port on which to listen for incoming data
    
    void die(char * s)
    {
        perror(s);
        exit(1);
    }
    
    char * reverse_string(char * s)
    {
        if (s != NULL)
        {
            char temp;
            for (int i=0, len=strlen(s), k=len-1; i < (len/2); i++,k--)
            {
                temp = s[i];
                s[i] = s[k];
                s[k] = temp;
            }
        }
        return s;
    }
    
    int main(void)
    {
        struct sockaddr_in si_me, si_other;
        int s, i, slen=sizeof(si_me), recv_len;
        char buf[BUFLEN];
        struct msghdr msgrecv;
        struct iovec iovrecv[1];
    
        //build my internet address
        memset((char *)&si_me, 0, slen);
        si_me.sin_family = AF_INET;
        si_me.sin_port = htons(PORT);
        si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    
        //create a UDP socket
        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
        {
            die("socket");
        }
    
        //bind socket to port
        if (bind(s, (struct sockaddr*)&si_me, slen) == -1)
        {
            die("bind");
        }
    
        memset(&msgrecv, 0, sizeof(msgrecv));
        memset(iovrecv, 0, sizeof(iovrecv));
        msgrecv.msg_name = &si_other;
        msgrecv.msg_namelen = sizeof(si_other);;
        msgrecv.msg_iov = iovrecv;
        msgrecv.msg_iovlen = 1;
        iovrecv[0].iov_base = buf;
        iovrecv[0].iov_len = sizeof(buf);
        while(1)
        {
            memset(buf, 0, BUFLEN);
            //try to receive some data, this is a blocking call
            if ((recv_len=recvmsg(s, &msgrecv, 0)) < 0)
            {
                die("recvmsg");
            }
    
            //print details of the client/peer and the data received
            printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
            printf("Data: %s\n" , buf);
    
            //now reply the client with the reversed data
            reverse_string(buf);
            if (sendmsg(s, &msgrecv, 0) == -1)
            {
                die("sendmsg()");
            }
    
        }
    
        close(s);
        return 0;
    }
    

    client.c

    /*
     *   Simple udp client
     */
    #include <stdio.h>  //printf, perror
    #include <string.h> //memset, strlen
    #include <stdlib.h> //exit
    #include <unistd.h> //close
    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <sys/uio.h> //recvmsg
    
    #define SERVER "127.0.0.1"
    #define BUFLEN 512  //Max length of buffer
    #define PORT 8888   //THe port on which to listen for incoming data
    
    void die(char *s)
    {
        perror(s);
        exit(1);
    }
    
    
    int main(void)
    {
        struct sockaddr_in si_other;
        int s, i, slen=sizeof(si_other), len;
        char buf[BUFLEN];
        struct msghdr msgsend;
        struct iovec iovsend[1];
    
        //build server internet address
        memset((char *) &si_other, 0, sizeof(si_other));
        si_other.sin_family = AF_INET;
        si_other.sin_port = htons(PORT);
        if (inet_aton(SERVER, &si_other.sin_addr) == 0)
        {
            die("inet_aton");
        }
    
        //create a UDP socket
        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
        {
            die("socket");
        }
    
        memset(&msgsend, 0, sizeof(msgsend));
        memset(iovsend, 0, sizeof(iovsend));
        msgsend.msg_name = &si_other;
        msgsend.msg_namelen = sizeof(si_other);;
        msgsend.msg_iov = iovsend;
        msgsend.msg_iovlen = 1;
        iovsend[0].iov_base = buf;
        iovsend[0].iov_len = 0;
    
        while(1)
        {
            printf("Enter message : ");
            memset (buf, 0, BUFLEN);
            if (fgets(buf, BUFLEN-1, stdin) == NULL)
            {
                die("gets");
            }
            len = strlen(buf);
            if (len > 0 && buf[len-1] == '\n')
            {
                buf[len-1] = '\0';
                iovsend[0].iov_len = len-1;
            }
            else {
                iovsend[0].iov_len = len;
            }
    
            //send the message
            if (sendmsg(s, &msgsend, 0) == -1)
            {
                die("sendmsg()");
            }
    
            //receive a reply and print it
            //clear the buffer by filling null, it might have previously received data
            memset(buf,'\0', BUFLEN);
            //try to receive some data, this is a blocking call
            if (recvmsg(s, &msgsend, 0) == -1)
            {
                die("recvmsg()");
            }
    
            printf("Receive: %s\n", buf);
        }
    
        close(s);
        return 0;
    }
    

    参考

    http://www.binarytides.com/programming-udp-sockets-c-linux/

    相关文章

      网友评论

          本文标题:UDP Socket 编程

          本文链接:https://www.haomeiwen.com/subject/tamavxtx.html