Unix/C + + IPC

Source: Internet
Author: User

Unix/C + + IPC

An example
  1. //c_unix. C
  2. #include <stdio. h>
  3. #include <sys/types. h>
  4. #include <sys/socket. h>
  5. #include <sys/un. h>
  6. #define Unix_domain "/tmp/unix.domain"
  7. int main(void)
  8. {
  9. int CONNECT_FD;
  10. int ret;
  11. Char snd_buf[1024x768];
  12. int i;
  13. static struct Sockaddr_un srv_addr;
  14. //creat UNIX sockets
  15. CONNECT_FD=socket(Pf_unix, Sock_stream, 0);
  16. if(connect_fd<0)
  17. {
  18. Perror("Cannot create communication socket");
  19. return 1;
  20. }
  21. Srv_addr. sun_family=af_unix;
  22. strcpy(srv_addr. Sun_path, Unix_domain);
  23. //connect Server
  24. RET=connect(connect_fd,(struct sockaddr*)&srv_addr, sizeof(srv_addr ));
  25. if(ret==-1)
  26. {
  27. Perror("Cannot connect to the server");
  28. Close(connect_fd);
  29. return 1;
  30. }
  31. Memset(Snd_buf, 0, 1024x768);
  32. strcpy(snd_buf,"message from client");
  33. //send Info Server
  34. for(i=0; I<4; I++)
  35. Write(connect_fd, snd_buf, sizeof(snd_buf));
  36. Close(connect_fd);
  37. return 0;
  38. }
  1. //s_unix. C
  2. #include <stdio. h>
  3. #include <sys/types. h>
  4. #include <sys/socket. h>
  5. #include <sys/un. h>
  6. #define Unix_domain "/tmp/unix.domain"
  7. int main(void)
  8. {
  9. socklen_t Clt_addr_len;
  10. int LISTEN_FD;
  11. int COM_FD;
  12. int ret;
  13. int i;
  14. static char recv_buf[1024x768];
  15. int len;
  16. struct Sockaddr_un clt_addr;
  17. struct Sockaddr_un srv_addr;
  18. LISTEN_FD=socket(Pf_unix, Sock_stream, 0);
  19. if(listen_fd<0)
  20. {
  21. Perror("Cannot create communication socket");
  22. return 1;
  23. }
  24. //Set server Addr_param
  25. Srv_addr. sun_family=af_unix;
  26. strncpy(srv_addrsun_path, Unix_domain, sizeof(srv_addr. Sun_path)-1) ;
  27. Unlink(Unix_domain);
  28. //bind sockfd & Addr
  29. RET=bind(listen_fd,(struct sockaddr*)&srv_addr, sizeof(srv_addr ));
  30. if(ret==-1)
  31. {
  32. Perror("Cannot bind server socket");
  33. Close(listen_fd);
  34. Unlink(Unix_domain);
  35. return 1;
  36. }
  37. //listen SOCKFD
  38. RET=listen(listen_fd, 1);
  39. if(ret==-1)
  40. {
  41. Perror("Cannot listen the client connect request");
  42. Close(listen_fd);
  43. Unlink(Unix_domain);
  44. return 1;
  45. }
  46. //have connect request Use Accept
  47. len=sizeof(clt_addr);
  48. COM_FD=accept(listen_fd,(struct sockaddr*)&clt_addr,&len) ;
  49. if(com_fd<0)
  50. {
  51. Perror("Cannot accept client connect request");
  52. Close(listen_fd);
  53. Unlink(Unix_domain);
  54. return 1;
  55. }
  56. //read and printf sent client info
  57. printf("/n=====info=====/n");
  58. for(i=0; I<4; I++)
  59. {
  60. Memset(Recv_buf, 0, 1024x768);
  61. int num=read(com_fd, recv_buf, sizeof(recv_buf));
  62. printf("Message from Client (%d)):%s/n", num, recv_buf);
  63. }
  64. Close(com_fd);
  65. Close(listen_fd);
  66. Unlink(Unix_domain);
  67. return 0;
  68. }

Unix/C + + IPC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.