📄 sendfile.cpp.svn-base
字号:
#ifndef WIN32 #include <cstdlib>#else #include <winsock2.h> #include <ws2tcpip.h>#endif#include <fstream>#include <iostream>#include <cassert>#include <udt.h>using namespace std;#ifdef UDT_FT_USE_NON_BLOCKING# define UDT_FT_SND_BUF_SIZE 4096000# define UDT_FT_RCV_BUF_SIZE 4096000#else# define UDT_FT_SND_BUF_SIZE 256 * 1024# define UDT_FT_RCV_BUF_SIZE UDT_FT_SND_BUF_SIZE #endif#define UDT_FT_MSS 1400// staticbool setSocketOpt(UDTSOCKET udtSocket){ bool succeed = false; int optValue = UDT_FT_MSS; // !nash! set less MTU int error = UDT::setsockopt(udtSocket, 0, UDT_MSS, &optValue, sizeof(int)); assert(error == 0); succeed |= (error == 0); // !nash! set smaller sending and receiving buffer limit optValue = UDT_FT_SND_BUF_SIZE; // 1MB error = UDT::setsockopt(udtSocket, 0, UDT_SNDBUF, &optValue, sizeof(int)); assert(error == 0); succeed |= (error == 0); optValue = UDT_FT_RCV_BUF_SIZE; error = UDT::setsockopt(udtSocket, 0, UDT_RCVBUF, &optValue, sizeof(int)); assert(error == 0); succeed |= (error == 0); // !nash! set smaller UDP rcv buffer size, set to this value is due to that Darwin has relatively small default UDP rcv buffer optValue = 42080; error = UDT::setsockopt(udtSocket, 0, UDP_RCVBUF, &optValue, sizeof(int)); assert(error == 0); succeed |= (error == 0); return succeed;}int main(int argc, char* argv[]){ //usage: sendfile [server_port] if ((2 < argc) || ((2 == argc) && (0 == atoi(argv[1])))) { cout << "usage: sendfile [server_port]" << endl; return 0; } UDTSOCKET serv = UDT::socket(AF_INET, SOCK_STREAM, 0); setSocketOpt(serv);//#ifdef WIN32// int mss = 1052;// UDT::setsockopt(serv, 0, UDT_MSS, &mss, sizeof(int));//#endif int port = 9000; if (2 == argc) port = atoi(argv[1]); sockaddr_in my_addr; my_addr.sin_family = AF_INET; my_addr.sin_port = htons(port); my_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero), '\0', 8); if (UDT::ERROR == UDT::bind(serv, (sockaddr*)&my_addr, sizeof(my_addr))) { cout << "bind: " << UDT::getlasterror().getErrorMessage() << endl; return 0; } cout << "server is ready at port: " << port << endl; UDT::listen(serv, 1); sockaddr_in their_addr; int namelen = sizeof(their_addr); UDTSOCKET fhandle; if (UDT::INVALID_SOCK == (fhandle = UDT::accept(serv, (sockaddr*)&their_addr, &namelen))) { cout << "accept: " << UDT::getlasterror().getErrorMessage() << endl; return 0; } else { cout << "accepted" << endl; } setSocketOpt(fhandle); UDT::close(serv); // aquiring file name information from client char file[1024]; int len; if (UDT::ERROR == UDT::recv(fhandle, (char*)&len, sizeof(int), 0)) { cout << "recv: " << UDT::getlasterror().getErrorMessage() << endl; return 0; } if (UDT::ERROR == UDT::recv(fhandle, file, len, 0)) { cout << "recv: " << UDT::getlasterror().getErrorMessage() << endl; return 0; } file[len] = '\0'; // open the file ifstream ifs(file, ios::in | ios::binary); ifs.seekg(0, ios::end); int64_t size = ifs.tellg(); ifs.seekg(0, ios::beg); // send file size information if (UDT::ERROR == UDT::send(fhandle, (char*)&size, sizeof(int64_t), 0)) { cout << "send: " << UDT::getlasterror().getErrorMessage() << endl; return 0; } else { cout << "File: " << file << " | Size: " << size << endl; } UDT::TRACEINFO trace; UDT::perfmon(fhandle, &trace); UDT::UDSET writeFds; timeval tv; int64_t sizeSent = 0; do { writeFds.clear(); UD_SET(fhandle, &writeFds); tv.tv_sec = 0; tv.tv_usec = 500; if (UDT::select(0, NULL, &writeFds, NULL, &tv)) { if (UD_ISSET(fhandle, &writeFds)) { // send the file int sizeToSend = 8192; if (size - sizeSent < 8192) { sizeToSend = size - sizeSent; } int ssent = UDT::sendfile(fhandle, ifs, sizeSent, sizeToSend); if (UDT::ERROR == ssent) { cout << "sendfile: " << UDT::getlasterror().getErrorMessage() << endl; return 0; } else { sizeSent += ssent; //cout << "Size Sent: " << sizeSent << endl; } } } } while(sizeSent < size); UDT::perfmon(fhandle, &trace); cout << "speed = " << trace.mbpsSendRate << endl; UDT::close(fhandle); ifs.close(); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -