广播的
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0)
#define SER_IP "192.168.0.255"//本机 IP
#define SER_PORT 6666 //1024-49151
#define CLI_IP "192.168.8.61"
#define CLI_PORT 8888
int main(int argc,const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
//设置允许广播
int broad = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("setsockopt success\n");
//绑定客户端的地址信息结构体,非必须绑定,如果不绑定,由操作系统自动绑定
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family =AF_INET;//必须填AF_INET
sin.sin_port = htons(SER_PORT);//端口号的网络字节序
sin.sin_addr.s_addr= inet_addr(SER_IP);//服务器ip地址
struct sockaddr_in rcvaddr;//存储数据包是从谁哪里来的
socklen_t addrlen = sizeof(rcvaddr);
char buf[128]="";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
//发送数据发送给服务器
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
/*
//接收数据
res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&rcvaddr,&addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s : %d]: %s\n",inet_ntoa(rcvaddr.sin_addr),ntohs(rcvaddr.sin_port),buf);
*/
}
//关闭文件描述符
close(cfd);
return 0;
}
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.0.255"//本机 IP
#define PORT 6666 //1024-49151
int main(int argc,const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family =AF_INET;//必须填AF_INET
sin.sin_port = htons(PORT);//端口号
sin.sin_addr.s_addr= inet_addr(IP);//本机IP
//绑定 必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
struct sockaddr_in cin;//存储数据包是从哪里来的
socklen_t addrlen = sizeof(cin);
char buf[128]="";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
//接收数据,必须接收数据包的发送方地址信息给sendto使用
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
if(res<0)
{
ERR_MSG("recvform");
return -1;
}
printf("[%s : %d]: %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//关闭文件描述符
}
close(sfd);
return 0;
}
组播的发送与接收
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0)
#define GRP_IP "224.1.2.3"//本机 IP
#define SER_PORT 6666 //1024-49151
#define CLI_IP "192.168.8.61"
#define CLI_PORT 6666
int main(int argc,const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("setsockopt success\n");
//绑定客户端的地址信息结构体,非必须绑定,如果不绑定,由操作系统自动绑定
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family =AF_INET;//必须填AF_INET
sin.sin_port = htons(SER_PORT);//端口号的网络字节序
sin.sin_addr.s_addr= inet_addr(GRP_IP);//组播ip地址
struct sockaddr_in rcvaddr;//存储数据包是从谁哪里来的
socklen_t addrlen = sizeof(rcvaddr);
char buf[128]="";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
//发送数据发送给服务器
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
/*
//接收数据
res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&rcvaddr,&addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s : %d]: %s\n",inet_ntoa(rcvaddr.sin_addr),ntohs(rcvaddr.sin_port),buf);
*/
}
//关闭文件描述符
close(cfd);
return 0;
}
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.0.118"//本机 IP
#define GRP_IP "224.1.2.3"
#define PORT 6666 //1024-49151
int main(int argc,const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
//加入多播组
struct ip_mreqn mq;
mq.imr_multiaddr.s_addr=inet_addr(GRP_IP);
mq.imr_address.s_addr = inet_addr(IP);
mq.imr_ifindex = 2;
if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mq,sizeof(mq))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("加入组播成功\n");
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family =AF_INET;//必须填AF_INET
sin.sin_port = htons(PORT);//端口号
sin.sin_addr.s_addr= inet_addr(GRP_IP);//本机IP
//绑定 必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
struct sockaddr_in cin;//存储数据包是从哪里来的
socklen_t addrlen = sizeof(cin);
char buf[128]="";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
//接收数据,必须接收数据包的发送方地址信息给sendto使用
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
if(res<0)
{
ERR_MSG("recvform");
return -1;
}
printf("[%s : %d]: %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//关闭文件描述符
}
close(sfd);
return 0;
}