發信人: gdtyy (gdtyy), 信區: Embedded
標 題: 第四講 UDP編程
發信站: 水木社區 (Mon Jun 25 23:31:12 2007), 站內
******************
* 第四講 UDP編程 *
******************
2006/12/31 asdjf@163.com www.armecos.com
很多網友需要跑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測試程式
#include <network.h>
#include <pkgconf/system.h>
#include <pkgconf/net.h>
#include <cyg/infra/testcase.h>
#ifdef CYGBLD_DEVS_ETH_DEVICE_H // Get the device config if it exists
#include 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)ð0_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://newsmth.net·[FROM: 61.149.56.*]