1.设置端口允许快速复用;允许广播
快速复用代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
int main(int argc, const char *argv[])
{
//1.创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
perror("socket");
return -1;
}
printf("套接字创建完成\n");
//设置允许端口快速重用
int optname = -1;
socklen_t addlen = sizeof(optname);
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &optname, addlen) < 0)
{
perror("setsockopt");
return -1;
}
printf("允许快速重用设置成功\n");
//判断端口是否允许快速重用
if(getsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &optname, &addlen) < 0)
{
perror("getsockopt");
return -1;
}
printf("%d\n", optname);
close(sfd);
return 0;
}
运行结果:
允许广播代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
int main(int argc, const char *argv[])
{
//1.创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
perror("socket");
return -1;
}
printf("套接字创建完成\n");
//设置允许广播
int optname = -1; //设置为非0即可
socklen_t addlen = sizeof(optname);
if(setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &optname, addlen) < 0)
{
perror("setsockopt");
return -1;
}
printf("允许广播设置成功\n");
//判断是否允许广播
if(getsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &optname, &addlen) < 0)
{
perror("getsockopt");
return -1;
}
printf("%d\n", optname);
close(sfd);
return 0;
}
运行结果:
2.发送广播、接收广播信息。
发送代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
#define ERR_MSG(msg) {fprintf(stderr, "行:%d", __LINE__);perror(msg);}
#define PORT 6666
#define IP "192.168.124.255"
int main(int argc, const char *argv[])
{
//1.创建报式套接字
int cfd = socket(AF_INET, SOCK_DGRAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("套接字创建完成\n");
//设置允许广播
int optname = -1; //设置为非0即可
socklen_t addlen = sizeof(optname);
if(setsockopt(cfd, SOL_SOCKET, SO_BROADCAST, &optname, addlen) < 0)
{
perror("setsockopt");
return -1;
}
printf("允许广播设置成功\n");
//2.填充地址信息结构体,给sendto使用
//3.将数据发送给对端
struct sockaddr_in cin;
cin.sin_family = AF_INET; //必须填AF_INET
cin.sin_port = htons(PORT); //对端的端口
cin.sin_addr.s_addr = inet_addr(IP); //对端IP
struct sockaddr_in sin; //存储接收到的对端地址信息
socklen_t addrlen = sizeof(sin); //存储对端地址长度
char buf[128] = "";
ssize_t res;
while(1)
{
bzero(buf, sizeof(buf));
//4.发送
printf("请输入数据:\n");
scanf("%s", buf);
if(sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, sizeof(cin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功:%s\n", buf);
}
//5.关闭
if(close(cfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
接收代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
#define ERR_MSG(msg) {fprintf(stderr, "行:%d", __LINE__);perror(msg);}
#define PORT 6666
#define IP "192.168.124.255"
int main(int argc, const char *argv[])
{
//1.创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("套接字创建完成\n");
//2.允许端口快速被复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速被复用\n");
//允许广播
int optname = -1; //设置为非0即可
socklen_t addlen = sizeof(optname);
if(setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &optname, addlen) < 0)
{
perror("setsockopt");
return -1;
}
printf("允许广播设置成功\n");
//3.填充地址信息结构体,给bind使用
//4.真实的地址信息结构体根据地址族指定,AF_INET-->MAN 7 IP
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(PORT); //端口号的网络字节序,1014-49151
sin.sin_addr.s_addr = inet_addr(IP); //本机IP,ubuntu 中ifconfig
//5.bind绑定服务器地址信息,必须绑定
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin) ) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("绑定成功\n");
char buf[128] = "";
struct sockaddr_in cin; //存储接收到的对端地址信息
socklen_t addrlen = sizeof(cin); //存储对端地址长度
ssize_t res;
while(1)
{
bzero(buf, sizeof(buf));
//6.接收
if((res = recvfrom(sfd, buf, sizeof(buf), 0,(struct sockaddr*)&cin, &addrlen)) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("接收成功[%s: %d] %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
}
//8.关闭
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
运行结果:
3.发送组播、接收组播信息。
发送代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
#define ERR_MSG(msg) {fprintf(stderr, "行:%d", __LINE__);perror(msg);}
#define PORT 6666
#define IP "224.1.2.3"
int main(int argc, const char *argv[])
{
//1.创建报式套接字
int cfd = socket(AF_INET, SOCK_DGRAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("套接字创建完成\n");
//2.填充地址信息结构体,给sendto使用
//3.将数据发送给对端
struct sockaddr_in cin;
cin.sin_family = AF_INET; //必须填AF_INET
cin.sin_port = htons(PORT); //组播的端口
cin.sin_addr.s_addr = inet_addr(IP); //组播IP
struct sockaddr_in sin; //存储接收到的对端地址信息
socklen_t addrlen = sizeof(sin); //存储对端地址长度
char buf[128] = "";
ssize_t res;
while(1)
{
bzero(buf, sizeof(buf));
//4.发送
printf("请输入数据:\n");
scanf("%s", buf);
if(sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, sizeof(cin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功:%s\n", buf);
}
//6.关闭
if(close(cfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
接收代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
#define ERR_MSG(msg) {fprintf(stderr, "行:%d", __LINE__);perror(msg);}
#define PORT 6666 //进程端口1024-49151
#define IP "224.1.2.3" //组播IP
#define LIP "192.168.124.25" //本机IP
int main(int argc, const char *argv[])
{
//1.创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("套接字创建完成\n");
//2.允许端口快速被复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速被复用\n");
//3.加入多播组
struct ip_mreqn mq;
mq.imr_multiaddr.s_addr = inet_addr(IP);
mq.imr_address.s_addr = inet_addr(LIP);
mq.imr_ifindex = 0;
if(setsockopt(sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mq, sizeof(mq)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("加入多播组成功\n");
//4.填充地址信息结构体,给bind使用
//5.真实的地址信息结构体根据地址族指定,AF_INET-->MAN 7 IP
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
//6.bind绑定服务器地址信息,必须绑定
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin) ) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("绑定成功\n");
char buf[128] = "";
struct sockaddr_in cin; //存储接收到的对端地址信息
socklen_t addrlen = sizeof(cin); //存储对端地址长度
ssize_t res;
while(1)
{
bzero(buf, sizeof(buf));
//7.接收
if((res = recvfrom(sfd, buf, sizeof(buf), 0,(struct sockaddr*)&cin, &addrlen)) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("接收成功[%s: %d] %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
}
//8.关闭
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
运行结果: