1 분 소요

linux application ipc를 socket를 활용해서 제작..

서버는 소켓을 열고 받은걸 바로 다시 보내는 형태로 제작

클라이언트는 연결된 소켓을 사용해서 보내고 받는걸 처리 (read에서 계속 대기 하는 형태입니다.)

server.c

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/socket.h>
  4. #include <sys/un.h>
  5. #include
  6. #include
  7. #include
  8. #include
  9. #define MAXLINE 1024
  10. int main(int argc, char **argv)
  11. {
  12. int server_sockfd, client_sockfd;
  13. int state, client_len;
  14. pid_t pid;
  15. struct sockaddr_un clientaddr, serveraddr;
  16. char buf[MAXLINE];
  17. if (argc != 2)
  18. {
  19. printf(“Usage : %s [socket file name]\n”, argv[0]);
  20. printf(“예    : %s /tmp/mysocket\n”, argv[0]);
  21. exit(0);
  22. }
  23. if (access(argv[1], F_OK) == 0)
  24. {
  25. unlink(argv[1]);
  26. }
  27. client_len = sizeof(clientaddr);
  28. if ((server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
  29. {
  30. perror(“socket error : “);
  31. exit(0);
  32. }
  33. bzero(&serveraddr, sizeof(serveraddr));
  34. serveraddr.sun_family = AF_UNIX;
  35. strcpy(serveraddr.sun_path, argv[1]);
  36. state = bind(server_sockfd , (struct sockaddr *)&serveraddr,
  37. sizeof(serveraddr));
  38. if (state == -1)
  39. {
  40. perror(“bind error : “);
  41. exit(0);
  42. }
  43. state = listen(server_sockfd, 5);
  44. if (state == -1)
  45. {
  46. perror(“listen error : “);
  47. exit(0);
  48. }
  49. while(1)
  50. {
  51. client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientaddr, &client_len);
  52. pid = fork();
  53. if (pid == 0)
  54. {
  55. if (client_sockfd == -1)
  56. {
  57. perror(“Accept error : “);
  58. exit(0);
  59. }
  60. while(1)
  61. {
  62. memset(buf, 0x00, MAXLINE);
  63. if (read(client_sockfd, buf, MAXLINE) <= 0)
  64. {
  65. close(client_sockfd);
  66. exit(0);
  67. }
  68. printf(“receivd > %s “,buf);
  69. write(client_sockfd, buf, strlen(buf));
  70. printf(“send > %s “, buf);
  71. }
  72. }
  73. }
  74. close(client_sockfd);
  75. }

client.c

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/socket.h>
  4. #include
  5. #include <sys/un.h>
  6. #include
  7. #include
  8. #include
  9. #define MAXLINE 1024
  10. int main(int argc, char **argv)
  11. {
  12. int client_len;
  13. int client_sockfd;                                                                                                                                                                            char buf_in[MAXLINE];
  14. char buf_get[MAXLINE];
  15. struct sockaddr_un clientaddr;
  16. if (argc != 2)
  17. {
  18. printf(“Usage : %s [file_name]\n”, argv[0]);
  19. printf(“예    : %s /tmp/mysocket\n”, argv[0]);
  20. exit(0);
  21. }
  22. client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  23. if (client_sockfd == -1)
  24. {
  25. perror(“error : “);
  26. exit(0);
  27. }
  28. bzero(&clientaddr, sizeof(clientaddr));
  29. clientaddr.sun_family = AF_UNIX;
  30. strcpy(clientaddr.sun_path, argv[1]);
  31. client_len = sizeof(clientaddr);
  32. if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) < 0)
  33. {
  34. perror(“Connect error: “);
  35. exit(0);
  36. }
  37. while(1)
  38. {
  39. memset(buf_in, 0x00, MAXLINE);
  40. memset(buf_get, 0x00, MAXLINE);
  41. printf(“> “);
  42. fgets(buf_in, MAXLINE, stdin);
  43. write(client_sockfd, buf_in, strlen(buf_in));
  44. read(client_sockfd, buf_get, MAXLINE);
  45. printf(“-> %s”, buf_get);
  46. }
  47. close(client_sockfd);
  48. exit(0);
  49. }

ref : https://www.joinc.co.kr/w/Site/system_programing/Book_LSP/ch08_IPC