当前位置:   article > 正文

Linux本地套接字(Unix域套接字)----SOCK_DGRAM方式_socket(af_unix, sock_dgram, 0)

socket(af_unix, sock_dgram, 0)

目录

 

简述

创建服务端代码:

创建客户端代码

接收函数封装

发送封装

服务端测试main函数

客户端测试main函数

编译运行结果


简述

这里介绍一下Linux进程间通信的socket方式---Local socket。这篇主要是介绍下SOCK_DGRAM方式的通信,即数据包的方式(与UDP类似),面向无连接。

这个代码是我刚开始学的时候写的,代码比较简单,适合初学,学习最快的方式就是直接拿源码修改、编译运行、调试。

完整源代码:https://gitee.com/fensnote/demo_code/tree/master/Linux/unix_socket

 

创建服务端代码:

  1. int startServer()
  2. {
  3. int iRet;
  4. TSockAddrUn serv_unadr;
  5. TSockAddrIn serv_inadr;
  6. TSockAddr *pSockAddr = NULL;
  7. bzero(&serv_unadr,sizeof(serv_unadr));
  8. bzero(&serv_inadr,sizeof(serv_inadr));
  9. serv_unadr.sun_family = AF_UNIX;
  10. strcpy(serv_unadr.sun_path,UNIX_SOCKET_PATH);
  11. pSockAddr = (TSockAddr *)&serv_unadr;
  12. signal(SIGPIPE, SIG_IGN);
  13. /* 创建本地socket */
  14. sockFd = socket(AF_UNIX, SOCK_DGRAM, 0);//数据包方式
  15. if ( sockFd <= 0)
  16. {
  17. perror("socket error");
  18. return sockFd;
  19. }
  20. /* 绑定监听口 */
  21. int flag = 1;
  22. iRet = setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
  23. setSocketAttr(sockFd);
  24. unlink(UNIX_SOCKET_PATH);
  25. iRet = bind(sockFd, pSockAddr, sizeof(TSockAddr));
  26. if (iRet != 0)
  27. {
  28. perror("bind error");
  29. close(sockFd);
  30. return -1;
  31. }
  32. return sockFd;
  33. }

创建客户端代码

面向无连接的方式,和服务端的代码差别不大:

  1. int InitUdpClient()
  2. {
  3. TSockAddrUn unadr;
  4. TSockAddr *pSockAddr = NULL;
  5. bzero(&unadr,sizeof(unadr));
  6. char tmpPath[] = "/tmp/unix_XXXX";
  7. char *tmpName = mktemp(tmpPath);
  8. unadr.sun_family = AF_LOCAL;
  9. strcpy(unadr.sun_path, tmpName);
  10. pSockAddr = (TSockAddr *)&unadr;
  11. /* 创建本地socket */
  12. sockFd = socket(AF_LOCAL, SOCK_DGRAM, 0);//数据包方式
  13. if ( sockFd <= 0)
  14. {
  15. perror("CUdpClient:: socket error");
  16. return sockFd;
  17. }
  18. unlink(tmpPath);
  19. /* 绑定监听口 */
  20. //setSocketAttr(sockFd);
  21. int iRet = bind(sockFd,pSockAddr, sizeof(TSockAddr));
  22. if (iRet != 0)
  23. {
  24. perror("bind error");
  25. close(sockFd);
  26. return -1;
  27. }
  28. return sockFd;
  29. }

接收函数封装

  1. //返回0 超时 timeOut-超时时间
  2. int UnixRead(char *recvBuf, int len, int timeOut)
  3. {
  4. int nRead = readable_timeo(sockFd, timeOut);
  5. if ( nRead <= 0 )
  6. {
  7. printf("UnixRead, read time out!\n");
  8. return 0;
  9. }
  10. pSockAddr = (TSockAddr *)&unClientaddr;
  11. socklen = sizeof(TSockAddrUn);
  12. bzero(recvBuf, len);
  13. nRead = recvfrom(sockFd, recvBuf, len, 0, pSockAddr, &socklen);
  14. if ( nRead <= 0 )
  15. {
  16. if ( (EAGAIN == errno) || (EINTR == errno))
  17. {
  18. return 0; //接收连接超时
  19. }
  20. perror("UnixRead read error:");
  21. }
  22. return nRead;
  23. }

发送封装

  1. int UnixSend(const void *data, int len)
  2. {
  3. TSockAddrUn unadr;
  4. TSockAddr *pSockAddr = NULL;
  5. bzero(&unadr,sizeof(unadr));
  6. unadr.sun_family = AF_LOCAL;
  7. strcpy(unadr.sun_path, UNIX_SOCKET_PATH);
  8. pSockAddr = (TSockAddr *)&unadr;
  9. socklen_t socklen = sizeof(TSockAddrUn);
  10. return sendto(sockFd, data, len, 0, pSockAddr, socklen);
  11. }

服务端测试main函数

  1. int main()
  2. {
  3. startServer();
  4. int nRead = 0;
  5. char recvBuf[1024] = {0};
  6. while(1)
  7. {
  8. nRead = UnixRead(recvBuf, 1024, 5);
  9. if ( nRead <= 0 )
  10. {
  11. continue;
  12. }
  13. else
  14. {
  15. printf("recv %d data: %s\n",nRead, recvBuf);
  16. const char *sendMsg = "svr ack!";
  17. UnixSend(sendMsg, strlen(sendMsg));
  18. }
  19. sleep(1);
  20. }
  21. return 0;
  22. }

客户端测试main函数

  1. int main( )
  2. {
  3. int sockFd = InitUdpClient();
  4. int nRead = 0;
  5. const char *sendMsg = "hello";
  6. char recvBuf[1024] = {0};
  7. while(1)
  8. {
  9. nRead = UnixSend(sendMsg, strlen(sendMsg));
  10. printf("send %d data: %s\n", nRead, sendMsg);
  11. nRead = UnixRead(recvBuf, 1024, 5);
  12. printf("recv %d data: %s\n", nRead, recvBuf);
  13. sleep(2);
  14. }
  15. return 0;
  16. }

编译运行结果

左边是服务端,右边是客户端。

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/158813
推荐阅读
相关标签
  

闽ICP备14008679号