Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void *receiveDCC(void *ptr) {
- struct dcc_thread_args *args = (struct dcc_thread_args *)ptr;
- int sockfd;
- char buf[BUFF_SIZE];
- struct addrinfo hints, *servinfo, *p;
- int rv;
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- struct in_addr in;
- in.s_addr = args->ip;
- if ((rv = getaddrinfo(inet_ntoa(in), args->port, &hints, &servinfo)) != 0) {
- fprintf(stderr, "getaddrinfo: %s\r\n", gai_strerror(rv));
- exit(1);
- }
- for(p = servinfo; p != NULL; p = p->ai_next) {
- if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
- perror("client: socket");
- continue;
- }
- if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
- close(sockfd);
- perror("client: connect");
- continue;
- }
- break;
- }
- if (p == NULL) {
- fprintf(stderr, "client: failed to connect\n");
- exit(2);
- }
- int res;
- FILE *file;
- file = fopen(args->filename, "wb");
- do {
- memset((void*)&buf, 0, BUFF_SIZE);
- if ((res = recv(sockfd, &buf, BUFF_SIZE, 0)) == -1) {
- fprintf(stderr, "recv error: %s\r\n", gai_strerror(res));
- close(sockfd);
- exit(1);
- }
- fwrite (buf , 1 , sizeof(buf) , file );
- } while (res == BUFF_SIZE);
- fclose(file);
- printf(": Filetransfer complete: received '%s' from %s\r\n", args->filename, args->from);
- freeaddrinfo(servinfo);
- close(sockfd);
- }
Add Comment
Please, Sign In to add comment