小弟刚学习网络编程,想编写一个局域网的文件传输的程序,可是总是调试不成功。或是我的思路都错了。现在把我写的源代码的核心部分发出来希望有达人帮偶解决一下!谢谢!!!
sendfile:
void CSendFileDlg::OnFileSend() 
{
// TODO: Add your control notification handler code here
if(m_path=="")
AfxMessageBox("请输入您要发送文件的路径。");
else
{
UpdateData(FALSE);
unsigned  char  *pIP;  
CString  strIP; 
DWORD  dwIP;  
m_IpAddr.GetAddress(dwIP);
pIP  =  (unsigned  char*)&dwIP; 
strIP.Format("%u.%u.%u.%u",*(pIP+3),*(pIP+2),*(pIP+1),  *pIP);
if(dwIP==NULL)
::AfxMessageBox("请输入对方的IP地址:");
    else
{
CSocket iSend;
sockaddr_in iSendAddr;
iSendAddr.sin_family=AF_INET;
iSendAddr.sin_port=htons(8200);
iSendAddr.sin_addr.S_un.S_addr=inet_addr(strIP);
iSend.Create(8200,SOCK_STREAM);
if(iSend.Connect((struct sockaddr *) &iSendAddr,sizeof(SOCKADDR_IN))==SOCKET_ERROR)
::AfxMessageBox("连接错误!");
else
{
char *fpointer=new char[512];
unsigned long filelen=512;
unsigned long i=1;
unsigned long fsize;
DWORD len;
CFile file;
file.Open(m_path,CFile::modeRead|CFile::shareExclusive,NULL);
fsize=file.GetLength();
do
{
file.Read(fpointer,sizeof(fpointer));
len=iSend.Send(fpointer,sizeof(fpointer),0);
if(len<0)
{
AfxMessageBox("文件发送错误,退出!");
exit(0);
}
file.Seek(filelen*i,CFile::begin);
i++;
}while(filelen*i!=fsize);
AfxMessageBox("文件发送完毕,退出!");
}
}
}
}void CSendFileDlg::OnFileFind() 
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
if(dlg.DoModal()==IDOK)
{
m_path=dlg.GetPathName();
UpdateData(FALSE);
}
}
recvfile:
void CRecvFileDlg::OnFOk() 
{
// TODO: Add your control notification handler code here
if(m_ipaddr.IsBlank())
AfxMessageBox("请输入本机ip地址!");
else
{
char buff[1024];
int iport=8200;
int recvlen;
int filelen=512;
unsigned  char  *pIP;
CString  strIP;
DWORD  dwIP;
m_ipaddr.GetAddress(dwIP);
pIP  =  (unsigned  char*)&dwIP;
strIP.Format("%u.%u.%u.%u",*(pIP+3),  *(pIP+2),  *(pIP+1),  *pIP);
sockaddr_in server;
sockaddr_in client;
server.sin_family=AF_INET;
server.sin_addr.S_un.S_addr=inet_addr("10.1.2.2");
server.sin_port=htons(u_short(iport));
iServer=socket(AF_INET,SOCK_STREAM,0);
::bind(iServer,(struct sockaddr *)&server,sizeof(server));
listen(iServer,5);
int clen=sizeof(client);
iClient=::accept(iServer,(struct sockaddr *)&client,&clen);
CFile file;
file.Open(m_fpath,CFile::modeCreate|CFile::modeWrite,NULL);
do
{
memset(buff,0,sizeof(buff));
recvlen=::recv(iClient,buff,sizeof(buff),0);
file.Seek(filelen,CFile::current);
file.Write(buff,sizeof(buff));
}while(recvlen>0);
file.Close();
::closesocket(iClient);
::AfxMessageBox("文件接收完毕!");

}
}
void CRecvFileDlg::OnRecv() 
{
// TODO: Add your control notification handler code here
CFileDialog fdlg(FALSE);
if(fdlg.DoModal()==IDOK)
{
m_fpath=fdlg.GetFileName();
}
UpdateData(FALSE);
}

解决方案 »

  1.   

    file.Read(fpointer,sizeof(fpointer));
    len=iSend.Send(fpointer,sizeof(fpointer),0);
    4==sizeof(fpointer)?
      

  2.   

    ^_^
    谢谢楼上的咯
    不过我觉得最重要的不是那里啊
    我把file.Read(fpointer,sizeof(fpointer));改了后还是不行啊
    连不上去哈
      

  3.   

    len=iSend.Send(fpointer,sizeof(fpointer),0);
    if(len<0)是在这里错了吗?用WSAGetLastError看看是什么错误,然后贴上来
      

  4.   

    应该不是这里出错了啊
    在server端监听的时候根本就收不到客户端的连接啊
    还有就是server端一运行到linsten()函数这里的时候程序就仿佛死了一样
    我晓得listen是一个阻塞调用
    可是怎么解决让程序运行到listen函数的时候不出现上述情况啊?!
      

  5.   

    你这种代码进行文件传输的话太不可靠了, 首先就不能断点续传了,最好用CAsyncSocket吧,文件传输不需要使用到阻塞形的socket