oid send_file(int sk) { struct msghdr outmsg; char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; struct cmsghdr *cmsg; struct sctp_sndrcvinfo *sinfo; struct iovec iov; char message[1024]; struct hostent *hst; struct sockaddr *addrs; int msglen; int error = 0; int try_error=0; int stream = 0; //Kiem lai dia chi dich va gan cac thong tin phu hop if (remote_host != NULL) { hst = gethostbyname(remote_host); if (hst == NULL || hst->h_length < 1) { fprintf(stderr, "%s: bad hostname: \n", remote_host); exit(1); } ra_family = hst->h_addrtype; ra_len = sizeof(remote_addr); ra_raw = &remote_addr.sin_addr; remote_addr.sin_port = htons(8000); remote_addr.sin_family = AF_INET; memcpy(ra_raw, hst->h_addr_list[0], hst->h_length); } else { //Hostname khong ton tai thoat chuong trinh... printf("Not found remote host!!!\n"); printf("Shutdown program.\n"); exit(1); } printf("Host name is %s.\n",remote_host); // KIem tra su ton tai cua ket noi toi remote addrs if ((SOCK_SEQPACKET == socket_type) && associd && (0 != test_sk_for_assoc(sk, associd))) { associd = test_recv_assoc_change(sk); printf("Old association gone, Starting a new one!\n"); new_connection = 1; } int fd[10]; int check_file=0; for(check_file=0;check_filecmsg_level = IPPROTO_SCTP; cmsg->cmsg_type = SCTP_SNDRCV; cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); outmsg.msg_controllen = cmsg->cmsg_len; sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); sinfo->sinfo_flags = 0; sinfo->sinfo_stream = 0; error = sendmsg(sk, &outmsg, MSG_WAITALL); if (error != sizeof(message)) printf(stderr, ": error: %s.\n", strerror(errno)); } while (error != sizeof(message)); /* If this is the first message sent over a UDP-style socket, * get the associd from the SCTP_ASSOC_CHANGE notification. */ if ((SOCK_SEQPACKET == socket_type) && (0 == associd)) associd = test_recv_assoc_change(sk); /* Verify there is no association. */ if (0 != test_sk_for_assoc(sk, associd)) { printf("No association is present now!!\n"); new_connection = 1; } else { if (new_connection) { int rc = sctp_getpaddrs(sk, associd, &addrs); if (0 >= rc) { if (rc == 0) { fprintf(stderr, "sctp_getpaddrs failed, no peers.\n"); } else { fprintf(stderr, "sctp_getpaddrs failed %s(%d).\n", strerror(errno), errno); } exit(1); } printf("New connection, peer addresses\n"); //print_addr_buf(addrs, rc); sctp_freepaddrs(addrs); new_connection = 0; } } iov.iov_base = buffer; iov.iov_len = 1; outmsg.msg_iov = &iov; outmsg.msg_iovlen = 1; int end_file[count_file]; for(check_file=0;check_filesinfo_stream = stream; iov.iov_len = sizeof(message); error = sendmsg(sk, &outmsg, MSG_WAITALL); } } finish=time(NULL); finish_c=clock(); temp=finish_c-start_c; duration_c=(double)temp/CLOCKS_PER_SEC; duration=finish-start; //duration=finish-start; printf("the file has been transferred in %d secs and %2.3f secs.\n",duration,duration_c); close(fd); close(sk); }