C语言两种方法实现进程间 socket 通信

最近写代码需要进程间socket通信,于是上网查了一些资料,自己动手写代码实现了一下,最后发现其实 socket    进程通信与网络通信使用的是统一套接口,只是地址结构与某些参数不同。

第一种实现方案:使用 “127.0.0.1”

客户端程序:

[objc] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <sys/types.h>  
  5. #include <sys/socket.h>  
  6. #include <sys/un.h>  
  7. #include <netinet/in.h>  
  8. #include <unistd.h>  
  9.   
  10. //#define UNIX_DOMAIN "/tmp/UNIX.domain"  
  11. #define DATELEN 1024  
  12.   
  13. int main(int argc, charchar *argv[])  
  14. {  
  15.     int GuiConnect_fd = -1;  
  16.     int iRet = -1;  
  17.     int iRecvLen = 0;  
  18.     int iSendLen = 0;  
  19.     char GuiSendBuf[DATELEN] = {0};  
  20.     char GuiRecvBuf[DATELEN] = {0};  
  21.   
  22.     //static struct sockaddr_un ServAddr;  
  23.     struct sockaddr_in ServAddr;  
  24.   
  25.     //creat unix socket  
  26.     //GuiConnect_fd = socket(PF_UNIX, SOCK_STREAM, 0);  
  27.     GuiConnect_fd = socket(AF_INET, SOCK_STREAM, 0);  
  28.     printf("== GuiConnect_fd = %d\n", GuiConnect_fd);  
  29.   
  30.     if (GuiConnect_fd < 0)  
  31.     {  
  32.         perror("cannot create communication socket");  
  33.         return 1;  
  34.     }  
  35.   
  36.     //ServAddr.sun_family = AF_UNIX;  
  37.     //strncpy(ServAddr.sun_path, UNIX_DOMAIN, sizeof(ServAddr.sun_path) - 1);  
  38.   
  39.     memset(&ServAddr, 0sizeof(ServAddr));  
  40.     ServAddr.sin_family = AF_INET;  
  41.     ServAddr.sin_port = htons(5050);  
  42.     ServAddr.sin_addr.s_addr = inet_addr("127.0.0.1");  
  43.   
  44.     //connect server  
  45.     iRet = connect(GuiConnect_fd, (struct sockaddr*)&ServAddr, sizeof(ServAddr));  
  46.     if(-1 == iRet)  
  47.     {  
  48.         perror("cannot connect to the server");  
  49.         close(GuiConnect_fd);  
  50.         return 1;  
  51.     }  
  52.   
  53.     //receive and send message  
  54.     memset(GuiRecvBuf, 0, DATELEN);  
  55.     printf("GUI Receie Msg from TDC\n");  
  56.     //iRecvLen = read(GuiConnect_fd, GuiRecvBuf, sizeof(GuiRecvBuf));  
  57.     iRecvLen = recv(GuiConnect_fd, GuiRecvBuf, DATELEN, 0);  
  58.     printf("receive message from server (%d) :%s\n", iRecvLen, GuiRecvBuf);  
  59.     printf("GUI Send msg to TDC server:\n");  
  60.     memset(GuiSendBuf, 0, DATELEN);  
  61.     strcpy(GuiSendBuf, "receive message from GUI client\n");  
  62.     GuiSendBuf[strlen(GuiSendBuf)] = '\0';  
  63.     //iSendLen = write(GuiConnect_fd, GuiSendBuf, sizeof(GuiSendBuf));  
  64.     iSendLen = send(GuiConnect_fd, GuiSendBuf, strlen(GuiSendBuf), 0);  
  65.     printf("wrint Date Len to server (%d) : %s\n", iSendLen, GuiSendBuf);  
  66.   
  67.     close(GuiConnect_fd);  
  68.   
  69.     return 0;  
  70. }  
