void 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[1500]; struct hostent *hst; struct sockaddr *addrs; int msglen; int error = 0; int try_error=0; int stream = 1; //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; if ( localfile == NULL ) fd = 0; fd = open(localfile,O_RDONLY); if(fd==-1) { printf("Read file error.\n"); exit(1); } fprintf(stderr," ready to send...\n"); printf("Ready to send file....\n"); msglen=read(fd,message,1024); do { /* Initialize the message struct we use to pass * messages to the remote socket. */ try_error++; if(try_error==100) { printf("Can't connect remote address,program shutdown.\n"); exit(1); } iov.iov_base = message; iov.iov_len = msglen; outmsg.msg_iov = &iov; outmsg.msg_iovlen = 1; outmsg.msg_control = outcmsg;; outmsg.msg_controllen = sizeof(outcmsg); outmsg.msg_name = &remote_addr; outmsg.msg_namelen = ra_len; cmsg = CMSG_FIRSTHDR(&outmsg); cmsg->cmsg_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 = 1; sinfo->sinfo_ppid=rand(); error = sendmsg(sk, &outmsg, MSG_WAITALL); if (error != msglen) printf(stderr, ": error: %s.\n", strerror(errno)); } while (error != msglen); /* 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; /* open the file */ while ( (msglen = read(fd,buffer,1024)) > 0 ) { /* Send to our neighbor. */ if(stream==1) stream=2; if(stream==2) stream=1; sinfo->sinfo_stream = stream; iov.iov_len = msglen; sendmsg(sk, &outmsg, MSG_WAITALL); } close(fd); close(sk); }