如何用socket发送广播帧
1个回答
展开全部
这是一个发送多播帧的例子:
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define SERV_PORT 5000
int main(int,char**)
{
int SendtoSocket;
sockaddr_in SendtoAddr;
int AddrLen=sizeof(SendtoAddr);
char szBuffer[200]="This is a test\n";
char *szTime;
SendtoSocket=socket(AF_INET,SOCK_DGRAM,0);
bzero(&SendtoAddr,sizeof(SendtoAddr));
SendtoAddr.sin_family=AF_INET;
SendtoAddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,"224.0.3.1",&SendtoAddr.sin_addr);
const int on=1;
// setsockopt(SendtoSocket,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on));
time_t CurrentTime;
for(;;)
{
time(&CurrentTime);
szTime=ctime(&CurrentTime);
sendto(SendtoSocket,szTime,strlen(szTime),0,(sockaddr *)&SendtoAddr,AddrLe
n);
sleep(1);
}
}
这是接收多播帧的例子:
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <string.h>
#include <unistd.h>
#define SERV_PORT 5000
int main(int,char**)
{
int RecvSocket;
sockaddr_in ServAddr;
socklen_t AddrLen=sizeof(ServAddr);
char szBuffer[201];
RecvSocket=socket(AF_INET,SOCK_DGRAM,0);
bzero(&ServAddr,sizeof(ServAddr));
ServAddr.sin_family=AF_INET;
ServAddr.sin_addr.s_addr=htonl(INADDR_ANY);
ServAddr.sin_port=htons(SERV_PORT);
bind(RecvSocket,(sockaddr *)&ServAddr,AddrLen);
ip_mreq mreq;
inet_pton(AF_INET,"224.0.3.1",&mreq.imr_multiaddr.s_addr);
mreq.imr_interface.s_addr=ServAddr.sin_addr.s_addr;
setsockopt(RecvSocket,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
int nRecv;
for(;;)
{
nRecv=recvfrom(RecvSocket,szBuffer,200,0,(sockaddr *)&ServAddr,&AddrLen);
szBuffer[nRecv]='\0';
printf(szBuffer);
}
}
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define SERV_PORT 5000
int main(int,char**)
{
int SendtoSocket;
sockaddr_in SendtoAddr;
int AddrLen=sizeof(SendtoAddr);
char szBuffer[200]="This is a test\n";
char *szTime;
SendtoSocket=socket(AF_INET,SOCK_DGRAM,0);
bzero(&SendtoAddr,sizeof(SendtoAddr));
SendtoAddr.sin_family=AF_INET;
SendtoAddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,"224.0.3.1",&SendtoAddr.sin_addr);
const int on=1;
// setsockopt(SendtoSocket,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on));
time_t CurrentTime;
for(;;)
{
time(&CurrentTime);
szTime=ctime(&CurrentTime);
sendto(SendtoSocket,szTime,strlen(szTime),0,(sockaddr *)&SendtoAddr,AddrLe
n);
sleep(1);
}
}
这是接收多播帧的例子:
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <string.h>
#include <unistd.h>
#define SERV_PORT 5000
int main(int,char**)
{
int RecvSocket;
sockaddr_in ServAddr;
socklen_t AddrLen=sizeof(ServAddr);
char szBuffer[201];
RecvSocket=socket(AF_INET,SOCK_DGRAM,0);
bzero(&ServAddr,sizeof(ServAddr));
ServAddr.sin_family=AF_INET;
ServAddr.sin_addr.s_addr=htonl(INADDR_ANY);
ServAddr.sin_port=htons(SERV_PORT);
bind(RecvSocket,(sockaddr *)&ServAddr,AddrLen);
ip_mreq mreq;
inet_pton(AF_INET,"224.0.3.1",&mreq.imr_multiaddr.s_addr);
mreq.imr_interface.s_addr=ServAddr.sin_addr.s_addr;
setsockopt(RecvSocket,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
int nRecv;
for(;;)
{
nRecv=recvfrom(RecvSocket,szBuffer,200,0,(sockaddr *)&ServAddr,&AddrLen);
szBuffer[nRecv]='\0';
printf(szBuffer);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询