1、将广播发送端和接收端各实现一遍
发送端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd == -1)
{
perror("socker error");
return -1;
}
printf("socket success\n");
//设置允许广播
int broadcast = -1;
if(setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast))==-1)
{
perror("setsockopt error");
return -1;
}
//绑定(非必要)
//填充接收端地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(6666);
sin.sin_addr.s_addr = inet_addr("192.168.117.255");
//发送数据
char sbuf[128]="";
while(1)
{
printf("请输入:");
fgets(sbuf,sizeof(sbuf),stdin);
sbuf[strlen(sbuf)-1] = 0;
sendto(sfd,sbuf,sizeof(sbuf),0,(struct sockaddr*)&sin,sizeof(sin));
printf("发送成功\n");
}
close(sfd);
return 0;
}
接收端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int rfd = socket(AF_INET,SOCK_DGRAM,0);
if(rfd == -1)
{
perror("socket error");
return -1;
}
printf("socket success\n");
//填入地址信息结构体
struct sockaddr_in rin;
rin.sin_family = AF_INET;
rin.sin_port = htons(6666);
rin.sin_addr.s_addr = inet_addr("192.168.117.255");
//绑定
if(bind(rfd,(struct sockaddr*)&rin,sizeof(rin))==-1)
{
perror("bind error");
return -1;
}
//数据交互
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
recv(rfd,rbuf,sizeof(rbuf),0);
printf("收到的消息为:%s\n",rbuf);
}
close(rfd);
return 0;
}
2、将组播发送端和接收端各实现一遍
发送端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//1、创建套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd == -1)
{
perror("socket error");
return -1;
}
//2、bind(非必须)
//3、填充接收端地址信息结构体
struct sockaddr_in rin;
rin.sin_family = AF_INET;
rin.sin_port = htons(6666);
rin.sin_addr.s_addr = inet_addr("227.1.2.3");
//4、发送消息
char sbuf[128] = "";
while(1)
{
printf("请输入:");
fgets(sbuf, sizeof(sbuf), stdin);
sbuf[strlen(sbuf)-1] = 0;
//将数据发送出去
sendto(sfd, sbuf, sizeof(sbuf), 0, (struct sockaddr*)&rin, sizeof(rin));
printf("发送成功\n");
}
//5、关闭套接字
close(sfd);
return 0;
}
接收端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int rfd = socket(AF_INET,SOCK_DGRAM,0);
if(rfd == -1)
{
perror("socket success");
return -1;
}
//设置网络属性加入多播组
struct ip_mreqn im;
im.imr_multiaddr.s_addr = inet_addr("227.1.2.3");
im.imr_address.s_addr = inet_addr("192.168.117.108");
im.imr_ifindex = 2;
//设置网络属性
if(setsockopt(rfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&im,sizeof(im))==-1)
{
perror("setsockopt error");
return -1;
}
printf("加入多播组成功\n");
//绑定
struct sockaddr_in rin;
rin.sin_family = AF_INET;
rin.sin_port = htons(6666);
rin.sin_addr.s_addr = inet_addr("227.1.2.3");
if(bind(rfd,(struct sockaddr*)&rin,sizeof(rin))==-1)
{
perror("bind error");
return -1;
}
//接收数据
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
recv(rfd,rbuf,sizeof(rbuf),0);
printf("收到消息为:%s\n",rbuf);
}
close(rfd);
return 0;
}
3、将流式域套接字的服务器端和客户端各实现一遍
服务端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建用于链接的套接字
int sfd = socket(AF_UNIX,SOCK_STREAM,0);
if(sfd == -1)
{
perror("socket error");
return -1;
}
//判断要绑定的套接字是否存在
if(access("./unix",F_OK))
{
//将该文件删除
if(unlink("./unix")==-1)
{
perror("unlink error");
return -1;
}
}
//bind
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1)
{
perror("bind error");
return -1;
}
//监听
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
//等待客户端链接请求
struct sockaddr_un cun;
socklen_t socklen = sizeof(cun);
int newfd = accept(sfd,(struct sockaddr*)&cun,&socklen);
if(newfd == -1)
{
perror("accept error");
return -1;
}
//通信
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
int res = recv(newfd,rbuf,sizeof(rbuf)-1,0);
if(res == 0)
{
printf("客户端下线\n");
return -1;
}
printf("[%s]:%s\n",cun.sun_path,rbuf);
strcat(rbuf,"*_*");
send(newfd,rbuf,strlen(rbuf),0);
printf("发送成功\n");
}
close(newfd);
close(sfd);
return 0;
}
客户端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int cfd = socket(AF_UNIX,SOCK_STREAM,0);
if(cfd == -1)
{
perror("socket error");
return -1;
}
//判断套接字是否存在
if(access("./linux",F_OK)==0)
{
//将该文件删除
if(unlink("./linux")==-1)
{
perror("unlink error");
return -1;
}
}
//bind
struct sockaddr_un cun;
cun.sun_family = AF_UNIX;
strcpy(cun.sun_path,"./linux");
if(bind(cfd,(struct sockaddr*)&cun,sizeof(cun))==-1)
{
perror("bind error");
return -1;
}
//链接服务器
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(connect(cfd,(struct sockaddr*)&sun,sizeof(sun))==-1)
{
perror("connect error");
return -1;
}
//收发数据
char wbuf[128] = "";
while(1)
{
printf("请输入>>>");
fgets(wbuf, sizeof(wbuf), stdin); //从终端上获取一个字符串
wbuf[strlen(wbuf)-1] = '\0'; //将换行换成 '\0'
//判断输入的字符串值
if(strcmp(wbuf, "quit") ==0)
{
break;
}
//将数据发送给服务器
send(cfd, wbuf, strlen(wbuf), 0);
//将字符数组清空
bzero(wbuf, sizeof(wbuf));
recv(cfd, wbuf, sizeof(wbuf)-1, 0);
printf("收到服务器消息为:%s\n", wbuf);
}
//5、关闭套接字
close(cfd);
return 0;
}