跳至主要内容

【转】udp编程实例

UDP通讯实例

2008-04-29 15:30:05 / 个人分类:linux C编程

UDP协议的几点好处:
1.UDP不要求保持一个连接;
2.UDP没有因接收方认可收到数据包(或者当数据包没有正确抵达而自动重传)而带来的开销;
3.设计UDP的目的是用于短应用和控制信息;
4.在一个数据包接一个数据包的基础上,UDP要求的网络带宽比TCP更小。
UDP的几个缺点:
1.程序员必须创建代码检测传输错误并进行重传(如果应用程序要求这样做);
2.程序员必须把大数据包分片。

code:
<1>
/*
* sender.c--UDP protocol example
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int port = 6789;

int main()
{
    int socket_descrīptor;
    int iter = 0;
    char buf[80];
    struct sockaddr_in address;

    /* Initialize socket address structure for Interner Protocols */
    bzero(&address, sizeof(address)); // empty data structure
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(port);

    /* Create a UDP socket */
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);

    //Loop 20 times (a nice round number ) sending data
    for(iter = 0; iter <= 20; iter++)
    {
        sprintf(buf, "data packet with ID %d\n", iter);
        sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address));
    }   

    /* send a termination message */
    sprintf(buf, "stop\n");
    sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address)); //address is the target of the message send
   
    close (socket_descrīptor);
    printf("Message Sent, Terminating\n");
    return 0;
}

<2>
/*
* receiver.c--received of the UDP protocol
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int port = 6789;

int main()
{
    int sin_len;
    char message[256];
    int socket_descrīptor;
    struct sockaddr_in sin;
    printf("Waiting for data from sender\n");

    // Initialize socket address structure for Internet Protocols
    bzero(&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(port);
    sin_len = sizeof(sin);

    //Create a UDP socket and bind it to the port
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    bind(socket_descrīptor, (struct sockaddr *)&sin, sizeof(sin));
   
    //Loop forever (or until a termination message is received)
    // Received data through the socket and process it.The processing in this program is really simple --printing
    while(1)
    {
        recvfrom(socket_descrīptor, message, sizeof(message), 0, (struct sockaddr *)&sin, &sin_len);
        printf("Response from server : %s\n", message);
        if(strncmp(message, "stop", 4) == 0)
        {
            printf("sender has told me to end the connection\n");
            break;
        }
    }
    close(socket_descrīptor);
    return 0;
}

分别编译产生sender、receiver两个可执行文件。
然后在两个终端中分别一次运行,可以得到结果如下:
[hyj@localhost udp_]$ ./receiver
Waiting for data from sender
Response from server : data packet with ID 0

Response from server : data packet with ID 1

Response from server : data packet with ID 2

Response from server : data packet with ID 3

Response from server : data packet with ID 4

Response from server : data packet with ID 5

Response from server : data packet with ID 6

Response from server : data packet with ID 7

Response from server : data packet with ID 8

Response from server : data packet with ID 9

Response from server : data packet with ID 10

Response from server : data packet with ID 11

Response from server : data packet with ID 12

Response from server : data packet with ID 13

Response from server : data packet with ID 14

Response from server : data packet with ID 15

Response from server : data packet with ID 16

Response from server : data packet with ID 17

Response from server : data packet with ID 18

Response from server : data packet with ID 19

Response from server : data packet with ID 20

Response from server : stop

sender has told me to end the connection
发送端结果:
[hyj@localhost udp_]$ ./sender
Message Sent, Terminating


说明:
    sendto函数同send函数,该函数通过套接口发送一条消息。此时使用了无连接的套接口;消息发送的目的地址由address指定;
    recfrom函数同sendto。注意:对于TCP和UDP你都可以使用recvfrom函数,但是recv只能用于TCP;
    此外,如果在编译时提示找不到socket函数,那么在编译时是需要指定Internet库,在gcc命令中加入-lxnet选项,如:CC -o sender sender.c -lxnet

    bind函数给套接口分配一个地址;
    函数recvfrom等待来自于无连接套接口的一个数据包。它也在地址数据结构中返回发送方的地址。这个源地址在示例程序中没有用,但是更高级的程序能够用它打开一个返回发送方的套接口。如果你不关心这个地址,可以给参数传递NULL。

    以上代码未加入任何错误处理程序,加入最少错误检测后的代码如下:
/*
* receiver2.c--received of the UDP protocol,add the error check
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

int port = 6789;

int main()
{
    int sin_len;
    char message[256];
    int socket_descrīptor;
    struct sockaddr_in sin;

    int bind_rc, close_rc;
    ssize_t recv_rc;

    printf("Waiting for data from sender\n");

    // Initialize socket address structure for Internet Protocols
    bzero(&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(port);
    sin_len = sizeof(sin);

    //Create a UDP socket and bind it to the port
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    if(socket_descrīptor == -1)
    {
        perror("socket call failed");
        exit(errno);
    }

    bind_rc = bind(socket_descrīptor, (struct sockaddr *)&sin, sizeof(sin));
    if(bind_rc == -1)
    {
        perror("bind call failed");
        exit(errno);
    }
   
    //Loop forever (or until a termination message is received)
    // Received data through the socket and process it.The processing in this program is really simple --printing
    while(1)
    {
        recv_rc = recvfrom(socket_descrīptor, message, sizeof(message), 0, (struct sockaddr *)&sin, &sin_len);
        if(recv_rc == -1)
        {
            perror("bind call failed");
            exit(errno);
        }
        printf("Response from server : %s\n", message);
        if(strncmp(message, "stop", 4) == 0)
        {
            printf("sender has told me to end the connection\n");
            break;
        }
    }
    close_rc = close(socket_descrīptor);
    if(close_rc == -1)
    {
        perror("close call failed");
        exit(errno);
    }
    return 0;
}


/*
* sender2.c--UDP protocol example, add the erro check
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

int port = 6789;

int main()
{
    int socket_descrīptor;
    int iter = 0;

    ssize_t sendto_rc;
    int close_rc;

    char buf[80];
    struct sockaddr_in address;
    struct hostent *hostbyname;

    //Translate a host name to IP address
    hostbyname = gethostbyname("127.0.0.1");
    if(hostbyname == NULL)
    {
        perror("Gethostbyname failed");
        exit(errno);
    }

    /* Initialize socket address structure for Interner Protocols.The address comes from the datastructure returned by gethostbyname() */
    bzero(&address, sizeof(address)); // empty data structure
    address.sin_family = AF_INET;

    memcpy(&address.sin_addr.s_addr, hostbyname->h_addr, sizeof(address.sin_addr.s_addr));

    //print the host IP
    printf("The host IP is %s\n", inet_ntoa(*(struct in_addr*)hostbyname->h_addr));   

    //address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(port);

    /* Create a UDP socket */
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    if(socket_descrīptor == -1)
    {
        perror("socket call failed");
        exit(errno);
    }

    //Loop 20 times (a nice round number ) sending data
    for(iter = 0; iter <= 20; iter++)
    {
        sprintf(buf, "data packet with ID %d\n", iter);
        sendto_rc = sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address));
        if(sendto_rc == -1)
        {
            perror("sendto call failed");
            exit(errno);
        }
    }   

    /* send a termination message */
    sprintf(buf, "stop\n");
    sendto_rc = sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address)); //address is the target of the message send
    if(sendto_rc == -1)
    {
        perror("sendto  STOP call failed");
        exit(errno);
    }
   

    //Most people don''t bother to check the return code returned by the close function
    close_rc = close (socket_descrīptor);
    if(close_rc == -1)
    {
        perror("close call failed");
        exit(errno);
    }

    printf("Message Sent, Terminating\n");
    return 0;
}
   下面使用非阻塞I/O来对上面的接收程序做一些修改:
