跳至主要内容

【转】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

评论

此博客中的热门博文

【转】VxWorks中的地址映射

在运用嵌入式系统VxWorks和MPC860进行通信系统设计开发时,会遇到一个映射地址不能访问的问题。 缺省情况下,VxWorks系统已经进行了如下地址的映射:   memory地址、bcsr(Board Control and Status)地址、PC_BASE_ADRS(PCMCIA)地址、Internal Memory地址、rom(Flach memory)地址等,但是当你的硬件开发中要加上别的外设时,如(falsh、dsp、FPGA等),对这些外设的访问也是通过地址形式进行读写,如果你没有加相应的地址映射,那么是无法访问这些外设的。   和VxWorks缺省地址映射类似,你也可以进行相应的地址映射。   如下是地址映射原理及实现:   1、 地址映射结构 在Tornado\target\h\vmLib.h文件中 typedef struct phys_mem_desc { void *virtualAddr; void *physicalAddr; UINT len; UINT initialStateMask; /* mask parameter to vmStateSet */ UINT initialState; /* state parameter to vmStateSet */ } PHYS_MEM_DESC; virtualAddr:你要映射的虚拟地址 physicalAddr:硬件设计时定义的实际物理地址 len;要进行映射的地址长度 initialStateMask:可以初始化的地址状态: 有如下状态: #define VM_STATE_MASK_VALID 0x03 #define VM_STATE_MASK_WRITABLE 0x0c #define VM_STATE_MASK_CACHEABLE 0x30 #define VM_STATE_MASK_MEM_COHERENCY 0x40 #define VM_STATE_MASK_GUARDED 0x80 不同的CPU芯片类型还有其特殊状态 initialState:实际初始化的地址状态: 有如下状态: #define VM_STATE_VALID 0x01 #define VM_STATE_VALID_NOT 0x00 #define VM_STATE_WRITA

【转】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文件,在最后添加如下内容: Config ARM_CS8900    tristate "CS8900 support" depends on NET_ETHERNET && A

【转】多迷人Gtkmm啊

前边已经说过用glade设计界面然后动态装载,接下来再来看看怎么改变程序的皮肤(主题)     首先从 http://art.gnome.org/themes/gtk2 下载喜欢的主题,从压缩包里提取gtk-2.0文件夹让它和我们下边代码生成的可执行文件放在同一个目录下,这里我下载的的 http://art.gnome.org/download/themes/gtk2/1317/GTK2-CillopMidnite.tar.gz     然后用glade设计界面,命名为main.glade,一会让它和我们下边代码生成的可执行程序放在同一个目录下边     然后开始写代码如下: //main.cc #include <gtkmm.h> #include <libglademm/xml.h> int main(int argc, char *argv[]) {     Gtk::Main kit(argc,argv);         Gtk::Window *pWnd;        gtk_rc_parse("E:\\theme-viewer\\themes\\gtk-2.0\\gtkrc");       Glib::RefPtr<Gnome::Glade::Xml> refXml;     try     {         refXml = Gnome::Glade::Xml::create("main.glade");     }     catch(const Gnome::Glade::XmlError& ex)     {         Gtk::MessageDialog dialog("Load glade file failed!", false,       \                                   Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);         dialog.run();               return 1;     }         refXml->get_widget("main", pWnd);     if(pW