Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4. #include <sys/socket.h>
  5. #include <sys/un.h>
  6. #include <sys/ioctl.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #define SV_SOCK_PATH "/tmp/srv_sk_stream"
  10. #define BACKLOG 1
  11. #define MAXWRITE 1000
  12. #define TOMSEC 1000
  13.  
  14. int main(int argc, char* argv[]) {
  15. struct sockaddr_un addr;
  16. int sfd, cfd = -1;
  17. ssize_t numRead;
  18. char buffer[30];
  19. int i = 0;
  20. ssize_t retValWrite = -1;
  21. numRead = sizeof(buffer);
  22. if(argc !=2 ) {printf("Give the sleep value argument (msec)!n"); return EXIT_FAILURE;}
  23. int sleepTime = atoi(argv[1]) * TOMSEC;
  24. fd_set writeFdSet;
  25. struct timeval tv;
  26. tv.tv_sec = 0;
  27. tv.tv_usec = 0;
  28. struct timespec ts;
  29. ts.tv_sec = 0;
  30. ts.tv_nsec = 0;
  31.  
  32. /* create socket */
  33. sfd = socket(AF_UNIX, SOCK_STREAM, 0);
  34. if (sfd == -1) {perror("FAILED creating a socket!"); return EXIT_FAILURE;}
  35. if (remove(SV_SOCK_PATH) == -1 && errno != ENOENT) {perror("FAILED removing old socket fd! - "); return EXIT_FAILURE;}
  36.  
  37. /* prepare it */
  38. memset(&addr, 0, sizeof(struct sockaddr_un));
  39. addr.sun_family = AF_UNIX;
  40. strncpy(addr.sun_path, SV_SOCK_PATH, sizeof(addr.sun_path) - 1);
  41.  
  42. /* bind it */
  43. if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {perror("FAILED bind! - "); return EXIT_FAILURE;}
  44.  
  45. /* start to listen */
  46. if (listen(sfd, BACKLOG) == -1) {perror("FAILED listen! - "); return EXIT_FAILURE;}
  47.  
  48. while(i<MAXWRITE) {
  49. int bytesToRead = 0;
  50. retValWrite = -13;
  51.  
  52.  
  53. /* block in accept until a client connects (only one) */
  54. if (-1 == cfd) {
  55. cfd = accept(sfd, NULL, NULL);
  56. if (cfd == -1) {perror("FAILED accept! - "); return EXIT_FAILURE;}
  57. FD_ZERO(&writeFdSet);
  58. FD_SET(cfd, &writeFdSet);
  59. }
  60.  
  61. //ioctl(cfd,FIONREAD,&bytesToRead);
  62. //printf("---------> SND_BUFF has %d bytes left to be readn", bytesToRead);
  63.  
  64. sprintf(buffer, "PING FROM SERVER %d", i);
  65. /* write to the client's socket */
  66. int retValSelect = 0;
  67. errno = 0;
  68. retValSelect = select(cfd+1, NULL, &writeFdSet, NULL, &tv);
  69. //perror("ERRNO from pselect: ");
  70. if (retValSelect > 0) {
  71. int retValFdIsSet = 0;
  72. retValFdIsSet = FD_ISSET(cfd, &writeFdSet);
  73. //perror("FD_ISSET - ");
  74. if(retValFdIsSet) {
  75. retValWrite = write(cfd, buffer, numRead);
  76. if (retValWrite == 0 ) {
  77. printf("Written 0 bytesn");
  78. } else if (retValWrite < 0) {
  79. perror("Error writiing to socket!n");
  80. }
  81.  
  82. ++i;
  83. printf("Written: %d n", i);
  84.  
  85. usleep(sleepTime);
  86. }
  87. } else {
  88. printf("Wait for it... %dn", count);
  89. }
  90. }
  91.  
  92. /* close client socket */
  93. if (close(cfd) == -1) {perror("FAILED close srv socket! - "); return EXIT_FAILURE;}
  94. if (remove(SV_SOCK_PATH) == -1 && errno != ENOENT) {perror("FAILED removing old socket fd! - "); return EXIT_FAILURE;}
  95.  
  96. return EXIT_SUCCESS;
  97. }
  98.  
  99. #include <stdio.h>
  100. #include <stdlib.h>
  101. #include <sys/stat.h>
  102. #include <sys/socket.h>
  103. #include <sys/un.h>
  104. #include <sys/ioctl.h>
  105. #include <unistd.h>
  106. #include <errno.h>
  107. #define SV_SOCK_PATH "/tmp/srv_sk_stream"
  108. #define MAXREAD 1000
  109. #define TOMSEC 1000
  110.  
  111. int main(int argc, char** argv)
  112. {
  113. struct sockaddr_un addr;
  114. int sfd;
  115. ssize_t numRead;
  116. char pingStr[30];
  117. numRead = sizeof(pingStr);
  118. int readCount = 0;
  119. if(argc !=2 ) {printf("Give the sleep value argument (msec)!n"); return EXIT_FAILURE;}
  120. int sleepTime = atoi(argv[1]) * TOMSEC;
  121.  
  122. /* create socket */
  123. sfd = socket(AF_UNIX, SOCK_STREAM, 0);
  124.  
  125. if(sfd == -1) {perror("FAILED creating a socket! - "); return EXIT_FAILURE;}
  126.  
  127. /* prepare it */
  128. memset(&addr, 0, sizeof(struct sockaddr_un));
  129. addr.sun_family = AF_UNIX;
  130. strncpy(addr.sun_path, SV_SOCK_PATH, sizeof(addr.sun_path) - 1);
  131.  
  132. /* connect */
  133. if (connect(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {perror("FAILED connecting to socket! - "); return EXIT_FAILURE;}
  134.  
  135. while (readCount < MAXREAD) {
  136. char strRead[numRead];
  137. int bytesRead=0;
  138. int bytesToRead=0;
  139.  
  140. ioctl(sfd,FIONREAD, &bytesToRead);
  141. printf("---------> RCV_BUFF has %d bytes left to be readn", bytesToRead);
  142.  
  143. /* read */
  144. bytesRead = read(sfd, &strRead, numRead);
  145. if(bytesRead == 0) {
  146. perror("Read 0 bytes from socket! - ");
  147. }
  148. else if (bytesRead < 0) {
  149. perror("FAILED reading from socket! - ");
  150. }
  151. ++readCount;
  152. printf("%d - READ: %sn", readCount, strRead);
  153. usleep(sleepTime);
  154. }
  155.  
  156. /* close */
  157. if (close(sfd) == -1) {perror("FAILED close clt socket! - "); return EXIT_FAILURE;}
  158.  
  159. return EXIT_SUCCESS;
  160. }
  161.  
  162. ./srvstream 100
  163. Written: 1
  164. Written: 2
  165. Written: 3
  166. Written: 4
  167. Written: 5
  168. Written: 6
  169. Written: 7
  170. Written: 8
  171. Written: 9
  172. Written: 10
  173. Written: 11
  174. Written: 12
  175. Written: 13
  176. Written: 14
  177. Written: 15
  178. Written: 16
  179. Written: 17
  180. Written: 18
  181. Written: 19
  182. Written: 20
  183. Written: 21
  184. Written: 22
  185. Written: 23
  186. Written: 24
  187. Written: 25
  188. Written: 26
  189. Written: 27
  190. Written: 28
  191. Written: 29
  192. Written: 30
  193. Written: 31
  194. Written: 32
  195. Written: 33
  196. Written: 34
  197. Written: 35
  198. Written: 36
  199. Written: 37
  200. Written: 38
  201. Written: 39
  202. Written: 40
  203. Written: 41
  204. Written: 42
  205. Written: 43
  206. Written: 44
  207. Written: 45
  208. Written: 46
  209. Written: 47
  210. Written: 48
  211. Written: 49
  212. Written: 50
  213. Written: 51
  214. Written: 52
  215. Written: 53
  216. Written: 54
  217. Written: 55
  218. Written: 56
  219. Written: 57
  220. Written: 58
  221. Written: 59
  222. Written: 60
  223. Written: 61
  224. Written: 62
  225. Written: 63
  226. Written: 64
  227. Written: 65
  228. Written: 66
  229. Written: 67
  230. Written: 68
  231. Written: 69
  232. Written: 70
  233. Written: 71
  234. Written: 72
  235. Written: 73
  236. Written: 74
  237. Written: 75
  238. Written: 76
  239. Written: 77
  240. Written: 78
  241. Wait for it... 0
  242. Wait for it... 0
  243. Wait for it... 0
  244. Wait for it... 0
  245. Wait for it... 0
  246. ------->8-------- stays this way even after the client reports consumes the whole buffer
  247.  
  248. ./cltstream 1000
  249. ---------> RCV_BUFF has 0 bytes left to be read
  250. 1 - READ: PING FROM SERVER 0
  251. ---------> RCV_BUFF has 270 bytes left to be read
  252. 2 - READ: PING FROM SERVER 1
  253. ---------> RCV_BUFF has 540 bytes left to be read
  254. 3 - READ: PING FROM SERVER 2
  255. ---------> RCV_BUFF has 810 bytes left to be read
  256. 4 - READ: PING FROM SERVER 3
  257. ---------> RCV_BUFF has 1080 bytes left to be read
  258. 5 - READ: PING FROM SERVER 4
  259. ---------> RCV_BUFF has 1350 bytes left to be read
  260. 6 - READ: PING FROM SERVER 5
  261. ---------> RCV_BUFF has 1620 bytes left to be read
  262. 7 - READ: PING FROM SERVER 6
  263. ---------> RCV_BUFF has 1890 bytes left to be read
  264. 8 - READ: PING FROM SERVER 7
  265. ---------> RCV_BUFF has 2100 bytes left to be read
  266. 9 - READ: PING FROM SERVER 8
  267. ---------> RCV_BUFF has 2070 bytes left to be read
  268. 10 - READ: PING FROM SERVER 9
  269. ---------> RCV_BUFF has 2040 bytes left to be read
  270. 11 - READ: PING FROM SERVER 10
  271. ---------> RCV_BUFF has 2010 bytes left to be read
  272. 12 - READ: PING FROM SERVER 11
  273. ---------> RCV_BUFF has 1980 bytes left to be read
  274. 13 - READ: PING FROM SERVER 12
  275. ---------> RCV_BUFF has 1950 bytes left to be read
  276. 14 - READ: PING FROM SERVER 13
  277. ---------> RCV_BUFF has 1920 bytes left to be read
  278. 15 - READ: PING FROM SERVER 14
  279. ---------> RCV_BUFF has 1890 bytes left to be read
  280. 16 - READ: PING FROM SERVER 15
  281. ---------> RCV_BUFF has 1860 bytes left to be read
  282. 17 - READ: PING FROM SERVER 16
  283. ---------> RCV_BUFF has 1830 bytes left to be read
  284. 18 - READ: PING FROM SERVER 17
  285. ---------> RCV_BUFF has 1800 bytes left to be read
  286. 19 - READ: PING FROM SERVER 18
  287. ---------> RCV_BUFF has 1770 bytes left to be read
  288. 20 - READ: PING FROM SERVER 19
  289. ---------> RCV_BUFF has 1740 bytes left to be read
  290. 21 - READ: PING FROM SERVER 20
  291. ---------> RCV_BUFF has 1710 bytes left to be read
  292. 22 - READ: PING FROM SERVER 21
  293. ---------> RCV_BUFF has 1680 bytes left to be read
  294. 23 - READ: PING FROM SERVER 22
  295. ---------> RCV_BUFF has 1650 bytes left to be read
  296. 24 - READ: PING FROM SERVER 23
  297. ---------> RCV_BUFF has 1620 bytes left to be read
  298. 25 - READ: PING FROM SERVER 24
  299. ---------> RCV_BUFF has 1590 bytes left to be read
  300. 26 - READ: PING FROM SERVER 25
  301. ---------> RCV_BUFF has 1560 bytes left to be read
  302. 27 - READ: PING FROM SERVER 26
  303. ---------> RCV_BUFF has 1530 bytes left to be read
  304. 28 - READ: PING FROM SERVER 27
  305. ---------> RCV_BUFF has 1500 bytes left to be read
  306. 29 - READ: PING FROM SERVER 28
  307. -----------8<------------ -- continues this way until RCV_BUFF has 0 bytes left and then blocks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement