Advertisement
Guest User

Untitled

a guest
Aug 14th, 2012
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/socket.h>
  11. #include <sys/sendfile.h>
  12.  
  13. struct SendInfo {
  14. const char *filename;
  15. char *filebuffer;
  16. };
  17.  
  18. static void onexit(int a, int b, int c, int d) {
  19. exit(0);
  20. }
  21.  
  22. static void * sender (void *arg) {
  23. int newsockd = *(int *)arg;
  24. int sockd;
  25. int fpl, fd;
  26. struct stat fileStat;
  27. uint32_t fsize, size_to_send;
  28. int rc;
  29. char buffer[512];
  30. off_t offset;
  31.  
  32. if((fpl = open("/tmp/input.txt", O_RDONLY)) < 0){
  33. perror("open file with open");
  34. onexit(newsockd, sockd, 0, 2);
  35. }
  36. if(fstat(fpl, &fileStat) < 0){
  37. perror("Errore fstat");
  38. onexit(newsockd, sockd, fpl, 3);
  39. }
  40. fsize = fileStat.st_size;
  41. if(send(newsockd, &fsize, sizeof(fsize), 0) < 0){
  42. perror("Errore durante l'invio grandezza file list");
  43. onexit(newsockd, sockd, fpl, 3);
  44. }
  45.  
  46. fd = fpl;
  47. offset = 0;
  48. for (size_to_send = fsize; size_to_send > 0; ){
  49. rc = sendfile(newsockd, fd, &offset, size_to_send);
  50. if (rc <= 0){
  51. perror("sendfile");
  52. onexit(newsockd, sockd, fd, 3);
  53. }
  54. /*offset += rc;*/
  55. size_to_send -= rc;
  56. }
  57. close(fd); /*
  58. * la chiusura del file va qui altrimenti rischio loop
  59. * infinito e scrittura all'interno del file
  60. */
  61.  
  62. memset(buffer, 0, sizeof(buffer));
  63. strcpy(buffer, "226 File Successfully transfered\n");
  64. if(send(newsockd, buffer, strlen(buffer), 0) < 0){
  65. perror("Errore durante l'invio 226");
  66. onexit(newsockd, sockd, 0, 2);
  67. }
  68. memset(buffer, 0, sizeof(buffer));
  69. }
  70.  
  71. static void * receiver (void *arg) {
  72. int fd;
  73. int sockd = *(int *)arg;
  74. struct SendInfo sInfo = { "/tmp/output.txt", 0 };
  75. uint32_t fsize, fsize_tmp, total_bytes_read = 0;
  76. int nread;
  77. char buffer[512];
  78.  
  79. if(read(sockd, &fsize, sizeof(fsize)) < 0){
  80. perror("Errore durante ricezione grandezza file\n");
  81. onexit(sockd, 0 ,0 ,1);
  82. }
  83. fd = open(sInfo.filename, O_CREAT | O_WRONLY, 0644);
  84. if (fd < 0) {
  85. perror("open");
  86. onexit(sockd, 0 ,0 ,1);
  87. }
  88. fsize_tmp = fsize;
  89.  
  90. sInfo.filebuffer = malloc(fsize);
  91. if(sInfo.filebuffer == NULL){
  92. perror("malloc");
  93. onexit(sockd, 0, fd, 4);
  94. }
  95.  
  96. while((total_bytes_read != fsize) &&
  97. ((nread = read(sockd, sInfo.filebuffer, fsize_tmp)) > 0)){
  98. if(write(fd, sInfo.filebuffer, nread) != nread){
  99. perror("write RETR");
  100. onexit(sockd, 0, 0, 1);
  101. }
  102. total_bytes_read += nread;
  103. fsize_tmp -= nread;
  104. }
  105. close(fd); /*
  106. * la chiusura del file va qui altrimenti rischio loop
  107. * infinito e scrittura all'interno del file
  108. */
  109.  
  110.  
  111. memset(buffer, 0, sizeof(buffer));
  112. if(recv(sockd, buffer, 34, 0) < 0){
  113. perror("Errore ricezione 226");
  114. onexit(sockd, 0, 0, 1);
  115. }
  116. printf("%s", buffer);
  117. memset(buffer, 0, sizeof(buffer));
  118. /*memset(dirpath, 0, sizeof(dirpath));*/
  119. free(sInfo.filebuffer);
  120. }
  121.  
  122. int main () {
  123. int sp[2];
  124. pthread_t t[2];
  125. socketpair(AF_UNIX, SOCK_STREAM, 0, sp);
  126. pthread_create(t+0, 0, sender, sp+0);
  127. pthread_create(t+1, 0, receiver, sp+1);
  128. pthread_join(t[0], 0);
  129. pthread_join(t[1], 0);
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement