unix.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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,fd;
  25. socklen_t addrlen;
  26. int bufsize = 1024;
  27. char *buffer = malloc(bufsize);
  28. struct sockaddr_un address;
  29. if ((create_socket = socket(AF_UNIX,SOCK_STREAM,
  30. 0)) > 0)
  31. printf("The socket was created\n");
  32. address.sun_family = AF_UNIX;
  33. memcpy(address.sun_path,"u",10);
  34. if (bind(create_socket,(struct sockaddr *)&address,
  35. sizeof(address)) < 0) {
  36. perror("bind");
  37. close(create_socket);
  38. exit(-1);
  39. }
  40. if (listen(create_socket,3) < 0) {
  41. perror("listen");
  42. close(create_socket);
  43. exit(-1);
  44. }
  45. if (mode == PARALLEL) {
  46. close(pipefds[0]);
  47. char byte = 0;
  48. write(pipefds[1], &byte, 1);
  49. }
  50. addrlen = sizeof(address);
  51. new_socket = accept(create_socket,(struct sockaddr *)&address,
  52. &addrlen);
  53. if (new_socket < 0) {
  54. perror("accept");
  55. close(create_socket);
  56. exit(-1);
  57. }
  58. close(create_socket);
  59. printf("The client is connected...\n");
  60. if (do_fork) {
  61. if (fork() > 0) {
  62. asm volatile ("int $3");
  63. close(new_socket);
  64. wait(NULL);
  65. return 0;
  66. }
  67. }
  68. if ((fd = open(fname,O_RDONLY,0)) < 0) {
  69. perror("File Open Failed");
  70. close(new_socket);
  71. exit(-1);
  72. }
  73. while((conn = read(fd,buffer,
  74. bufsize)) > 0)
  75. sendto(new_socket,buffer,conn,0,0,0);
  76. printf("Request completed\n");
  77. close(new_socket);
  78. if (do_fork)
  79. exit(0);
  80. return 0;
  81. }
  82. int client(void)
  83. {
  84. int count,create_socket;
  85. int bufsize = 1024;
  86. char *buffer = malloc(bufsize);
  87. struct sockaddr_un address;
  88. if (mode == PARALLEL) {
  89. close(pipefds[1]);
  90. char byte = 0;
  91. read(pipefds[0], &byte, 1);
  92. }
  93. if ((create_socket = socket(AF_UNIX,SOCK_STREAM,0)) >= 0)
  94. printf("The socket was created\n");
  95. address.sun_family = AF_UNIX;
  96. memcpy(address.sun_path,"u",10);
  97. if (connect(create_socket,(struct sockaddr *)&address,
  98. sizeof(address)) == 0)
  99. printf("The connection was accepted with the server\n");
  100. else {
  101. printf("The connection was not accepted with the server\n");
  102. exit(0);
  103. }
  104. if (do_fork) {
  105. if (fork() > 0) {
  106. close(create_socket);
  107. wait(NULL);
  108. return 0;
  109. }
  110. }
  111. printf("The contents of file are...\n\n");
  112. while((count=recv(create_socket,buffer,bufsize,0))>0)
  113. write(1,buffer,count);
  114. printf("\nEOF\n");
  115. buffer[0] = 0;
  116. close(create_socket);
  117. if (do_fork)
  118. exit(0);
  119. return 0;
  120. }
  121. int main(int argc, char ** argv)
  122. {
  123. char fnamebuf[40];
  124. strcpy(fnamebuf, "unix");
  125. strcat(fnamebuf, ".c");
  126. fname = fnamebuf;
  127. if (argc > 1) {
  128. if (strcmp(argv[1], "client") == 0) {
  129. mode = SINGLE;
  130. client();
  131. return 0;
  132. }
  133. if (strcmp(argv[1], "server") == 0) {
  134. mode = SINGLE;
  135. server();
  136. return 0;
  137. }
  138. if (strcmp(argv[1], "fork") == 0) {
  139. do_fork = 1;
  140. goto old;
  141. }
  142. }
  143. else {
  144. old:
  145. pipe(pipefds);
  146. int pid = fork();
  147. if (pid == 0)
  148. client();
  149. else
  150. server();
  151. }
  152. return 0;
  153. }