UDT源码剖析(二):UDT自带例程recvfile注释
生活随笔
收集整理的這篇文章主要介紹了
UDT源码剖析(二):UDT自带例程recvfile注释
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這幾天學習了UDT自帶的例子,結合源碼進行了測試如下:
#ifndef WIN32
? ?#include <arpa/inet.h>? ?#include <netdb.h>
#else
? ?#include <winsock2.h>
? ?#include <ws2tcpip.h>
#endif
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <udt.h>
using namespace std;
int main(int argc, char* argv[])
{
? ?if ((argc != 5) || (0 == atoi(argv[2])))
? ?{
? ? ? cout << "usage: recvfile server_ip server_port remote_filename local_filename" << endl;
? ? ? return -1;
? ?}
? ?// use this function to initialize the UDT library
? ?// 初始化UDT庫
? ?UDT::startup();
? ?// 地址信息提示、目標的地址信息指針
? ?struct addrinfo hints, *peer;
? ?// 內存初始化,全部置為0,設置同sendfile
? ?memset(&hints, 0, sizeof(struct addrinfo));
? ?hints.ai_flags = AI_PASSIVE;
? ?hints.ai_family = AF_INET;
? ?hints.ai_socktype = SOCK_STREAM;
? ?// 創建一個本地Socket
? ?UDTSOCKET fhandle = UDT::socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
? ?// 根據傳入參數和地址信息提示創建目標地址信息
? ?if (0 != getaddrinfo(argv[1], argv[2], &hints, &peer))
? ?{
? ? ? cout << "incorrect server/peer address. " << argv[1] << ":" << argv[2] << endl;
? ? ? return -1;
? ?}
? ?// connect to the server, implict bind
? ?// 連接到服務器,第一個參數為Socket,第二個為連接地址,第三個為地址長度(底層用來判斷是否符合IPv4、IPv6標準的長度)
? ?if (UDT::ERROR == UDT::connect(fhandle, peer->ai_addr, peer->ai_addrlen))
? ?{
? ? ? cout << "connect: " << UDT::getlasterror().getErrorMessage() << endl;
? ? ? return -1;
? ?}
? ?// 釋放通過getaddrinfo分配的地址信息
? ?freeaddrinfo(peer);
? ?// send name information of the requested file
? ?// 發送請求的文件名稱
? ?int len = strlen(argv[3]);
? ?// 向目標發送文件名的長度
? ?// 第一個參數為Socket,第二個參數為數據地址,第三個參數為數據長度,第四個參數會被底層忽略,沒有意義
? ?if (UDT::ERROR == UDT::send(fhandle, (char*)&len, sizeof(int), 0))
? ?{
? ? ? cout << "send: " << UDT::getlasterror().getErrorMessage() << endl;
? ? ? return -1;
? ?}
? ?// 向目標發送文件名
? ?// 第一個參數為Socket,第二個參數為數據地址,第三個參數為數據長度,第四個參數會被底層忽略,沒有意義
? ?if (UDT::ERROR == UDT::send(fhandle, argv[3], len, 0))
? ?{
? ? ? cout << "send: " << UDT::getlasterror().getErrorMessage() << endl;
? ? ? return -1;
? ?}
? ?// get size information
? ?// 接收文件大小
? ?int64_t size;
? ?// 接收一個文件大小數據,內容為一個int64_t值,長度為int64_t的長度,數值保存在size中
? ?// 第一個參數為Socket,第二個參數為數據保存地址,第三個參數為數據長度,第四個參數底層會忽略,沒有意義
? ?if (UDT::ERROR == UDT::recv(fhandle, (char*)&size, sizeof(int64_t), 0))
? ?{
? ? ? cout << "send: " << UDT::getlasterror().getErrorMessage() << endl;
? ? ? return -1;
? ?}
? ?// 負值表示沒有該文件
? ?if (size < 0)
? ?{
? ? ? cout << "no such file " << argv[3] << " on the server\n";
? ? ? return -1;
? ?}
? ?// receive the file
? ?// 開始接收文件
? ?// 通過二進制方式寫入文件,如果文件已存在則覆蓋
? ?fstream ofs(argv[4], ios::out | ios::binary | ios::trunc);
? ?// 接收到的數據大小以及偏移量
? ?int64_t recvsize;
? ?int64_t offset = 0;
? ?// 從0位置開始接收文件數據
? ?// 第一個參數為Socket,第二個參數為文件流,第三個參數為偏移量,第四個參數為文件大小
? ?if (UDT::ERROR == (recvsize = UDT::recvfile(fhandle, ofs, offset, size)))
? ?{
? ? ? cout << "recvfile: " << UDT::getlasterror().getErrorMessage() << endl;
? ? ? return -1;
? ?}
? ?// 關閉連接Socket
? ?UDT::close(fhandle);
? ?// 關閉文件流
? ?ofs.close();
? ?// use this function to release the UDT library
? ?// 釋放UDT庫
? ?UDT::cleanup();
? ?return 0;
}
總結
以上是生活随笔為你收集整理的UDT源码剖析(二):UDT自带例程recvfile注释的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自己处理公式
- 下一篇: s7200cpu224xp手册_S7-2