资源简介
这个一个利用TCP协议,实现文件传输的程序,是单线程的,并可以显示传输速度。对学习文件传输的流程,理解TCP传输有很大的帮助。
我学习过了给大家共享一下。
代码片段和文件信息
/**
Offload file transfer module using socket to test TCP offload engine(TOE).
By Vino @ 9th June 2004.
Email: rtc@eyou.com
All copyrights reserved.
*/
#include
#include
#include
#include
#include
#include
#ifdef WIN32
#include
#include
#else
#include
#include
#include
#include
#include
#endif
#include
#include “olfile.h“
const char str_usg[] =
“\nTransfer a file from data source to data sink through network.\n\n“
“ olfile -src sourcefile -snk sinkfile -ip dotip [-y]\n“
“ Copy sourcefile on local to sinkfile on ip dotip.\n\n“
“ olfile -snk sinkfile -src sourcefile -ip dotip [-y]\n“
“ Copy sourcefile on ip dotip to sinkfile on local.\n\n“
“ olfile -server\n“
“ Start olfile service.\n\n“
“Options:\n“
“ -src + sourcefile Data source file full path\n“
“ -snk + sinkfile Data sink file full path\n“
“ -ip + dotip Dot divided ip address\n“
“ -y Default to delete exists file.\n\n“
“Example:\n“
“ -snk f:/data.txt -src f:/datarcv.txt -ip 10.190.5.179 -y\n\n“
“By Vino.Cui @ 9th June 2004 from Inventec.\n“
“Revision:\n“
“1. 13th June 2004 Completed the original version: Transfering file \nfrom data source to data sink via ip specified by -ip argument. \n“;
const size_t OP_HEAD_SIZE = sizeof(struct st_op_head);
struct st_reg_sem s_reg_sem;
/** these 4 buffer pools are dynamic allocated in the program.
file -> read-File Buffer Pool -> send-NIC Buffer Pool -> NIC
file <- write-File Buffer Pool <- receive-NIC Buffer Pool <- NIC
but this implementation needs buffer copy(copy read-file Buffer to send-NIC Buffer pool and back-forth).
char *rdFileBufPool *wrFileBufPool;
char *sendNicBufPool *recvNicBufPool;
*/
char *lanin_buf *lanout_buf;
char *gfin_buf *gfout_buf;
float ftop_spd = 0.0; // top speed bytes/s
/***/
int
init_socket()
{
#ifdef WIN32
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(22) &wsaData);
if (iResult != NO_ERROR)
{
printf(“Error at WSAStartup()\n“);
return -1;
}
return 0;
#else
return 0;
#endif
}
int
create_conn(){
SOCKET s;
struct sockaddr_in local;
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
int iOptLen = sizeof(int);
if((s = socket(AF_INET_NORMAL SOCK_STREAM 0)) == -1){
perror(“socket“);
return -1;
}
int a= setsockopt(s SOL_SOCKET SO_REUSEADDR (char*)&bOptVal bOptLen);
memset(&(local) 0 sizeof(struct sockaddr_in));
local.sin_family = AF_INET; /* host byte order */
local.sin_port = htons(SERVER_PORT);
local.sin_addr.s_addr = INADDR_ANY;
if ( -1 == bind(s (struct sockaddr*)&local sizeof(struct sockaddr)) )
{
fprintf(stderr “bind error\n“);
show_err();
return (-1);
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2004-06-25 12:08 client
文件 58368 2004-07-07 13:13 client\olfile.ncb
文件 1270 2004-07-07 13:13 client\olfile.plg
文件 4425 2004-06-13 16:10 client\olfile.dsp
文件 54784 2004-06-17 14:10 client\olfile.opt
文件 535 2004-07-07 13:12 client\olfile.dsw
目录 0 2004-06-25 12:07 server
文件 537 2004-06-10 11:42 server\olfiled.dsw
文件 58368 2004-06-17 13:43 server\olfiled.ncb
文件 1300 2004-06-17 13:31 server\olfiled.plg
文件 4436 2004-06-13 16:10 server\olfiled.dsp
文件 54784 2004-06-17 13:43 server\olfiled.opt
文件 727 2004-06-25 11:08 Makefile
文件 31205 2004-07-07 13:14 olfile.cpp
文件 4116 2004-07-07 13:14 olfile.h
文件 1725 2004-07-07 13:31 readme.txt
文件 3189 2004-04-19 10:52 代码中国.txt
文件 126 2004-04-03 17:45 代码中国.url
文件 3930 2003-11-05 18:50 说明.htm
----------- --------- ---------- ----- ----
283825 19
- 上一篇:TDMA算法 C语言编写
- 下一篇:高效 C++ 屏幕找图 屏幕搜索 透明找图
评论
共有 条评论