本文主要是介绍广播,组播,多进程并发通信,多线程并发通信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
广播服务器搭建:
#include <myhead.h>
#define PORT 8888
#define IP "192.168.124.255"
int main(int argc, const char *argv[])
{
//创建流套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
fprintf(stderr,"line:%d ",__LINE__);
perror("socket");
return -1;
}
printf("创建流套接字成功 sfd=%d __%d__\n",sfd,__LINE__);
//允许端口号被复用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("setsockopt");
return -1;
}
//填充接收方地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定服务器自身的地址信息
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//存储数据包发送的地址信息
char buf[128] = "";
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
while(1){
bzero(buf,sizeof(buf));
//接收
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("recvfrom");
return -1;
}
printf("[%s:%d]:%s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf,__LINE__);
}
close(sfd);
return 0;
}
客户端搭建:
#include <myhead.h>
#define PORT 8888
#define IP "192.168.124.255"
int main(int argc, const char *argv[])
{
//创建流套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0){
fprintf(stderr,"line:%d ",__LINE__);
perror("socket");
return -1;
}
printf("creat ok cfd=%d __%d__",cfd,__LINE__);
//设置允许广播
int broad = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad)) < 0){
fprintf(stderr,"line:%d ",__LINE__);
perror("socket");
return -1;
}
printf("允许广播成功\n");
//填充接收方地址信息
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(PORT);
cin.sin_addr.s_addr = inet_addr(IP);
//发送
char buf[128];
struct sockaddr_in rcvaddr;
socklen_t addrlen = sizeof(cin);
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*)&cin,sizeof(cin)) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("sendto");
return -1;
}
printf("发送成功\n");
}
close(cfd);
return 0;
}
组播服务器搭建:
#include <myhead.h>
#define PORT 8888
#define IP "192.168.124.73"
#define GRP_IP "224.1.2.3"
int main(int argc, const char *argv[])
{
//创建流套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
fprintf(stderr,"line:%d ",__LINE__);
perror("socket");
return -1;
}
printf("创建流套接字成功 sfd=%d __%d__\n",sfd,__LINE__);
//允许端口号被复用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("setsockopt");
return -1;
}
//加入多播组
struct ip_mreqn mq;
mq.imr_multiaddr.s_addr = inet_addr(GRP_IP); //加入组播IP
mq.imr_address.s_addr = inet_addr(IP);
mq.imr_ifindex = 0; //网络设备索引号
if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mq,sizeof(mq)) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("setsockopt");
return -1;
}
printf("加入小组成功[%s]\n",GRP_IP);
//填充服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定服务器自身的地址信息
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//存储数据包发送的地址信息
char buf[128] = "";
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
while(1){
bzero(buf,sizeof(buf));
//接收
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("recvfrom");
return -1;
}
printf("[%s:%d]:%s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf,__LINE__);
}
close(sfd);
return 0;
}
组播客户端搭建:
#include <myhead.h>
#define PORT 8888
#define IP "224.1.2.3"
int main(int argc, const char *argv[])
{
//创建流套接字 int cfd = socket(AF_INET,SOCK_DGRAM,0);
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0){
fprintf(stderr,"line:%d ",__LINE__);
perror("socket");
return -1;
}
printf("creat ok cfd=%d __%d__",cfd,__LINE__);
//允许端口被复用
int reuse = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("setsockopt");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//发送
char buf[128];
struct sockaddr_in rcvaddr;
socklen_t addrlen = sizeof(sin);
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){
fprintf(stderr,"line=%d",__LINE__);
perror("sendto");
return -1;
}
printf("发送成功\n");
}
close(cfd);
return 0;
}
多进程通信(TCP):
#include <myhead.h>
#define IP "192.168.124.73"
#define PORT 8888
void recycle_zombie()
{
while(waitpid(-1,NULL,WNOHANG)<0);
}
int do_cli_msg(int newfd,struct sockaddr_in cin);
int main(int argc, const char *argv[])
{
//捕获17号信号
if(signal(17,recycle_zombie) == SIG_ERR){
fprintf(stderr,"line=%d",__LINE__);
perror("signal");
return -1;
}
//创建流式套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("socket");
return -1;
}
printf("创建流式套接字成功 sfd=%d __%d__\n",sfd,__LINE__);
//允许端口号被重复使用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("setsockopt");
return -1;
}
//填充服务器的地址信息结构体,真实的地址信息结构体根据地址族制定
//AF_INET -->man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定服务器自身的地址信息
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0){
fprintf(stderr,"line:%d",__LINE__);
perror("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//套接字设置为被动监听
if(listen(sfd,128) < 0 ){
fprintf(stderr,"line:%d",__LINE__);
perror("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
//获取链接成功的套接字
struct sockaddr_in cin; //存储客户端信息
socklen_t addrlen = sizeof(cin);
//会从已经完成链接队列的对头获取一个客户端的信息,生成一个新的文件描述符
//accept(sfd,NULL,NULL)
int newfd; //接收
pid_t pid = 0;
while(1){
newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd < 0){
fprintf(stderr,"line:%d",__LINE__);
perror("accept");
return -1;
}
printf("[%s:%d] 客户端链接成功 newfd=%d __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
pid = fork();
if(pid == 0){
close(sfd);
do_cli_msg(newfd,cin);
close(newfd);
exit(0);
}
else if(pid > 0){
close(newfd);
}
else{
fprintf(stderr,"line:%d",__LINE__);
perror("fork");
return -1;
}
}
//关闭文件描述符
close(sfd);
return 0;
}
int do_cli_msg(int newfd,struct sockaddr_in cin)
{
char buf[128];
ssize_t res;
while(1){
bzero(buf,sizeof(buf));
//接收
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0){
fprintf(stderr,"line:%d",__LINE__);
perror("recv");
return -1;
}
else if(res == 0){
printf("[%s:%d] 客户端断开 newfd=%d :%s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);
break;
}
printf("[%s:%d]newfd=%d :%s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);
//发送
strcat(buf," I GET IT!");
if(send(newfd,buf,sizeof(buf),0) < 0){
fprintf(stderr,"line:%d",__LINE__);
perror("send");
return -1;
}
printf("send success __%d__\n",__LINE__);
}
return 0;
}
多线程通信:
#include <myhead.h>
#define IP "192.168.124.73"
#define PORT 8888
struct CliInfo{
int newfd;
struct sockaddr_in cin;
};
void* do_cli_msg(void* arg);
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("socket");
return -1;
}
printf("创建流式套接字成功 sfd=%d __%d__\n",sfd,__LINE__);
//允许端口号被重复使用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
fprintf(stderr,"line=%d",__LINE__);
perror("setsockopt");
return -1;
}
//填充服务器的地址信息结构体,真实的地址信息结构体根据地址族制定
//AF_INET -->man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定服务器自身的地址信息
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0){
fprintf(stderr,"line:%d\n",__LINE__);
perror("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//套接字设置为被动监听
if(listen(sfd,128) < 0 ){
fprintf(stderr,"line:%d",__LINE__);
perror("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
//获取链接成功的套接字
struct sockaddr_in cin; //存储客户端信息
socklen_t addrlen = sizeof(cin);
//会从已经完成链接队列的对头获取一个客户端的信息,生成一个新的文件描述符
//accept(sfd,NULL,NULL)
int newfd; //接收
pthread_t tid;
struct CliInfo dcli;
while(1){
newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd < 0){
fprintf(stderr,"line:%d",__LINE__);
perror("accept");
return -1;
}
printf("[%s:%d] 客户端链接成功 newfd=%d __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
//运行到此时则代表客户端链接成功,需要创建一个分支线程负责交互
//分之线程负责交互
dcli.newfd = newfd;
dcli.cin = cin;
if(pthread_create(&tid,NULL,do_cli_msg,(void*)&dcli) != 0){
fprintf(stderr,"line:%d pthread_create failed\n",__LINE__);
return -1;
}
pthread_detach(tid);
}
//关闭文件描述符
close(sfd);
return 0;
}
void* do_cli_msg(void* arg)
{
int newfd = ((struct CliInfo*)arg)->newfd;
struct sockaddr_in cin = ((struct CliInfo*)arg)->cin;
char buf[128];
ssize_t res;
while(1){
bzero(buf,sizeof(buf));
//接收
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0){
fprintf(stderr,"line:%d",__LINE__);
perror("recv");
break;;
}
else if(res == 0){
printf("[%s:%d] 客户端断开 newfd=%d :%s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);
break;
}
printf("[%s:%d]newfd=%d :%s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);
//发送
strcat(buf," I GET IT!");
if(send(newfd,buf,sizeof(buf),0) < 0){
fprintf(stderr,"line:%d",__LINE__);
perror("send");
break;
}
printf("send success __%d__\n",__LINE__);
}
close(newfd);
pthread_exit(NULL);
}
这篇关于广播,组播,多进程并发通信,多线程并发通信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!