在程序头文件中定义的UDP数据报的结构:
struct tagEthernet
{
_US len; // 为data字段的实际长度
_UC dest[6];
_UC src[6];
_US type;
_UC ethdata[1];
};
struct tagIP
{
_UC ver_hl;
_UC tos;
_US len;
_US id;
_US flag_off;
_UC ttl;
_UC proto;
_US chksum;
union
{
_UL longSrc;
_UC byteSrc[4];
} src;
union
{
_UL longDest;
_UC byteDest[4];
} dest;
_UC ipdata[1];
};
struct tagUDP
{
_US src;
_US dest;
_US len;
_US chksum;
_UC udpdata[1];
};
extern _UC xdata buffer[2400];
#define p ((struct tagEthernet*)buffer)
// IP层提供的函数
#define iphdr ((struct tagIP*)(p-> ethdata))
void ip_input();
void ip_output(_UL dest, _UC proto);
// UDP层提供的函数
#define pUdp ((struct tagUDP*)(((struct tagIP*)(p-> ethdata))-> ipdata));
void udp_input(_US len);
void udp_output(_US L);
下面是部分KEIL C51编的UDP打包程序
//UDP打包函数
void udp_output(_US L)
{
//设置UDP头
pUdp-> src=1025;
pUdp-> dest=1024;
pUdp-> len=L+8;
pUdp-> chksum=udp_chksum();
//设置IP头、以太网帧头
ip_output(iphdr-> src.longSrc,17); //proto=17表示上层协议是udp
//发送数据
p-> len=(pUdp-> len)+(20+14); //20为IP头长度、14为以太帧长度
Send();
}
在对UDP打包程序进行编译时,总是提示dest,src,len以及chksum没有定义或语法错误。UDP提供的函数中这个宏定义#define pUdp ((struct tagUDP*)(((struct tagIP*)(p-> ethdata))-> ipdata));好象没起作用。
恳请那位对UDP有所 了解的大侠呢感指点一下。谢谢啦!