UDP Socket 编程

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/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容