服务端程序:
[objc] view plain copy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <sys/types.h>  
  5. #include <sys/socket.h>  
  6. #include <sys/un.h>  
  7. #include <netinet/in.h>  
  8. #include <unistd.h>  
  9.   
  10. //#define UNIX_DOMAIN "/tmp/UNIX.domain"  
  11. #define MAXClIENT 10  
  12. #define DATELEN 1024 //TDC 和 GUI 进程间通信数据长度  
  13.   
  14. int main(int argc, charchar *argv[])  
  15. {  
  16.     int iRet = -1;  
  17.     int iRecvLen = 0;  
  18.     int iCltAddrLen = 0;  
  19.     int TdcServer_fd = -1;  
  20.     int GuiClient_fd = -1;  
  21.     char TdcRecvBuf[DATELEN] = {0};   
  22.     char TdcSendBuf[DATELEN] = {0};   
  23.     //struct sockaddr_un CltAddr;  
  24.     //struct sockaddr_un SrvAddr;  
  25.     struct sockaddr_in CltAddr;  
  26.     struct sockaddr_in SrvAddr;  
  27.   
  28.     //unlink(UNIX_DOMAIN); //保证没有已经存在的文件  
  29.   
  30.     //creat server socket   
  31.     //TdcServer_fd = socket(PF_UNIX, SOCK_STREAM, 0);  
  32.     TdcServer_fd = socket(AF_INET, SOCK_STREAM, 0);  
  33.     printf(" === TdcServer_fd = %d\n", TdcServer_fd);  
  34.     if (TdcServer_fd < 0)  
  35.     {  
  36.         perror("TDC cannot create communication socket");  
  37.         return 1;  
  38.     }    
  39.   
  40.     //set server addr_param  
  41.     //SrvAddr.sun_family = AF_UNIX;//IPV4  
  42.     //strncpy(SrvAddr.sun_path, UNIX_DOMAIN, sizeof(SrvAddr.sun_path) - 1);  
  43.     memset(&SrvAddr,0,sizeof(SrvAddr));  
  44.     SrvAddr.sin_family = AF_INET;  
  45.     SrvAddr.sin_port = htons(5050);  //这里输入服务器端口号  
  46.     SrvAddr.sin_addr.s_addr = htonl(INADDR_ANY); //INADDR_ANY表示本机IP  
  47.   
  48.     //bind sockfd & addr  
  49.     iRet = bind(TdcServer_fd, (struct sockaddr*)&SrvAddr, sizeof(SrvAddr));  
  50.     if (-1 == iRet)  
  51.     {  
  52.         perror("cannot bind server socket");  
  53.         close(TdcServer_fd);  
  54.         // unlink(UNIX_DOMAIN);  
  55.         return 1;  
  56.     }  
  57.   
  58.     //listen sockfd   
  59.     iRet = listen(TdcServer_fd, MAXClIENT);  
  60.     if (-1 == iRet)  
  61.     {  
  62.         perror("cannot listen the client connect request");  
  63.         close(TdcServer_fd);  
  64.         // unlink(UNIX_DOMAIN);  
  65.         return 1;  
  66.     }  
  67.   
  68.     //have connect request use accept  
  69.     iCltAddrLen = sizeof(CltAddr);  
  70.   
  71.     while(1)  
  72.     {  
  73.         GuiClient_fd = accept(TdcServer_fd, (struct sockaddr*)&CltAddr, &iCltAddrLen);  
  74.         printf("============== GuiClient_fd = %d\n", GuiClient_fd);  
  75.         if(GuiClient_fd < 0)  
  76.         {  
  77.             perror("cannot accept client connect request");  
  78.             close(TdcServer_fd);  
  79.             // unlink(UNIX_DOMAIN);  
  80.             return 1;  
  81.         }  
  82.   
  83.         //read and printf sent client info  
  84.         memset(TdcSendBuf, 0, DATELEN);  
  85.         strcpy(TdcSendBuf, "you have connected to TDC succeed, NOW Receive msg from server\n");  
  86.         //write(GuiClient_fd, TdcSendBuf, sizeof(TdcSendBuf));  
  87.         printf("== GuiClient_fd = %d\n", GuiClient_fd);  
  88.         send(GuiClient_fd, TdcSendBuf, strlen(TdcSendBuf), 0);  
  89.         memset(TdcRecvBuf, 0, DATELEN);  
  90.         //sleep(5);  
  91.         //iRecvLen = read(GuiClient_fd, TdcRecvBuf, sizeof(TdcRecvBuf));  
  92.         printf("receiveing .....\n");  
  93.   
  94.         while (1)  
  95.         {  
  96.             iRecvLen = recv(GuiClient_fd, TdcRecvBuf, DATELEN, 0);  
  97.             printf("Message from client (%d)) :%s/n", iRecvLen, TdcRecvBuf);  
  98.         }  
  99.     }  
  100.   
  101.     close(GuiClient_fd);  
  102.     close(TdcServer_fd);  
  103.     //unlink(UNIX_DOMAIN);  
  104.   
  105.     return 0;  
  106. }  

