0


嵌入式学习——3——域套接字UNIX

1、域套接字UNIX

1、域套接字是最原始的套接字通信方式,是完成同一主机之间多个进程间通信

2、由于不需要跨主机进行通信了,那么就无需使用ip地址和端口号了

3、通信本质:依然使用的是内核空间

4、域套接字的通信介质为套接字文件 bcd-lsp

5、域套接字也分为流式域套接字报式域套接字

6、跟网络通信中相关函数的区别

#include <sys/types.h>

#include <sys/socket.h>

**int socket(int domain, int type, int protocol); **

功能:为网络通信提供一个端点,并返回该端点的文件描述符,文件描述符使用原则为最小未分配原则

参数:

domain:通信域,可以选择协议族

  1. Name Purpose Man page
  2. **AF_UNIX, AF_LOCAL** 非跨主机通信 详细信息请看 man 7 unix

**type: **指定通信语义,常用的通信语义如下

  1. SOCK_STREAM 支持传输层的TCP通信方式
  2. SOCK_DGRAM 支持传输层的UDP通信方式
  3. SOCK_RAW 支持原始套接字通信

**protocol: **当第二个参数中没有指定对应传输协议时,第三个参数要指定,如果第二个参数中已经确定了传输协议,那么第三个参数填0即可

  1. 1.如果参数**type**是 SOCK_STREAMSOCK_DGRAM时,参数**protocol**可以填0
  2. 2.如果参数**type**没有指定是TCP还是UDP通信时,第三个参数**protocol**可以使用 IPPROTO_TCPIPPROTO_UDP来确定

返回值:成功返回一个新的套接字文件描述符,失败返回-1 并置位错误码

7、地址信息结构体的不同

#include <sys/types.h>

#include <sys/socket.h>

*int bind(int sockfd, const struct sockaddr addr, socklen_t addrlen);

功能:为给定的套接字文件描述符绑定IP地址和端口号

参数:

sockfd :要被绑定的套接字文件描述符

**addr : **通用地址信息结构体,实际的地址信息,取决于地址族,

  1. AF_INET 参见 man 7 ip ,
  2. AF_UNIX 参见 man 7 unix

对于本地通信的套接字地址信息结构体:

  1. ** struct sockaddr_un { **

** sa_family_t sun_family; /* 通信域 */ **

** char sun_path[108]; /* 套接字文件路径 */ **

** };**

