Guest User

Untitled

a guest
Apr 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. void *receiveDCC(void *ptr) {
  2. struct dcc_thread_args *args = (struct dcc_thread_args *)ptr;
  3.  
  4. int sockfd;
  5. char buf[BUFF_SIZE];
  6. struct addrinfo hints, *servinfo, *p;
  7. int rv;
  8.  
  9. memset(&hints, 0, sizeof hints);
  10. hints.ai_family = AF_UNSPEC;
  11. hints.ai_socktype = SOCK_STREAM;
  12.  
  13. struct in_addr in;
  14. in.s_addr = args->ip;
  15. if ((rv = getaddrinfo(inet_ntoa(in), args->port, &hints, &servinfo)) != 0) {
  16. fprintf(stderr, "getaddrinfo: %s\r\n", gai_strerror(rv));
  17. exit(1);
  18. }
  19.  
  20. for(p = servinfo; p != NULL; p = p->ai_next) {
  21. if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
  22. perror("client: socket");
  23. continue;
  24. }
  25.  
  26. if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
  27. close(sockfd);
  28. perror("client: connect");
  29. continue;
  30. }
  31.  
  32. break;
  33. }
  34.  
  35. if (p == NULL) {
  36. fprintf(stderr, "client: failed to connect\n");
  37. exit(2);
  38. }
  39.  
  40. int res;
  41. FILE *file;
  42. file = fopen(args->filename, "wb");
  43.  
  44. do {
  45. memset((void*)&buf, 0, BUFF_SIZE);
  46.  
  47. if ((res = recv(sockfd, &buf, BUFF_SIZE, 0)) == -1) {
  48. fprintf(stderr, "recv error: %s\r\n", gai_strerror(res));
  49. close(sockfd);
  50. exit(1);
  51. }
  52.  
  53. fwrite (buf , 1 , sizeof(buf) , file );
  54.  
  55. } while (res == BUFF_SIZE);
  56.  
  57. fclose(file);
  58. printf(": Filetransfer complete: received '%s' from %s\r\n", args->filename, args->from);
  59. freeaddrinfo(servinfo);
  60. close(sockfd);
  61. }
Add Comment
Please, Sign In to add comment