最近在学习socket编程,编写了一个客户端程序和一个服务器端程序,通过两个程序传输文件,但是老出现错误,我把源代码贴上来,请大家帮我看看

解决方案 »

  1.   

    这是接受端的源代码void CFilerecieveDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    AfxSocketInit();
    // CSocket recso,seversocket;
    SOCKADDR_IN client;
    if(!recso.Socket())
    {
    MessageBox("初始化失败");
    return;
    }
    BOOL boptvalue=TRUE;
    int boptlen=sizeof(BOOL);
    recso.SetSockOpt(SO_REUSEADDR,(void *)&boptvalue,boptlen,SOL_SOCKET);
    if (!recso.Bind(11024))
    {
    MessageBox("绑定失败");
    return;
    }
    if (!recso.Listen(10))
    {
    MessageBox("监听失败");
    return;
    }
    if (!recso.Accept(seversocket))
    {
    MessageBox("接受失败");
    return;
    }
    else
    {
    //client=seversocket.
    while (1)
    {
    saveYouFiles(seversocket,client);
    }



    }

    }
    int CFilerecieveDlg::saveYouFiles(CSocket &recSo, SOCKADDR_IN &client)
    {

    CString fnames;
    CString  filenamepath;
    //static int filescount=0;
    CString strfileIp,strfileName,strfileLength;
    char filemes[512]={0};//存文件名字的
    int fileLengths=0;
    FOLDERINFO myFileInfo;
    recSo.Receive(&myFileInfo,sizeof(FOLDERINFO));
    fileLengths=myFileInfo.fileLength;
    /*strfileIp.Format(inet_ntoa(client.sin_addr));*/
    strfileName.Format(myFileInfo.fileName);                 //得到文件名字
    strfileLength.Format("%f",myFileInfo.fileLength/1024.0);
    filenamepath.Format(myFileInfo.filePath);                 //得到文件路径
    int a=filenamepath.ReverseFind(_T('\\')); //这5行代码是用来得到一个例如:\AMD\Intel\ATI\ filenamepath=filenamepath.Left(a+1); //主要是用来建立目录
    CString b=filenamepath;
    a=b.ReverseFind(_T(':'));
    b=b.Right(b.GetLength()-a-1);
    filenamepath="files"+b;                 //所有的文件都存在files文件夹下
    CreateDirectory(filenamepath,NULL);            //创建目录
    strcpy(filemes,filenamepath+strfileName);
    //strcpy(filemes,filenamepath);

    /*
    byte* data = new byte[fileLengths];
    recSo.Receive(data,fileLengths);
    CFile fs(filemes,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary); //存文件
    fs.Write(data,fileLengths+1);*/


        char bufs[1024];                           //缓冲SIZEFILE=1024
            //byte* data = new byte[1024];
    CFile fs(filemes,CFile::modeCreate|CFile::modeWrite |CFile::typeBinary); //存文件
    int n=0; //接受的字节数 0表示结束
    int temp=0;

    //开始接收


    for(;;)
    {
    n=recSo.Receive(bufs,1024); //接受
    if(n==0)  //0表示结束
    break;  //接受完毕

    fs.Write(bufs,n);
    // fputs(bufs,fp);
    temp+=n;


    }

    // delete []data;
    fs.Close();


    //recSo.Close();
    return 0;
    }
      

  2.   

    这是自定义的一个结构体,存储文件的信息
    struct FOLDERINFO {

    int fileLength;                    //记录文件长度

    char fileName[128];                //记录文件名

    char filePath[512];                //记录文件路径
    byte* data;
    };
      

  3.   

    这是发送端的代码
    void CFilesendDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    AfxSocketInit();
    // CSocket sockClient;
    sockClient.Create();                         //创建Socket
    sockClient.Connect("127.0.0.1", 11024);       //
    Recurse(_T("E:\\test"));
    sockClient.Close(); 
    }void CFilesendDlg::Recurse(LPCTSTR pstr)
    {
    CFileFind finder;
    /*
    AfxSocketInit();
    CSocket sockClient;
    sockClient.Create();                         //创建Socket
    sockClient.Connect("127.0.0.1", 11024);       //*/


    // build a string with wildcards

    CString strWildcard(pstr);
    strWildcard += _T("\\*.*");

    // start working for files

    BOOL bWorking = finder.FindFile(strWildcard);

    while (bWorking)
    {
    bWorking = finder.FindNextFile();

    // skip . and .. files; otherwise, we'd

    // recur infinitely!


    if (finder.IsDots())
    continue;

    CString sFileName = finder.GetFileName();
    CString filepath=finder.GetFilePath();
    //cout << (LPCTSTR)sFileName << endl;//输出查找文件夹下的所有文件名
    if (finder.IsDirectory())
    {

    Recurse(_T(filepath));
    return;
    }

    ///////////////////////////////////////////////////////////////////
    CFile myFile;
    FOLDERINFO myFileInfo;
    if(!myFile.Open(_T(filepath/*"E:\\test\\dixin.txt"*/),CFile::modeRead|CFile::typeBinary,NULL))//m_fileName是遍历后得到的文件
    {
    return ;




    myFileInfo.fileLength=myFile.GetLength();          //得到文件大小
    strcpy(myFileInfo.fileName,myFile.GetFileName());    //得到文件名称
    strcpy(myFileInfo.filePath,filepath/*"E:\test\dixin.txt"*/);
    //myFileInfo.myfilesend=&myFile;
    sockClient.Send(&myFileInfo,sizeof(myFileInfo));     //发送文件信息
    /*
    byte* data = new byte[myFile.GetLength()];
    myFile.Read(data, myFile.GetLength());
    sockClient.Send(data, myFile.GetLength());*/






    myFile.Seek(0,CFile::begin);
    char m_buf[1024]={0};                           //缓冲区
    CString strError;
    int num=0;
    int temp=0;
    int end=0;
    //开始发送
    for(;;)
    {
    num=myFile.Read(m_buf, 1024);
    if(num==0) break;
    end=sockClient.Send(m_buf, num); 
    temp+=end;  
    }





    //delete []data;
    myFile.Close();

    // return ;


    }
    //sockClient.Close(); 
    finder.Close();}
      

  4.   

    发送端发送完信息后,接受端应该子啊recSo.Receive(&myFileInfo,sizeof(FOLDERINFO))处停止,但是接受再这里没有停止,而是接受了一些错误的信息,这些错误的信息在创建新文件时产生错误。还有一点很奇怪的,传输多个文件时,运行程序时只能传输一个文件就报错,但DEBUG时可以把多个文件都传输过来。
      

  5.   

    刚才发的有点乱,把错误再发一遍发送端发送完信息后,接受端应该在recSo.Receive(&myFileInfo,sizeof(FOLDERINFO))处停止,但是接受端在这里没有停止,而是接受了一些错误的信息,这些错误的信息在创建新文件时产生错误。还有一点很奇怪的,传输多个文件时,运行程序时只能传输一个文件就报错,但DEBUG时可以把多个文件都传输过来。
      

  6.   

    http://download.csdn.net/source/2373543
    不知是否是你想要 的