第二种方法实现:使用unix域本地socket (个人简单理解为本地读写文件)

客户端程序:

[objc] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <sys/types.h>  
  5. #include <sys/socket.h>  
  6. #include <sys/un.h>  
  7. #include <netinet/in.h>  
  8. #include <unistd.h>  
  9.   
  10. #define UNIX_DOMAIN "/tmp/UNIX.domain"  
  11. #define DATELEN 1024  
  12.   
  13. int main(int argc, charchar *argv[])  
  14. {  
  15.     int GuiConnect_fd = -1;  
  16.     int iRet = -1;  
  17.     int iRecvLen = 0;  
  18.     int iSendLen = 0;  
  19.     char GuiSendBuf[DATELEN] = {0};  
  20.     char GuiRecvBuf[DATELEN] = {0};  
  21.   
  22.     static struct sockaddr_un ServAddr;  
  23.     //struct sockaddr_in ServAddr;  
  24.   
  25.     //creat unix socket  
  26.     GuiConnect_fd = socket(PF_UNIX, SOCK_STREAM, 0);  
  27.     //GuiConnect_fd = socket(AF_INET, SOCK_STREAM, 0);  
  28.     printf("== GuiConnect_fd = %d\n", GuiConnect_fd);  
  29.   
  30.     if (GuiConnect_fd < 0)  
  31.     {  
  32.         perror("cannot create communication socket");  
  33.         return 1;  
  34.     }  
  35.   
  36.     ServAddr.sun_family = AF_UNIX;  
  37.     strncpy(ServAddr.sun_path, UNIX_DOMAIN, sizeof(ServAddr.sun_path) - 1);  
  38.   
  39.     //memset(&ServAddr, 0, sizeof(ServAddr));  
  40.     //ServAddr.sin_family = AF_INET;  
  41.     //ServAddr.sin_port = htons(5050);  
  42.     //ServAddr.sin_addr.s_addr = inet_addr("127.0.0.1");  
  43.   
  44.     //connect server  
  45.     iRet = connect(GuiConnect_fd, (struct sockaddr*)&ServAddr, sizeof(ServAddr));  
  46.     if(-1 == iRet)  
  47.     {  
  48.         perror("cannot connect to the server");  
  49.         close(GuiConnect_fd);  
  50.         return 1;  
  51.     }  
  52.   
  53.     //receive and send message  
  54.     memset(GuiRecvBuf, 0, DATELEN);  
  55.     printf("GUI Receie Msg from TDC\n");  
  56.     //iRecvLen = read(GuiConnect_fd, GuiRecvBuf, sizeof(GuiRecvBuf));  
  57.     iRecvLen = recv(GuiConnect_fd, GuiRecvBuf, DATELEN, 0);  
  58.     printf("receive message from server (%d) :%s\n", iRecvLen, GuiRecvBuf);  
  59.     printf("GUI Send msg to TDC server:\n");  
  60.     memset(GuiSendBuf, 0, DATELEN);  
  61.     strcpy(GuiSendBuf, "receive message from GUI client\n");  
  62.     GuiSendBuf[strlen(GuiSendBuf)] = '\0';  
  63.     //iSendLen = write(GuiConnect_fd, GuiSendBuf, sizeof(GuiSendBuf));  
  64.     iSendLen = send(GuiConnect_fd, GuiSendBuf, strlen(GuiSendBuf), 0);  
  65.     printf("wrint Date Len to server (%d) : %s\n", iSendLen, GuiSendBuf);  
  66.   
  67.     close(GuiConnect_fd);  
  68.   
  69.     return 0;  
  70. }  
服务端程序:

