| OLD | NEW |
| (Empty) |
| 1 #ifndef WIN32 | |
| 2 #include <arpa/inet.h> | |
| 3 #include <netdb.h> | |
| 4 #else | |
| 5 #include <winsock2.h> | |
| 6 #include <ws2tcpip.h> | |
| 7 #endif | |
| 8 #include <fstream> | |
| 9 #include <iostream> | |
| 10 #include <cstdlib> | |
| 11 #include <cstring> | |
| 12 #include <udt.h> | |
| 13 | |
| 14 using namespace std; | |
| 15 | |
| 16 int main(int argc, char* argv[]) | |
| 17 { | |
| 18 if ((argc != 5) || (0 == atoi(argv[2]))) | |
| 19 { | |
| 20 cout << "usage: recvfile server_ip server_port remote_filename local_filen
ame" << endl; | |
| 21 return -1; | |
| 22 } | |
| 23 | |
| 24 // use this function to initialize the UDT library | |
| 25 UDT::startup(); | |
| 26 | |
| 27 struct addrinfo hints, *peer; | |
| 28 | |
| 29 memset(&hints, 0, sizeof(struct addrinfo)); | |
| 30 hints.ai_flags = AI_PASSIVE; | |
| 31 hints.ai_family = AF_INET; | |
| 32 hints.ai_socktype = SOCK_STREAM; | |
| 33 | |
| 34 UDTSOCKET fhandle = UDT::socket(hints.ai_family, hints.ai_socktype, hints.ai_
protocol); | |
| 35 | |
| 36 if (0 != getaddrinfo(argv[1], argv[2], &hints, &peer)) | |
| 37 { | |
| 38 cout << "incorrect server/peer address. " << argv[1] << ":" << argv[2] <<
endl; | |
| 39 return -1; | |
| 40 } | |
| 41 | |
| 42 // connect to the server, implict bind | |
| 43 if (UDT::ERROR == UDT::connect(fhandle, peer->ai_addr, peer->ai_addrlen)) | |
| 44 { | |
| 45 cout << "connect: " << UDT::getlasterror().getErrorMessage() << endl; | |
| 46 return -1; | |
| 47 } | |
| 48 | |
| 49 freeaddrinfo(peer); | |
| 50 | |
| 51 | |
| 52 // send name information of the requested file | |
| 53 int len = strlen(argv[3]); | |
| 54 | |
| 55 if (UDT::ERROR == UDT::send(fhandle, (char*)&len, sizeof(int), 0)) | |
| 56 { | |
| 57 cout << "send: " << UDT::getlasterror().getErrorMessage() << endl; | |
| 58 return -1; | |
| 59 } | |
| 60 | |
| 61 if (UDT::ERROR == UDT::send(fhandle, argv[3], len, 0)) | |
| 62 { | |
| 63 cout << "send: " << UDT::getlasterror().getErrorMessage() << endl; | |
| 64 return -1; | |
| 65 } | |
| 66 | |
| 67 // get size information | |
| 68 int64_t size; | |
| 69 | |
| 70 if (UDT::ERROR == UDT::recv(fhandle, (char*)&size, sizeof(int64_t), 0)) | |
| 71 { | |
| 72 cout << "send: " << UDT::getlasterror().getErrorMessage() << endl; | |
| 73 return -1; | |
| 74 } | |
| 75 | |
| 76 if (size < 0) | |
| 77 { | |
| 78 cout << "no such file " << argv[3] << " on the server\n"; | |
| 79 return -1; | |
| 80 } | |
| 81 | |
| 82 // receive the file | |
| 83 fstream ofs(argv[4], ios::out | ios::binary | ios::trunc); | |
| 84 int64_t recvsize; | |
| 85 int64_t offset = 0; | |
| 86 | |
| 87 if (UDT::ERROR == (recvsize = UDT::recvfile(fhandle, ofs, offset, size))) | |
| 88 { | |
| 89 cout << "recvfile: " << UDT::getlasterror().getErrorMessage() << endl; | |
| 90 return -1; | |
| 91 } | |
| 92 | |
| 93 UDT::close(fhandle); | |
| 94 | |
| 95 ofs.close(); | |
| 96 | |
| 97 // use this function to release the UDT library | |
| 98 UDT::cleanup(); | |
| 99 | |
| 100 return 0; | |
| 101 } | |
| OLD | NEW |