unix.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* copied from http://www.daniweb.com/software-development/c/threads/179814 */
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/un.h>
  12. #include <sys/stat.h>
  13. #include <sys/wait.h>
  14. #define SRV_IP "127.0.0.1"
  15. #define PORT 9930
  16. #define BUFLEN 512
  17. #define NPACK 10
  18. const char * fname;
  19. enum { SINGLE, PARALLEL } mode = PARALLEL;
  20. int do_fork = 0;
  21. int pipefds[2];
  22. int server(void)
  23. {
  24. int conn,create_socket,new_socket,addrlen,fd;
  25. int bufsize = 1024;
  26. char *buffer = malloc(bufsize);
  27. struct sockaddr_un address;
  28. if ((create_socket = socket(AF_UNIX,SOCK_STREAM,
  29. 0)) > 0)
  30. printf("The socket was created\n");
  31. address.sun_family = AF_UNIX;
  32. memcpy(address.sun_path,"u",10);
  33. if (bind(create_socket,(struct sockaddr *)&address,
  34. sizeof(address)) < 0) {
  35. perror("bind");
  36. close(create_socket);
  37. exit(-1);
  38. }
  39. if (listen(create_socket,3) < 0) {
  40. perror("listen");
  41. close(create_socket);
  42. exit(-1);
  43. }
  44. if (mode == PARALLEL) {
  45. close(pipefds[0]);
  46. char byte = 0;
  47. write(pipefds[1], &byte, 1);
  48. }
  49. addrlen = sizeof(address);
  50. new_socket = accept(create_socket,(struct sockaddr *)&address,
  51. &addrlen);
  52. if (new_socket < 0) {
  53. perror("accept");
  54. close(create_socket);
  55. exit(-1);
  56. }
  57. close(create_socket);
  58. printf("The client is connected...\n");
  59. if (do_fork) {
  60. if (fork() > 0) {
  61. asm volatile ("int $3");
  62. close(new_socket);
  63. wait(NULL);
  64. return 0;
  65. }
  66. }
  67. if ((fd = open(fname,O_RDONLY,0)) < 0) {
  68. perror("File Open Failed");
  69. close(new_socket);
  70. exit(-1);
  71. }
  72. while((conn = read(fd,buffer,
  73. bufsize)) > 0)
  74. sendto(new_socket,buffer,conn,0,0,0);
  75. printf("Request completed\n");
  76. close(new_socket);
  77. if (do_fork)
  78. exit(0);
  79. return 0;
  80. }
  81. int client(void)
  82. {
  83. int count,create_socket;
  84. int bufsize = 1024;
  85. char *buffer = malloc(bufsize);
  86. struct sockaddr_un address;
  87. if (mode == PARALLEL) {
  88. close(pipefds[1]);
  89. char byte = 0;
  90. read(pipefds[0], &byte, 1);
  91. }
  92. if ((create_socket = socket(AF_UNIX,SOCK_STREAM,0)) >= 0)
  93. printf("The socket was created\n");
  94. address.sun_family = AF_UNIX;
  95. memcpy(address.sun_path,"u",10);
  96. if (connect(create_socket,(struct sockaddr *)&address,
  97. sizeof(address)) == 0)
  98. printf("The connection was accepted with the server\n");
  99. else {
  100. printf("The connection was not accepted with the server\n");
  101. exit(0);
  102. }
  103. if (do_fork) {
  104. if (fork() > 0) {
  105. close(create_socket);
  106. wait(NULL);
  107. return 0;
  108. }
  109. }
  110. printf("The contents of file are...\n\n");
  111. while((count=recv(create_socket,buffer,bufsize,0))>0)
  112. write(1,buffer,count);
  113. printf("\nEOF\n");
  114. buffer[0] = 0;
  115. close(create_socket);
  116. if (do_fork)
  117. exit(0);
  118. return 0;
  119. }
  120. int main(int argc, char ** argv)
  121. {
  122. char fnamebuf[40];
  123. strcpy(fnamebuf, argv[0]);
  124. strcat(fnamebuf, ".c");
  125. fname = fnamebuf;
  126. if (argc > 1) {
  127. if (strcmp(argv[1], "client") == 0) {
  128. mode = SINGLE;
  129. client();
  130. return 0;
  131. }
  132. if (strcmp(argv[1], "server") == 0) {
  133. mode = SINGLE;
  134. server();
  135. return 0;
  136. }
  137. if (strcmp(argv[1], "fork") == 0) {
  138. do_fork = 1;
  139. goto old;
  140. }
  141. }
  142. else {
  143. old:
  144. pipe(pipefds);
  145. int pid = fork();
  146. if (pid == 0)
  147. client();
  148. else
  149. server();
  150. }
  151. return 0;
  152. }