[objc] view plain copy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <sys/types.h>  
  5. #include <sys/socket.h>  
  6. #include <sys/un.h>  
  7. #include <netinet/in.h>  
  8. #include <unistd.h>  
  9.   
  10. #define UNIX_DOMAIN "/tmp/UNIX.domain"  
  11. #define MAXClIENT 10  
  12. #define DATELEN 1024 //TDC 和 GUI 进程间通信数据长度  
  13.   
  14. int main(int argc, charchar *argv[])  
  15. {  
  16.     int iRet = -1;  
  17.     int iRecvLen = 0;  
  18.     int iCltAddrLen = 0;  
  19.     int TdcServer_fd = -1;  
  20.     int GuiClient_fd = -1;  
  21.     char TdcRecvBuf[DATELEN] = {0};   
  22.     char TdcSendBuf[DATELEN] = {0};   
  23.     struct sockaddr_un CltAddr;  
  24.     struct sockaddr_un SrvAddr;  
  25.     //struct sockaddr_in CltAddr;  
  26.     //struct sockaddr_in SrvAddr;  
  27.   
  28.     unlink(UNIX_DOMAIN); //保证没有已经存在的文件  
  29.   
  30.     //creat server socket   
  31.     TdcServer_fd = socket(PF_UNIX, SOCK_STREAM, 0);  
  32.     //TdcServer_fd = socket(AF_INET, SOCK_STREAM, 0);  
  33.     printf(" === TdcServer_fd = %d\n", TdcServer_fd);  
  34.     if (TdcServer_fd < 0)  
  35.     {  
  36.         perror("TDC cannot create communication socket");  
  37.         return 1;  
  38.     }    
  39.   
  40.     //set server addr_param  
  41.     SrvAddr.sun_family = AF_UNIX;//IPV4  
  42.     strncpy(SrvAddr.sun_path, UNIX_DOMAIN, sizeof(SrvAddr.sun_path) - 1);  
  43.     //memset(&SrvAddr,0,sizeof(SrvAddr));  
  44.     //SrvAddr.sin_family = AF_INET;  
  45.     //SrvAddr.sin_port = htons(5050);  //这里输入服务器端口号  
  46.     //SrvAddr.sin_addr.s_addr = htonl(INADDR_ANY); //INADDR_ANY表示本机IP  
  47.   
  48.     //bind sockfd & addr  
  49.     iRet = bind(TdcServer_fd, (struct sockaddr*)&SrvAddr, sizeof(SrvAddr));  
  50.     if (-1 == iRet)  
  51.     {  
  52.         perror("cannot bind server socket");  
  53.         close(TdcServer_fd);  
  54.         unlink(UNIX_DOMAIN);  
  55.         return 1;  
  56.     }  
  57.   
  58.     //listen sockfd   
  59.     iRet = listen(TdcServer_fd, MAXClIENT);  
  60.     if (-1 == iRet)  
  61.     {  
  62.         perror("cannot listen the client connect request");  
  63.         close(TdcServer_fd);  
  64.         unlink(UNIX_DOMAIN);  
  65.         return 1;  
  66.     }  
  67.   
  68.     //have connect request use accept  
  69.     iCltAddrLen = sizeof(CltAddr);  
  70.   
  71.     while(1)  
  72.     {  
  73.         GuiClient_fd = accept(TdcServer_fd, (struct sockaddr*)&CltAddr, &iCltAddrLen);  
  74.         printf("============== GuiClient_fd = %d\n", GuiClient_fd);  
  75.         if(GuiClient_fd < 0)  
  76.         {  
  77.             perror("cannot accept client connect request");  
  78.             close(TdcServer_fd);  
  79.             unlink(UNIX_DOMAIN);  
  80.             return 1;  
  81.         }  
  82.   
  83.         //read and printf sent client info  
  84.         memset(TdcSendBuf, 0, DATELEN);  
  85.         strcpy(TdcSendBuf, "you have connected to TDC succeed, NOW Receive msg from server\n");  
  86.         //write(GuiClient_fd, TdcSendBuf, sizeof(TdcSendBuf));  
  87.         printf("== GuiClient_fd = %d\n", GuiClient_fd);  
  88.         send(GuiClient_fd, TdcSendBuf, strlen(TdcSendBuf), 0);  
  89.         memset(TdcRecvBuf, 0, DATELEN);  
  90.         //iRecvLen = read(GuiClient_fd, TdcRecvBuf, sizeof(TdcRecvBuf));  
  91.         printf("receiveing .....\n");  
  92.   
  93.         //while (1)  
  94.         {  
  95.             iRecvLen = recv(GuiClient_fd, TdcRecvBuf, DATELEN, 0);  
  96.             printf("Message from client (%d)) :%s/n", iRecvLen, TdcRecvBuf);  
  97.         }  
  98.     }  
  99.   
  100.     close(GuiClient_fd);  
  101.     close(TdcServer_fd);  
  102.     unlink(UNIX_DOMAIN);  
  103.   
  104.     return 0;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值