recvfile.cpp

00001 #ifndef WIN32
00002    #include <arpa/inet.h>
00003    #include <netdb.h>
00004 #else
00005    #include <winsock2.h>
00006    #include <ws2tcpip.h>
00007 #endif
00008 #include <fstream>
00009 #include <iostream>
00010 #include <cstdlib>
00011 #include <cstring>
00012 #include <udt.h>
00013 
00014 using namespace std;
00015 
00016 int main(int argc, char* argv[])
00017 {
00018    if ((argc != 5) || (0 == atoi(argv[2])))
00019    {
00020       cout << "usage: recvfile server_ip server_port remote_filename local_filename" << endl;
00021       return -1;
00022    }
00023 
00024    // use this function to initialize the UDT library
00025    UDT::startup();
00026 
00027    struct addrinfo hints, *peer;
00028 
00029    memset(&hints, 0, sizeof(struct addrinfo));
00030    hints.ai_flags = AI_PASSIVE;
00031    hints.ai_family = AF_INET;
00032    hints.ai_socktype = SOCK_STREAM;
00033 
00034    UDTSOCKET fhandle = UDT::socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
00035 
00036    if (0 != getaddrinfo(argv[1], argv[2], &hints, &peer))
00037    {
00038       cout << "incorrect server/peer address. " << argv[1] << ":" << argv[2] << endl;
00039       return -1;
00040    }
00041 
00042    // connect to the server, implict bind
00043    if (UDT::ERROR == UDT::connect(fhandle, peer->ai_addr, peer->ai_addrlen))
00044    {
00045       cout << "connect: " << UDT::getlasterror().getErrorMessage() << endl;
00046       return -1;
00047    }
00048 
00049    freeaddrinfo(peer);
00050 
00051 
00052    // send name information of the requested file
00053    int len = strlen(argv[3]);
00054 
00055    if (UDT::ERROR == UDT::send(fhandle, (char*)&len, sizeof(int), 0))
00056    {
00057       cout << "send: " << UDT::getlasterror().getErrorMessage() << endl;
00058       return -1;
00059    }
00060 
00061    if (UDT::ERROR == UDT::send(fhandle, argv[3], len, 0))
00062    {
00063       cout << "send: " << UDT::getlasterror().getErrorMessage() << endl;
00064       return -1;
00065    }
00066 
00067    // get size information
00068    int64_t size;
00069 
00070    if (UDT::ERROR == UDT::recv(fhandle, (char*)&size, sizeof(int64_t), 0))
00071    {
00072       cout << "send: " << UDT::getlasterror().getErrorMessage() << endl;
00073       return -1;
00074    }
00075 
00076    if (size < 0)
00077    {
00078       cout << "no such file " << argv[3] << " on the server\n";
00079       return -1;
00080    }
00081 
00082    // receive the file
00083    fstream ofs(argv[4], ios::out | ios::binary | ios::trunc);
00084    int64_t recvsize; 
00085    int64_t offset = 0;
00086 
00087    if (UDT::ERROR == (recvsize = UDT::recvfile(fhandle, ofs, offset, size)))
00088    {
00089       cout << "recvfile: " << UDT::getlasterror().getErrorMessage() << endl;
00090       return -1;
00091    }
00092 
00093    UDT::close(fhandle);
00094 
00095    ofs.close();
00096 
00097    // use this function to release the UDT library
00098    UDT::cleanup();
00099 
00100    return 0;
00101 }

Generated on 9 Feb 2013 for barchart-udt-core-2.2.2 by  doxygen 1.6.1