• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    兩臺機器間傳輸文件的函數

    發布: 2007-7-14 21:11 | 作者: 佚名    | 來源: 網絡轉載     | 查看: 26次 | 進入軟件測試論壇討論

    領測軟件測試網 作者  Vicken Simonian.   
    環境:VC6 SP4,NT4 SP5   
    這里有兩個在兩臺計算機之間傳輸文件的函數。在我身邊并沒有看到什么好的CSOCKET文件傳輸函數,于是我決定幫你寫一個。此代碼分為Server端和Client端。   
    Server(發送)端:   
    void SendFile()   
    {   
    #define PORT 34000 /// Select any free port you wish   
        
    AfxSocketInit(NULL);   
    CSocket sockSrvr;   
    sockSrvr.Create(PORT); // Creates our server socket   
    sockSrvr.Listen(); // Start listening for the client at PORT   
    CSocket sockRecv;   
    sockSrvr.Accept(sockRecv); // Use another CSocket to accept the conne   
    ction   
        
        
    CFile myFile;   
    myFile.Open("C:\\ANYFILE.EXE", CFile::modeRead | CFile::typeBinary);   
        
        
    int myFileLength = myFile.GetLength(); // Going to send the correct F   
    ile Size   
        
    sockRecv.Send(&myFileLength, 4); // 4 bytes long   
        
    byte* data = new byte[myFileLength];   
        
    myFile.Read(data, myFileLength);   
        
    sockRecv.Send(data, myFileLength); //Send the whole thing now   
        
    myFile.Close();   
    delete data;   
        
    sockRecv.Close();   
    }   
        
    Client(接收)端:   
    void GetFile(){   
        
    #define PORT 34000 /// Select any free port you wish   
        
    AfxSocketInit(NULL);   
    CSocket sockClient;   
    sockClient.Create();   
        
    // "127.0.0.1" is the IP to your server, same port   
    sockClient.Connect("127.0.0.1", PORT);  int dataLength;   
        
    sockClient.Receive(&dataLength, 4); //Now we get the File Size first   
        
        
    byte* data = new byte[dataLength];   
    sockClient.Receive(data, dataLength); //Get the whole thing   
        
    CFile destFile("C:\\temp\\ANYFILE.EXE",   
      CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);   
        
    destFile.Write(data, dataLength); // Write it destFile.Close(); delet   
    e data;   
        
    sockClient.Close();   
    }   
    (有沒有看到,既然能先傳文件大小,后傳文件,那么仿造這個例子,可以把文件分成多段輸)如果再做一個線程,那就更完美了。   
    在此感謝所有的朋友!Server端必須在Client端之前運行。我相信還有許多可以改進的地方,例如一次傳輸一個文件,可以將它分成多塊(我好像在C版中聽某位大蝦說過CSocket的Send一次最多只能傳64k,不知是對還是錯,如不能傳則將文件分段)。在任何時候都可以很方便地加入到一個工程中。 

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>