/*
* sender3.c--UDP protocol example, add the erro check,use sleep
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

int port = 6789;

int main()
{
    int socket_descrīptor;
    int iter = 0;

    ssize_t sendto_rc;
    int close_rc;

    char buf[80];
    struct sockaddr_in address;
    struct hostent *hostbyname;

    //Translate a host name to IP address
    hostbyname = gethostbyname("127.0.0.1");
    if(hostbyname == NULL)
    {
        perror("Gethostbyname failed");
        exit(errno);
    }

    /* Initialize socket address structure for Interner Protocols.The address comes from the datastructure returned by gethostbyname() */
    bzero(&address, sizeof(address)); // empty data structure
    address.sin_family = AF_INET;

    memcpy(&address.sin_addr.s_addr, hostbyname->h_addr, sizeof(address.sin_addr.s_addr));

    //print the host IP
    printf("The host IP is %s\n", inet_ntoa(*(struct in_addr*)hostbyname->h_addr));   

    //address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(port);

    /* Create a UDP socket */
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    if(socket_descrīptor == -1)
    {
        perror("socket call failed");
        exit(errno);
    }

    //Loop 20 times (a nice round number ) sending data
    for(iter = 0; iter <= 20; iter++)
    {
        sprintf(buf, "data packet with ID %d\n", iter);
        sendto_rc = sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address));
        if(sendto_rc == -1)
        {
            perror("sendto call failed");
            exit(errno);
        }
       
        sleep(3); //this is the only difference form sender2.c
       
    }   

    /* send a termination message */
    sprintf(buf, "stop\n");
    sendto_rc = sendto(socket_descrīptor, buf, sizeof(buf), 0, (struct sockaddr *)&address, sizeof(address)); //address is the target of the message send
    if(sendto_rc == -1)
    {
        perror("sendto  STOP call failed");
        exit(errno);
    }
   

    //Most people don''t bother to check the return code returned by the close function
    close_rc = close (socket_descrīptor);
    if(close_rc == -1)
    {
        perror("close call failed");
        exit(errno);
    }

    printf("Message Sent, Terminating\n");
    return 0;
}

