进程三种通信方式
第一种套接字通信
服务器端
#include
#define IP "192.168.31.243"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建连接套接字
int sfd=-1;
if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
return -1;
}
printf("套接字创建成功\n");
//绑定地址信息结构体
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))==-1)
{
perror("bind error");
return -1;
}
printf("绑定成功\n");
//开启监听状态
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
printf("监听成功\n");
//定义用于接收客户端地址信息的结构体
struct sockaddr_in cin;
socklen_t socklen=sizeof(cin);
int newfd=-1;
if((newfd=accept(sfd,(struct sockaddr*)&cin,&socklen))==-1)
{
perror("accept error");
return -1;
}
printf("连接成功\n");
//收发数据
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
int res=recv(newfd,rbuf,sizeof(rbuf),0);
if(res==0)
{
printf("客户端下线\n");
break;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
}
//关闭套接字
close(newfd);
close(sfd);
return 0;
}
客户端代码
#include
#define IP "192.168.31.243"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建通信套接字
int cfd=-1;
if((cfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
return -1;
}
printf("创建成功\n");
//设置端口号快速重用
int reuse=1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("setsockopt error");
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(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
printf("连接服务器成功\n");
//收发数据
char wbuf[128]="";
while(1)
{
bzero(wbuf,sizeof(wbuf));
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
send(cfd,wbuf,sizeof(wbuf),0);
/* int res=recv(cfd,wbuf,sizeof(wbuf),0);
if(res==0)
{
printf("服务器已下线\n");
break;
}*/
}
//关闭套接字
close(cfd);
return 0;
}
第二种方式
无名管道
#include
int main(int argc, const char *argv[])
{
pid_t pid=-1;
int pipefd[2]={0};
if(pipe(pipefd)==-1)
{
perror("pipe error");
return -1;
}
//创建一个子进程
pid=fork();
if(pid>0)
{
//父进程
close(pipefd[0]);//关闭读端
char buf[128]="";
while(1)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
write(pipefd[1],buf,sizeof(buf));//写数据
if(strcmp(buf,"quit")==0)
{
break;
}
}
close(pipefd[1]);//关闭写端
}
else if(pid==0)
{
//子进程
close(pipefd[1]);//关闭写端
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
read(pipefd[0],rbuf,sizeof(rbuf));
printf("收到消息%s\n",rbuf);
if(strcmp(rbuf,"quit")==0)
{
break;
}
}
close(pipefd[0]);//关闭读端
exit(EXIT_SUCCESS);
}
else
{
perror("fork error");
return -1;
}
wait(NULL);
return 0;
}
第三种消息队列
接收端
#include
#define SIZE (sizeof(struct msgbuf)-sizeof(long))
struct msgbuf
{
long mtype;
char mtext[1024];
};
int main(int argc, const char *argv[])
{
//创建key值
key_t key=ftok("/",'l');
if(key==-1)
{
perror("ftok error");
return -1;
}
printf("key=%#x\n",key);
//创建消息队列
int msgid=msgget(key,IPC_CREAT|0664);
if(msgid==-1)
{
perror("msgget error");
return -1;
}
printf("msgid=%d\n",msgid);
//向消息队列存放数据
struct msgbuf buf;
while(1)
{
printf("请输入你要发送的类型");
scanf("%ld",&buf.mtype);
printf("请输入罅隙内容");
scanf("%s",buf.mtext);
msgsnd(msgid,&buf,SIZE,0);
if(strcmp(buf.mtext,"quit")==0)
{
break;
}
}
if(msgctl(msgid,IPC_RMID,NULL)==-1)
{
perror("msgctl error");
return -1;
}
return 0;
}
发送端
#include
#define SIZE (sizeof(struct msgbuf)-sizeof(long))
struct msgbuf
{
long mtype;
char mtext[1024];
};
int main(int argc, const char *argv[])
{
key_t key=ftok("/",'l');
if(key==-1)
{
perror("key error");
return -1;
}
printf("key=%#x\n",key);
//创建消息队列
int msgid=msgget(key,IPC_CREAT|0664);
if(msgid==-1)
{
perror("msgget error");
return -1;
}
printf("msgid=%d\n",msgid);
//从消息队列中读取数据
struct msgbuf buf;
while(1)
{
msgrcv(msgid,&buf,SIZE,0,0);
printf("收到消息%s\n",buf.mtext);
if(strcmp(buf.mtext,"quit")==0)
{
break;
}
}
return 0;
}