|
我的发送程序
#include "vxWorks.h"
#include "fioLib.h"
#include "stdio.h"
#include "unistd.h"
#include "string.h"
#include "usrLib.h"
#include "errnoLib.h"
#include "hostLib.h"
#include "sockLib.h"
#include "socket.h"
#include "inetLib.h"
#include "in.h"
#define SOCKET_ERROR -1
#if 1
int testsend(void)
{
int eno,fd;
struct sockaddr_in sa;
char buf[] = "Hello, World!";
int sock_reuse = 1;
int loopback = 1;
int ttl = 50;
int quitting = -1;
sa.sin_family = AF_INET;
sa.sin_port = htons(5100);
sa.sin_addr.s_addr = inet_addr("224.1.2.1");
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
{
printf("Couldn't create a UDP socket\n");
return -1;
}
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_reuse, sizeof(sock_reuse)) < 0)
{
printf("socket options set error:reuse\n");
close(fd);
return -1;
}
if((eno = setsockopt(fd,IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(int)))<0)
{
printf("socket options set error:TTL eno:%d\n",eno);
close(fd);
return -1;
}
if(setsockopt(fd,IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopback, sizeof(int))<0)
{
printf("socket options set error:loopback\n");
close(fd);
return -1;
}
while(!quitting)
{
if(sendto(fd,buf,sizeof(buf),0,(struct sockaddr *)&sa,sizeof(sa))== SOCKET_ERROR)
{
printf("Send error!\n");
close(fd);
return -1;
}else
{
printf("Send OK\n!");
}
taskDelay(1);
}
close(fd);
}
#endif
运行的时候setsockopt 函数返回错误,就不能发送
|
|