• <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-07-14來源:作者:點擊數: 標簽:
    作者Vicken Simonian. 環境:VC6 SP4,NT4 SP5 這里有兩個在兩臺計算機之間傳輸文件的函數。在我身邊并沒有看到什么好的CSOCKET文件傳輸函數,于是我決定幫你寫一個。此代碼分為Server端和Client端。 Server(發送)端: void SendFile() { #define PORT 34000
    作者  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.Aclearcase/" target="_blank" >ccept(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

    老湿亚洲永久精品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>