** /* 套接字文件路径 / 必须是不存在的文件*

**addrlen: addr **的大小

返回值: 成功 0,失败 -1 并置位错误码

8、判断文件是否存在,以及删除文件

#include <unistd.h>

*int access(const char pathname, int mode);

功能:判断给定的文件是否具有给定的权限

参数1:要判断的文件路径

参数2:要判断的权限

  1. R_OK:读权限
  2. W_OK:写权限
  3. X_OK:可执行权限
  4. F_OK:是否存在

返回值:如果文件存在并具有要求的所有权限,则返回0,否则返回-1并置位错误码

*int unlink(const char pathname);

功能:删除指定的文件

参数:要删除的文件路径

返回值:成功 0,失败 -1 并置位错误码

2、流式域套接字

2.1 服务器端实现

  1. int main()
  2. {
  3. //1、为通信创建一个端点
  4. int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
  5. if(sfd == -1)
  6. {
  7. perror("socket error");
  8. return -1;
  9. }
  10. //判断要绑定的套接字文件是否存在?
  11. if(access("./unix", F_OK) == 0)
  12. {
  13. //说明文件存在,要将其删除
  14. if(unlink("./unix") ==-1)
  15. {
  16. perror("unlink error");
  17. return -1;
  18. }
  19. }
  20. //2、绑定ip和端口号
  21. //2.1 准备地址信息结构体
  22. struct sockaddr_un sun;
  23. sun.sun_family = AF_UNIX; //通信域
  24. strcpy(sun.sun_path , "./unix"); //套接字文件路径
  25. //2.2 绑定工作:必须绑定一个不存在的套接字文件
  26. if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) ==-1)
  27. {
  28. perror("bind error");
  29. return -1;
  30. }
  31. printf("bind success\n");
  32. //3、将套接字设置成被动监听状态
  33. if(listen(sfd, 128)==-1)
  34. {
  35. perror("listen error");
  36. return -1;
  37. }
  38. printf("listen success\n");
  39. //4、阻塞等待客户端的连接
  40. //4.1 定义用于接受客户端信息的容器
  41. struct sockaddr_un cun;
  42. socklen_t addrlen = sizeof(cun);
  43. int newfd = accept(sfd, (struct sockaddr*)&cun, &addrlen);
  44. if(newfd == -1)
  45. {
  46. perror("accept error");
  47. return -1;
  48. }
  49. printf("[%s]:发来连接请求\n", cun.sun_path);
  50. //5、与客户端进行相互通信
  51. char rbuf[128] = ""; //读取消息内容的容器
  52. while(1)
  53. {
  54. //清空容器
  55. bzero(rbuf, sizeof(rbuf));
  56. //从套接字中读取数据
  57. //int res = read(newfd, rbuf, sizeof(rbuf));
  58. int res = recv(newfd, rbuf, sizeof(rbuf), 0);
  59. if(res == 0)
  60. {
  61. printf("客户端已经下线\n");
  62. break;
  63. }
  64. //将读取的消息展示出来
  65. printf("[%s]:%s\n", cun.sun_path, rbuf);
  66. //将收到的消息处理一下,回复给客户端
  67. strcat(rbuf, "*_*");
  68. //讲消息发送给客户端
  69. send(newfd, rbuf, strlen(rbuf), 0);
  70. printf("发送成功\n");
  71. }
  72. //6、关闭套接字
  73. close(newfd);
  74. close(sfd);
  75. return 0;
  76. }

2.2 客户端实现

  1. int main()
  2. {
  3. //1、创建用于通信的套接字文件描述符
  4. int cfd = socket(AF_UNIX, SOCK_STREAM, 0);
  5. if(cfd == -1)
  6. {
  7. perror("socket error");
  8. return -1;
  9. }
  10. //判断要绑定的套接字文件是否存在?
  11. if(access("./linux", F_OK) == 0)
  12. {
  13. //说明文件存在,要将其删除
  14. if(unlink("./linux") ==-1)
  15. {
  16. perror("unlink error");
  17. return -1;
  18. }
  19. }
  20. //2、绑定IP地址和端口号
  21. //2.1 填充客户端地址信息结构体
  22. struct sockaddr_un cun;
  23. cun.sun_family = AF_UNIX;
  24. strcpy(cun.sun_path, "./linux");
  25. //2.2 绑定
  26. if( bind(cfd, (struct sockaddr*)&cun, sizeof(cun)) ==-1)
  27. {
  28. perror("bind error");
  29. return -1;
  30. }
  31. printf("bind success\n");
  32. //3、连接服务器
  33. //3.1 准备对端地址信息结构体
  34. struct sockaddr_un sun;
  35. sun.sun_family = AF_UNIX;
  36. strcpy(sun.sun_path, "./unix");
  37. //3.2 连接服务器
  38. if(connect(cfd, (struct sockaddr*)&sun, sizeof(sun))==-1)
  39. {
  40. perror("connect error");
  41. return -1;
  42. }
  43. printf("connect success\n");
  44. //4、数据收发
  45. char wbuf[128] = "";
  46. char rbuf[128] = "";
  47. while(1)
  48. {
  49. //从终端上获取要发送的数据
  50. fgets(wbuf, sizeof(wbuf), stdin);
  51. wbuf[strlen(wbuf)-1] = '\0'; //将读取的换行改成'\0'
  52. //将数据发送给服务器
  53. send(cfd, wbuf, strlen(wbuf), 0);
  54. printf("发送成功\n");
  55. //接收服务器发来的消息
  56. bzero(rbuf, sizeof(rbuf));
  57. recv(cfd, rbuf, sizeof(rbuf), 0);
  58. printf("服务器发来的消息为:%s\n", rbuf);
  59. }
  60. //5、关闭套接字
  61. close(cfd);
  62. return 0;
  63. }

3、报式域套接字

3.1 服务器端实现

  1. int main(int argc, const char *argv[])
  2. {
  3. //1、创建用于通信的套接字文件描述符
  4. int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
  5. if(sfd == -1)
  6. {
  7. perror("socket error");
  8. return -1;
  9. }
  10. //判断要绑定的套接字文件是否存在?
  11. if(access("./unix", F_OK) == 0)
  12. {
  13. //说明文件存在,要将其删除
  14. if(unlink("./unix") ==-1)
  15. {
  16. perror("unlink error");
  17. return -1;
  18. }
  19. }
  20. //2、绑定IP地址和端口号
  21. //2.1 填充地址信息结构体
  22. struct sockaddr_un sun;
  23. sun.sun_family = AF_UNIX;
  24. strcpy(sun.sun_path, "./unix");
  25. //2.2 绑定
  26. if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) ==-1)
  27. {
  28. perror("bind error");
  29. return -1;
  30. }
  31. printf("bind success\n");
  32. //3、数据收发
  33. char rbuf[128] = "";
  34. //准备接受对端的地址信息
  35. struct sockaddr_un cun;
  36. socklen_t addrlen = sizeof(cun);
  37. while(1)
  38. {
  39. //清空容器
  40. bzero(rbuf,sizeof(rbuf));
  41. //读取数据
  42. recvfrom(sfd, rbuf, sizeof(rbuf), 0, (struct sockaddr*)&cun, &addrlen);
  43. printf("[%s]:%s\n", cun.sun_path, rbuf);
  44. //将收到的消息,加个笑脸回过去
  45. strcat(rbuf, "*_*");
  46. if(sendto(sfd, rbuf, strlen(rbuf), 0, (struct sockaddr*)&cun, sizeof(cun))==-1)
  47. {
  48. perror("write error");
  49. return -1;
  50. }
  51. printf("发送成功\n");
  52. }
  53. //4、关闭套接字
  54. close(sfd);
  55. return 0;
  56. }

3.2 客户端实现

  1. int main(int argc, const char *argv[])
  2. {
  3. //1、创建用于通信的套接字文件描述符
  4. int cfd = socket(AF_UNIX, SOCK_DGRAM, 0);
  5. if(cfd == -1)
  6. {
  7. perror("socket error");
  8. return -1;
  9. }
  10. //2、绑定套接字文件
  11. //2.1 填充地址信息结构体
  12. struct sockaddr_un cun;
  13. cun.sun_family = AF_UNIX;
  14. strcpy(cun.sun_path, "./linux");
  15. //判断要绑定的套接字文件是否存在?
  16. if(access("./linux", F_OK) == 0)
  17. {
  18. //说明文件存在,要将其删除
  19. if(unlink("./linux") ==-1)
  20. {
  21. perror("unlink error");
  22. return -1;
  23. }
  24. }
  25. //2.2 绑定
  26. if(bind(cfd, (struct sockaddr*)&cun, sizeof(cun)) ==-1)
  27. {
  28. perror("bind error");
  29. return -1;
  30. }
  31. printf("bind success\n");
  32. //3、数据收发
  33. char wbuf[128] = "";
  34. //填充服务器的地址信息结构体
  35. struct sockaddr_un sun;
  36. sun.sun_family = AF_UNIX;
  37. strcpy(sun.sun_path, "./unix");
  38. char rbuf[128] = "";
  39. while(1)
  40. {
  41. //清空容器
  42. bzero(wbuf,sizeof(wbuf));
  43. bzero(rbuf, sizeof(rbuf));
  44. //从终端上获取信息
  45. fgets(wbuf, sizeof(wbuf), stdin);
  46. wbuf[strlen(wbuf)-1] = 0;
  47. //讲消息发送给服务器
  48. sendto(cfd, wbuf, strlen(wbuf), 0, (struct sockaddr*)&sun, sizeof(sun));
  49. printf("发送成功\n");
  50. //接受服务器发来的消息
  51. recvfrom(cfd, rbuf, sizeof(rbuf), 0,NULL, NULL);
  52. printf("收到服务器消息为:%s\n", rbuf);
  53. }
  54. //4、关闭套接字
  55. close(cfd);
  56. return 0;
  57. }
标签: 学习 unix 服务器

本文转载自: https://blog.csdn.net/qq_37193153/article/details/139100451
版权归原作者 koka_jerry 所有, 如有侵权,请联系我们删除。

“嵌入式学习——3——域套接字UNIX”的评论:

还没有评论