UDP编程

4、UDP编程

******************
* 第四讲 UDP编程 *
******************
    很多网友需要跑TCP/IP协议栈,那么用ecos是再惬意不过的事情了。ecos自身提供三种完整协议栈:FreeBSD、OpenBSD、lwip,当然如果你愿意,也可以移植其他协议栈,爽吧!
    我配置的ecos系统选择了FreeBSD栈,功能非常强大,该有的基本都有,标准接口,与PC机上网络开发没有任何区别。ecos自带测试范例,改改就能直接应用。
    UDP的优点是开销小,速度快;缺点是面向无连接,无纠错能力。UDP在网络实时应用中用得很多。
    下面是UDP测试源码,略微不同的是一开始要先执行“init_all_network_interfaces();”,同时判断网卡是否安装(条件判断CYGHWR_NET_DRIVER_ETH0)。剩下的部分就全是标准写法。这里用到的一些socket函数有阻塞功能,不需要额外延时,所以处理速度很快。
UDP测试源码
//此程序配合上位机UDP测试程序
#i nclude <network.h>
#i nclude <pkgconf/system.h>
#i nclude <pkgconf/net.h>
#i nclude <cyg/infra/testcase.h>
#ifdef CYGBLD_DEVS_ETH_DEVICE_H    // Get the device config if it exists
#i nclude CYGBLD_DEVS_ETH_DEVICE_H  // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
#endif
#ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
# ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
#  include CYGTST_DEVS_ETH_TEST_NET_REALTIME
# endif
#endif
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
static char stack[STACK_SIZE],stack1[STACK_SIZE];
static cyg_thread thread_data,thread_data1;
static cyg_handle_t thread_handle,thread_handle1;
void
pexit(char *s)
{
    CYG_TEST_FAIL_FINISH(s);
}
void
udp_test(struct bootp *bp)
{
    struct sockaddr_in host,client;
    int s,len,c_len;
    unsigned char buf[100];
    unsigned char msga[] = {"YY:
www.armecos.com"};
    unsigned char msge[] = {"Error!"};
    unsigned char num = 5;
    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s < 0) {
        pexit("socket");
        return;
    }
    // Set up host address
    host.sin_family = AF_INET;
    host.sin_len = sizeof(host);
    host.sin_addr.s_addr = INADDR_ANY;
    host.sin_port = ntohs(1025);
    if(bind(s, (struct sockaddr *) &host, sizeof(host)) < 0) {
        pexit("bind /source/ error");
    }
    while(true){
        c_len = sizeof(client);
        len = recvfrom(s, buf, sizeof(buf),0,(struct sockaddr *)&client,&c_len);
        if(len > 0){
            if(buf[0] == 'A')
                num = 5;
            else
                len = sendto(s,buf,sizeof(buf),0,(struct sockaddr *)&client,sizeof(client));
        }
        while(num != 0){
            len = sendto(s,msga,sizeof(msga),0,(struct sockaddr *)&client,sizeof(client));
            if(len != sizeof(msga))
                len = sendto(s,msge,sizeof(msge),0,(struct sockaddr *)&client,sizeof(client));
            //cyg_thread_delay(10);
            num--;
        }
    }   
}
void
net_test(cyg_addrword_t p)
{
    diag_printf("Start Networking Test...\n");
    init_all_network_interfaces();
#ifdef CYGHWR_NET_DRIVER_ETH0
    if (eth0_up) {   
        cyg_thread_create(10,                // Priority - just a number
                      udp_test,              // entry
                      (cyg_addrword_t)&eth0_bootp_data,      // entry parameter
                      "Network udp test",    // Name
                      &stack1[0],            // Stack
                      STACK_SIZE,            // Size
                      &thread_handle1,       // Handle
                      &thread_data1          // Thread data structure
            );
        cyg_thread_resume(thread_handle1);  // Start it
    }
#endif
}
void
cyg_start(void)
{
    // Create a main thread, so we can run the scheduler and have time 'pass'
    cyg_thread_create(10,                // Priority - just a number
                      net_test,          // entry
                      0,                 // entry parameter
                      "Network test",    // Name
                      &stack[0],         // Stack
                      STACK_SIZE,        // Size
                      &thread_handle,    // Handle
                      &thread_data       // Thread data structure
            );
    cyg_thread_resume(thread_handle);  // Start it
    cyg_scheduler_start();
}

http://blog.21ic.com/user1/5586/archives/2009/57917.html

原文地址:https://www.cnblogs.com/kuainiao/p/2854944.html