vongy 发表于 2008-11-25 15:25
请问在vxworks下如何往2个网口(或其中之一)发送UDP广播
用MPC852T,VXWORKS5.5,将MPC852T的FEC口为配为100M网口,SCC口配为10M网口,编写了发送UDP广播代码,通过在FEC和SCC的发送函数中加打印信息可以看到,发送的UDP广播,发到FEC口去了,我想将UDP广播发送到SCC口,不知如何实现?VXWORKS是如何区别两个网口的?代码如下,恳请高手看看,先谢过。初始化时,分别初始化为motfec1,motfcc2。FEC口IP地址:192.168.0.114,SCC口IP地址:10.168.1.238
long sockAddrSize;
BOOL opt = TRUE;
char buf[SOCKET_PACK_MAX_SIZE];
char *pBuf;
int pktSize;
long sockAddrSize;
int i,len;
sockAddrSize = sizeof(struct sockaddr_in);
if((gBspSocketTxTestFd = socket(AF_INET, SOCK_DGRAM, 0)) == ERROR)
{
return ERROR;
}
memset((char *)&gBspLocalRxSocketAddr, 0,sockAddrSize);
gBspLocalRxSocketAddr.sin_len = (u_char) sockAddrSize;
gBspLocalRxSocketAddr.sin_family = AF_INET;
gBspLocalRxSocketAddr.sin_addr.s_addr = INADDR_ANY;
gBspLocalRxSocketAddr.sin_addr.s_addr = (u_long)(0x0AA801EE);
gBspLocalRxSocketAddr.sin_addr.s_addr = inet_addr("10.168.1.255");
gBspLocalRxSocketAddr.sin_port = SOCKET_PORT; /* port */
if (bind (gBspSocketTxTestFd, (struct sockaddr *)&gBspLocalRxSocketAddr, sockAddrSize) == ERROR)
{
return ERROR;
}
setsockopt(gBspSocketTxTestFd,SOL_SOCKET,SO_BROADCAST,(char *)&opt,sizeof(opt));
sockAddrSize = sizeof(struct sockaddr_in);
pBuf = buf;
pktSize = len;
memset((char *)&gBspRemoteRxSocketAddr, 0,sockAddrSize);
gBspRemoteRxSocketAddr.sin_len = (u_char) sockAddrSize;
gBspRemoteRxSocketAddr.sin_family = AF_INET;
gBspRemoteRxSocketAddr.sin_addr.s_addr = INADDR_BROADCAST;
gBspRemoteRxSocketAddr.sin_port = SOCKET_PORT; /* port */
if(sendto(gBspSocketTxTestFd,(char*)pBuf,pktSize,0,
(struct sockaddr*)&gBspRemoteRxSocketAddr,sockAddrSize) == ERROR)
{
return ERROR;
}
return (BSP_OK);
wwp 发表于 2008-11-30 20:07
gBspRemoteRxSocketAddr.sin_addr.s_addr = INADDR_BROADCAST;
改成:
gBspRemoteRxSocketAddr.sin_addr.s_addr = inet_addr("10.168.1.255");
试试
zwicky 发表于 2008-12-1 16:08
bind时指定IP
huchuanyi 发表于 2008-12-4 15:14
谢谢楼上2位!我的问题基本已解决,我将SCC口的IP地址设置成为255.255.255.X,掩码设置成为255.255.255.0,就可以发送出UDP广播,而不是网口所在网段的UDP广播,(我分析了发出的数据包的IP头,的确是255.255.255.255);用ipDetach关掉FEC口后,发送UDP广播,就可以从SCC口发出。VXWORKS默认第一个网口发送广播。