我先发言吧:我用基于连接的TCP/IP协议作了一个比较完善的异步发送和接收文件的类,
就是速度比较慢,而且不太稳定。不知道MS在局域网中拷贝文件的时候使用的是什么方法,速度快呆了。
就是速度比较慢,而且不太稳定。不知道MS在局域网中拷贝文件的时候使用的是什么方法,速度快呆了。
解决方案 »
- 在.cpp主程序中函数!!!!
- GetDlgItemText该如何使用
- 怎么对edit筐发消息来显示汉字?
- 请教如何实现新建文档窗口的功能,急
- 新写了个看门狗服务,大家参详参详?
- 怎样是一个对话框能用滚动条。
- glTexImage2D和glDrawPixels的效率哪个高些
- 关于Client, COM对应数据库中datetime和money类型的问题(VC++)
- 有用过TAdvStringGrid的吗?OnGetEditorType( )为何不被调用?
- 如何编程使一个文件夹共享?
- SoftICE的Ctrl+D怎么了?
- 请问在用ATL编写DCOM时候,我想加入一个Dialog对话框,但总是加不进去,或者是加进去了编译也不能通过,应该怎么办呢?
怎么做MSDN里有例子.
但TCP/IP也够快了,我做的发送接受文件的,速度跟MS的拷贝差不多啊.
你是怎么做的,速度如何?
UDP has no congestion control and no data coalescing. This eliminates the delays caused by the delayed ACK and Nagle algorithms. (This is also a disadvantage in many situations, of course.)
There is less code in the UDP section of the stack than the TCP section. This means that there is less latency between a packet arriving at the network card and being delivered to the application.
tcp/ip,udp都可以。ipx也行,我用串口通过modem传诵文件都try过,速度还行啊
QQ用的就是UDP,
没见查多少阿
[email protected]
从编码上说,首先要考虑的是提高有效载荷率,也就是尽可能发送大的数据包。当然了,TCP是流式的,默认状态下会自动组合。但是如果从文件中一次读出的太少,程序本身的循环次数就大大增加,尤其还需要系统调用,效率将大大下降。所以首先应当考虑优化编码。另外从编程角度,则应该尽量提高代码运行速度,这里的技巧就不说了。这一块其实对性能影响最大。
我建议你还使用TCP,因为协议本身虽有性能差别,但是通用和易用更为重要。况且差别没有那么大。UDP需要复杂的状态机,他的优点不在这里,传文件是它最大的弱点。
另外需要澄清,FTP和TFTP都是应用层协议,与TCP/UDP不能并论。FTP使用的是TCP。
[email protected]
不知道Kronus(Kronus)从哪儿听来的
typedef struct tagFILEDATAEX
{
PACKETTYPE pt_type;
UINT uLength;
UINT uSum;
UINT uPos;
UINT uHandleLow;
UINT uHandleHigh;
OBJOPERATE op_type;
REFRESHDATATYPE dt_type;
char szFileName[128];
}FILEDATAEX,*LPFILEDATAEX;函数
int IsSocketReady(UINT sock)
{
fd_set writeFDs;
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 10;
while (TRUE)
{
FD_ZERO(&writeFDs);
FD_SET(sock,&writeFDs);
int fdsSet = select(sock+1, NULL, &writeFDs, NULL, &timeout);
FD_CLR(sock,&writeFDs);
//timeout. Timer expired
if (0 == fdsSet) continue;
if (SOCKET_ERROR == fdsSet)
{
if (WSAEINVAL == GetLastError())
continue;
}
return fdsSet;
}
}
void CConViewDlg::sRefreshData(LPFILEDATAEX lpFileDataEx,CString csFileName)
{
if (FALSE == m_gbLogFlag || NULL == lpFileDataEx || TRUE == csFileName.IsEmpty())
return;
CFile file;
if (0 == file.Open(csFileName,CFile::modeRead))
{
DWORD dwError=GetLastError();
acutPrintf("\n%s文件打不开",csFileName);
return;
}
int nFileLength=file.GetLength();
int nRead=0,nSend=0,nLeft=nFileLength;
//文件缓冲区
HGLOBAL hBuf=GlobalAlloc(GHND,nFileLength);
if (NULL == hBuf)
MSGRETURN("分配缓冲区错误");
PBYTE pbBuf=(PBYTE )GlobalLock(hBuf);
if (NULL == pbBuf)
{
GlobalFree(hBuf);
MSGRETURN("内存锁定错误");
}
nRead=file.ReadHuge((void *)pbBuf,nFileLength);
file.Close();
//2001/12/12 Modify by Julien
const int nSizeOfDataSection=MAXSIZEBUF-sizeof(PACKETHEADER);
int nSum=nFileLength/nSizeOfDataSection;
//文件长度
lpFileDataEx->uLength=htonl(nFileLength);
//总共数据包数目
lpFileDataEx->uSum=htonl(nFileLength%nSizeOfDataSection==0?nSum:nSum+1);
//strcpy(FileDataEx.szGroup,CConViewDlg::m_gLoginData.szGroup);
//strcpy(FileDataEx.szUserName,CConViewDlg::m_gLoginData.szUserName);
CComSocket &pSock=CConViewDlg::m_gskCom;
pSock.Send(lpFileDataEx,sizeof(FILEDATAEX));
sSendFileData(nFileLength,pbBuf);
GlobalUnlock(hBuf);
GlobalFree(hBuf);
}void CConViewDlg::sSendFileData(int nDataLength,LPBYTE pbBuf)
{
if (FALSE == m_gbLogFlag || nDataLength <= 0 || NULL == pbBuf)
return;
const int nSizeOfDataSection=MAXSIZEBUF-sizeof(PACKETHEADER);
int nLength,nSum=0,nLeft=nDataLength,nSend;
PACKETHEADER hdr;
CComSocket &pSock=m_gskCom;
char szBuf[MAXSIZEBUF];
LPBYTE pbSend=pbBuf;
//文件数据
while(nLeft>0)
{
//发送新的数据
nLength=min(nSizeOfDataSection,nLeft);
hdr.pt_type=(PACKETTYPE)htonl(PT_DATA);
hdr.uLength=htonl(nLength);
hdr.uPos=htonl(UINT(pbSend-pbBuf));
hdr.uSum=htonl(nSum);
memcpy(szBuf,&hdr,sizeof(hdr));
memcpy(szBuf+sizeof(hdr),pbSend,nLength);
if (IsSocketReady(pSock.m_hSocket) < 0)
MSGRETURN("网络错误");
nSend=pSock.Send(szBuf,nLength+sizeof(hdr));
if (SOCKET_ERROR==nSend)
{
if(WSAEWOULDBLOCK==GetLastError())
{
continue;
}
CString csMsg;
csMsg.Format("已经发送了 %d 个字节时出错",pbSend-pbBuf);
MSGRETURN(csMsg);
}
nSend-=sizeof(hdr);
if (nSend <= 0)
continue;
pbSend+=nSend;
nLeft-=nSend;
nSum++;
}
//结束数据包
hdr.pt_type=(PACKETTYPE)htonl(PT_END);
hdr.uLength=htonl(0);
hdr.uPos=htonl(0);
hdr.uSum=htonl(nSum);
int nRet;
if ((nRet=IsSocketReady(pSock.m_hSocket)) < 0)
MSGRETURN("网络错误");
pSock.Send(&hdr,sizeof(hdr));
}
Accept,和Connect,不过我用的端口是自定义的,能怎么算呢