socket实现进程间通信

本文介绍了一个使用socket进行本地进程间通信的例子。通过一个简单的服务端和客户端程序演示了如何利用socket进行数据交换,并展示了从创建socket到数据读写的完整过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

             进程间的通信方式有多种,今天来学一下socket实现进程间通信,听说这种通信方式现在用的最多,看代码吧。

 

[mapan@localhost TCP]$ ls
client.cpp  makefile  server.cpp
[mapan@localhost TCP]$ cat server.cpp 
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/un.h>
#define MAXLINE 4096
#define UNIXSTR_PATH "/tmp/unix.str"	


int main()
{
   int listenfd,connfd;
   socklen_t  clilen;
   struct sockaddr_un cliaddr,servaddr;
   char recvbuf[20]={0};

   listenfd=socket(AF_LOCAL,SOCK_STREAM,0);

   unlink(UNIXSTR_PATH);
   bzero(&servaddr,sizeof(servaddr));
   servaddr.sun_family=AF_LOCAL;
   strcpy(servaddr.sun_path,UNIXSTR_PATH);

   bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));  
   listen(listenfd,5);


   clilen=sizeof(cliaddr);
   connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
     
   printf("pid=%d\n",getpid());
   read(connfd,recvbuf,sizeof(recvbuf));
   printf("recvbuf=%s\n",recvbuf); 
   
   getchar();
   close(connfd);
   close(listenfd);
   return 0;
}
[mapan@localhost TCP]$ cat client.cpp 
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/un.h>
#define MAXLINE 4096
#define	UNIXSTR_PATH	"/tmp/unix.str"	



int main()
{
   int sockfd;
   struct sockaddr_un servaddr;
   char sendbuf[20]="1111111";

   sockfd=socket(AF_LOCAL,SOCK_STREAM,0);
   bzero(&servaddr,sizeof(servaddr));
   servaddr.sun_family=AF_LOCAL;
   strcpy(servaddr.sun_path,UNIXSTR_PATH); 

   int ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
   write(sockfd,sendbuf,strlen(sendbuf));
  

   getchar();
   close(sockfd);
   return 0;
}
[mapan@localhost TCP]$ cat makefile 
all:server client

server.o:server.cpp
	g++ -c server.cpp
client.o:client.cpp
	g++ -c client.cpp
server:server.o
	g++ -o server server.o
client:client.o
	g++ -o client client.o

clean:
	rm -f server client *.o
[mapan@localhost TCP]$ 


编译运行,运行服务端,打开另一个窗口运行客户端。

 

 

[mapan@localhost TCP]$ make
g++ -c server.cpp
g++ -o server server.o
g++ -c client.cpp
g++ -o client client.o
[mapan@localhost TCP]$ ./server 
pid=22701
recvbuf=1111111

 

通信成功,sockaddr_un本地通讯的套接字结构,它有2个参数:sun_family,sun_path。最重要的还是实践,其他零星知识点网上很多我就不赘述了。

 

 

参考资料:unix网络编程 卷一

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盼盼编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值