/*
* receiver3.c--received of the UDP protocol,add the error check,this have some I/O no-blocking
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <fcntl.h>

int port = 6789;

int main()
{
    int sin_len;
    char message[256];
    int socket_descrīptor;
    struct sockaddr_in sin;

    int bind_rc, close_rc;
    ssize_t recv_rc;

    long save_file_flags;

    printf("Waiting for data from sender\n");

    // Initialize socket address structure for Internet Protocols
    bzero(&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
    sin.sin_port = htons(port);
    sin_len = sizeof(sin);

    //Create a UDP socket and bind it to the port
    socket_descrīptor = socket(AF_INET, SOCK_DGRAM, 0);
    if(socket_descrīptor == -1)
    {
        perror("socket call failed");
        exit(errno);
    }

    bind_rc = bind(socket_descrīptor, (struct sockaddr *)&sin, sizeof(sin));
    if(bind_rc == -1)
    {
        perror("bind call failed");
        exit(errno);
    }

    //set socket to non-blocking
    save_file_flags = fcntl(socket_descrīptor, F_GETFL);
    printf("file flags are %ld\n", save_file_flags);
    save_file_flags |= O_NONBLOCK;

    if(fcntl(socket_descrīptor, F_SETFL, save_file_flags) == -1)
    {
        perror("trying to set input socket to non-blocking");
        exit(errno);
    }

    printf("file flags are now %ld\n", save_file_flags);
   
    //Loop forever (or until a termination message is received)
    // Received data through the socket and process it.The processing in this program is really simple --printing
    while(1)
    {
        sleep(1);  //wait a moment ...

        recv_rc = recvfrom(socket_descrīptor, message, sizeof(message), 0, (struct sockaddr *)&sin, &sin_len);
        if(recv_rc == -1 && errno != EAGAIN)
        {
            fprintf(stderr, "errno %d ", errno);
            perror("recvform call failed");
            exit(errno);
        }
        else if(recv_rc == 0 | errno == EAGAIN) //no data
        {
            printf("no data yet\n");
            errno = 0; // clear the error
            continue;
        }
   
        errno = 0; // clear the error
        printf("Response from server : %s\n", message);
        if(strncmp(message, "stop", 4) == 0)
        {
            printf("sender has told me to end the connection\n");
            break;
        }
    }
    close_rc = close(socket_descrīptor);
    if(close_rc == -1)
    {
        perror("close call failed");
        exit(errno);
    }
    return 0;
}

    receiver3.c中最重要的新内容是fcntl调用,它允许对打开文件的控制标志做修改。带有参数F_GETEL的第一个调用用来取得当前控制标志的状态。然后O_NONBLOCK标志被添加到当前标志中。带有参数F_SETFL的跌入个调用实际上改变了标志。一旦这一步成功完成,对套接口的读取(recvfrom)操作就不再等候数据了。当然,程序要可是fcntl的第二次调用是成功的。
    最终执行的结果如下:
[hyj@localhost udp_]$ ./sender3
The host IP is 127.0.0.1
Message Sent, Terminating


[hyj@localhost udp_]$ ./receiver3
Waiting for data from sender
file flags are 2
file flags are now 2050
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
no data yet
Response from server : data packet with ID 0

no data yet
no data yet
Response from server : data packet with ID 1

no data yet
no data yet
Response from server : data packet with ID 2

no data yet
no data yet
Response from server : data packet with ID 3

no data yet
no data yet
Response from server : data packet with ID 4

no data yet
no data yet
Response from server : data packet with ID 5

no data yet
no data yet
Response from server : data packet with ID 6

no data yet
no data yet
Response from server : data packet with ID 7

no data yet
no data yet
Response from server : data packet with ID 8

no data yet
no data yet
Response from server : data packet with ID 9

no data yet
no data yet
Response from server : data packet with ID 10

no data yet
no data yet
Response from server : data packet with ID 11

no data yet
no data yet
Response from server : data packet with ID 12

no data yet
no data yet
Response from server : data packet with ID 13

no data yet
no data yet
Response from server : data packet with ID 14

no data yet
no data yet
Response from server : data packet with ID 15

no data yet
no data yet
Response from server : data packet with ID 16

no data yet
no data yet
Response from server : data packet with ID 17

no data yet
no data yet
Response from server : data packet with ID 18

no data yet
no data yet
Response from server : data packet with ID 19

no data yet
no data yet
Response from server : data packet with ID 20

no data yet
no data yet
Response from server : stop

sender has told me to end the connection

评论

此博客中的热门博文

【转】AMBA、AHB、APB总线简介

AMBA 简介 随着深亚微米工艺技术日益成熟,集成电路芯片的规模越来越大。数字IC从基于时序驱动的设计方法,发展到基于IP复用的设计方法,并在SOC设计中得到了广泛应用。在基于IP复用的SoC设计中,片上总线设计是最关键的问题。为此,业界出现了很多片上总线标准。其中,由ARM公司推出的AMBA片上总线受到了广大IP开发商和SoC系统集成者的青睐,已成为一种流行的工业标准片上结构。AMBA规范主要包括了AHB(Advanced High performance Bus)系统总线和APB(Advanced Peripheral Bus)外围总线。   AMBA 片上总线        AMBA 2.0 规范包括四个部分:AHB、ASB、APB和Test Methodology。AHB的相互连接采用了传统的带有主模块和从模块的共享总线,接口与互连功能分离,这对芯片上模块之间的互连具有重要意义。AMBA已不仅是一种总线,更是一种带有接口模块的互连体系。下面将简要介绍比较重要的AHB和APB总线。 基于 AMBA 的片上系统        一个典型的基于AMBA总线的系统框图如图3所示。        大多数挂在总线上的模块(包括处理器)只是单一属性的功能模块:主模块或者从模块。主模块是向从模块发出读写操作的模块,如CPU,DSP等;从模块是接受命令并做出反应的模块,如片上的RAM,AHB/APB 桥等。另外,还有一些模块同时具有两种属性,例如直接存储器存取(DMA)在被编程时是从模块,但在系统读传输数据时必须是主模块。如果总线上存在多个主模块,就需要仲裁器来决定如何控制各种主模块对总线的访问。虽然仲裁规范是AMBA总线规范中的一部分,但具体使用的算法由RTL设计工程师决定,其中两个最常用的算法是固定优先级算法和循环制算法。AHB总线上最多可以有16个主模块和任意多个从模块,如果主模块数目大于16,则需再加一层结构(具体参阅ARM公司推出的Multi-layer AHB规范)。APB 桥既是APB总线上唯一的主模块,也是AHB系统总线上的从模块。其主要功能是锁存来自AHB系统总...

【转】GPIO编程模拟I2C入门

ARM编程:ARM普通GPIO口线模拟I2C  请教个问题: 因为需要很多EEPROM进行点对点控制,所以我现在要用ARM的GPIO模拟I2C,管脚方向我设 置的是向外的。我用网上的RW24C08的万能程序修改了一下,先进行两根线的模拟,SDA6, SCL6,但是读出来的数不对。我做了一个简单的实验,模拟SDA6,SCL6输出方波,在示波 器上看到正确方波,也就是说,我的输出控制是没问题的。 哪位大哥能指点一下,是否在接收时管脚方向要设为向内?(不过IOPIN不管什么方向都可 以读出当前状态值的阿) 附修改的RW24C08()程序: #define  SomeNOP() delay(300); /**/ /* *********************************  RW24C08   **************************************** */ /**/ /* ----------------------------------------------------------------------------- ---  调用方式:void I2CInit(void)   函数说明:私有函数,I2C专用 ------------------------------------------------------------------------------- -- */ void  I2CInit( void ) ... {  IO0CLR  =  SCL6;      // 初始状态关闭总线  SomeNOP();  // 延时   I2CStop();  // 确保初始化,此时数据线是高电平 }   /**/ /* ---------------------------------------------------------------------------- ----  调用方式:void I2CSta...

【转】cs8900网卡的移植至基于linux2.6内核的s3c2410平台

cs8900网卡的移植至基于linux2.6内核的s3c2410平台(转) 2008-03-11 20:58 硬件环境:SBC-2410X开发板(CPU:S3C2410X) 内核版本:2.6.11.1 运行环境:Debian2.6.8 交叉编译环境:gcc-3.3.4-glibc-2.3.3 第一部分 网卡CS8900A驱动程序的移植 一、从网上将Linux内核源代码下载到本机上,并将其解压: #tar jxf linux-2.6.11.1.tar.bz2 二、打开内核顶层目录中的Makefile文件,这个文件中需要修改的内容包括以下两个方面。 (1)指定目标平台。 移植前:         ARCH?= $(SUBARCH) 移植后: ARCH            :=arm (2)指定交叉编译器。 移植前: CROSS_COMPILE ?= 移植后: CROSS_COMPILE   :=/opt/crosstool/arm-s3c2410-linux-gnu/gcc-3.3.4-glibc-2.3.3/bin/arm-s3c2410-linux-gnu- 注:这里假设编译器就放在本机的那个目录下。 三、添加驱动程序源代码,这涉及到以下几个方面。(1)、从网上下载了cs8900.c和cs8900.h两个针对2.6.7的内核的驱动程序源代码,将其放在drivers/net/arm/目录下面。 #cp cs8900.c ./drivers/net/arm/ #cp cs8900.h ./drivers/net/arm/ 并在cs8900_probe()函数中,memset (&priv,0,sizeof (cs8900_t));函数之后添加如下两条语句: __raw_writel(0x2211d110,S3C2410_BWSCON); __raw_writel(0x1f7c,S3C2410_BANKCON3); 注:其原因在"第二部分"解释。 (2)、修改drivers/net/arm/目录下的Kconfig文件,在最后添加如...