| 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 <srt.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_filename" << endl; |
| 21 |
return -1; |
| 22 |
} |
| 23 |
|
| 24 |
// Use this function to initialize the UDT library |
| 25 |
srt_startup(); |
| 26 |
|
| 27 |
srt_setloglevel(srt_logging::LogLevel::debug); |
| 28 |
|
| 29 |
struct addrinfo hints, *peer; |
| 30 |
|
| 31 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 32 |
hints.ai_flags = AI_PASSIVE; |
| 33 |
hints.ai_family = AF_INET; |
| 34 |
hints.ai_socktype = SOCK_DGRAM; |
| 35 |
|
| 36 |
SRTSOCKET fhandle = srt_create_socket(); |
| 37 |
// SRT requires that third argument is always SOCK_DGRAM. The Stream API is set by an option, |
| 38 |
// although there's also lots of other options to be set, for which there's a convenience option, |
| 39 |
// SRTO_TRANSTYPE. |
| 40 |
SRT_TRANSTYPE tt = SRTT_FILE; |
| 41 |
srt_setsockopt(fhandle, 0, SRTO_TRANSTYPE, &tt, sizeof tt); |
| 42 |
|
| 43 |
if (0 != getaddrinfo(argv[1], argv[2], &hints, &peer)) |
| 44 |
{ |
| 45 |
cout << "incorrect server/peer address. " << argv[1] << ":" << argv[2] << endl; |
| 46 |
return -1; |
| 47 |
} |
| 48 |
|
| 49 |
// Connect to the server, implicit bind. |
| 50 |
if (SRT_ERROR == srt_connect(fhandle, peer->ai_addr, peer->ai_addrlen)) |
| 51 |
{ |
| 52 |
cout << "connect: " << srt_getlasterror_str() << endl; |
| 53 |
return -1; |
| 54 |
} |
| 55 |
|
| 56 |
freeaddrinfo(peer); |
| 57 |
|
| 58 |
// Send name information of the requested file. |
| 59 |
int len = strlen(argv[3]); |
| 60 |
|
| 61 |
if (SRT_ERROR == srt_send(fhandle, (char*)&len, sizeof(int))) |
| 62 |
{ |
| 63 |
cout << "send: " << srt_getlasterror_str() << endl; |
| 64 |
return -1; |
| 65 |
} |
| 66 |
|
| 67 |
if (SRT_ERROR == srt_send(fhandle, argv[3], len)) |
| 68 |
{ |
| 69 |
cout << "send: " << srt_getlasterror_str() << endl; |
| 70 |
return -1; |
| 71 |
} |
| 72 |
|
| 73 |
// Get size information. |
| 74 |
int64_t size; |
| 75 |
|
| 76 |
if (SRT_ERROR == srt_recv(fhandle, (char*)&size, sizeof(int64_t))) |
| 77 |
{ |
| 78 |
cout << "send: " << srt_getlasterror_str() << endl; |
| 79 |
return -1; |
| 80 |
} |
| 81 |
|
| 82 |
if (size < 0) |
| 83 |
{ |
| 84 |
cout << "no such file " << argv[3] << " on the server\n"; |
| 85 |
return -1; |
| 86 |
} |
| 87 |
|
| 88 |
// Receive the file. |
| 89 |
int64_t recvsize; |
| 90 |
int64_t offset = 0; |
| 91 |
|
| 92 |
SRT_TRACEBSTATS trace; |
| 93 |
srt_bstats(fhandle, &trace, true); |
| 94 |
|
| 95 |
if (SRT_ERROR == (recvsize = srt_recvfile(fhandle, argv[4], &offset, size, SRT_DEFAULT_RECVFILE_BLOCK))) |
| 96 |
{ |
| 97 |
cout << "recvfile: " << srt_getlasterror_str() << endl; |
| 98 |
return -1; |
| 99 |
} |
| 100 |
|
| 101 |
srt_bstats(fhandle, &trace, true); |
| 102 |
|
| 103 |
cout << "speed = " << trace.mbpsRecvRate << "Mbits/sec" << endl; |
| 104 |
int losspercent = 100*trace.pktRcvLossTotal/trace.pktRecv; |
| 105 |
cout << "loss = " << trace.pktRcvLossTotal << "pkt (" << losspercent << "%)\n"; |
| 106 |
|
| 107 |
srt_close(fhandle); |
| 108 |
|
| 109 |
// Signal to the SRT library to clean up all allocated sockets and resources. |
| 110 |
srt_cleanup(); |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |