2015-03-13 169 views
-1

嗯,我正在嘗試編寫一個C#服務器應用程序,它將接收來自用C++編寫的幾個客戶端的文件,主要問題是我對C#.Net很新。C#異步套接字文件傳輸

設法爲同步套接字C#服務器編寫一個代碼,但無法弄清楚如何爲異步套接字字節明智地接收數據並將它們寫入OnDataReceive回調中的單個文件。

public void OnDataReceived(IAsyncResult asyn) 
{ 
    try 
    { 
    SocketPacket socketData = (SocketPacket)asyn.AsyncState; 

    ////Original code inside this function 
    //int iRx = socketData.m_currentSocket.EndReceive(asyn); 
    //char[] chars = new char[iRx + 1]; 
    //System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); 
    //int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0); 
    //System.String szData = new System.String(chars); 
    //richTextBoxReceivedMsg.AppendText(szData); 

    ////My old code from Sync socket file transfer 
    //Receive data from client socket till it continues 
    byte[] bufferData = new byte[1024]; 
    int recvBytesLen = 0; 
    BinaryWriter bWrite = new BinaryWriter(File.Open(m_strCurrFolder + "File", FileMode.Append)); 
    do 
    { 
     recvBytesLen = clientSock.Receive(bufferData, bufferData.Length, 0); 
     bWrite.Write(bufferData, 0, recvBytesLen); 
    } while (recvBytesLen > 0); 

    //Closing file and socket once done 
    bWrite.Close(); 

    // Continue the waiting for data on the Socket 
    WaitForData(socketData.m_currentSocket); 
    } 
    catch (ObjectDisposedException) 
    { 
    System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); 
    } 
    catch (SocketException se) 
    { 
    MessageBox.Show(se.Message); 
    } 
} 
+0

這是什麼問題? – Claies 2015-03-13 13:05:35

+0

您已經在使用'recvBytesLen = clientSock.Receive(bufferData,bufferData.Length,0);'[Socket.Receive](https://msdn.microsoft.com/en-us/library/ 8s4y8aff%28v = vs.110%29.aspx) – 2015-03-13 13:10:08

+0

這是一個正確的方式來循環異步套接字服務器內的數據? – hypheni 2015-03-13 13:47:31

回答

0

找到這個鏈接是有幫助的,做我的工作。 https://code.msdn.microsoft.com/windowsapps/Fixed-size-large-file-dfc3f45d

private static void ReceiveCallback(IAsyncResult ar) 
{ 
    StateObject state = (StateObject)ar.AsyncState; 
    Socket clientSocket = state.WorkSocket; 
    BinaryWriter writer; 


    int bytesRead = clientSocket.EndReceive(ar); 
    if (bytesRead > 0) 
    { 
     //If the file doesn't exist, create a file with the filename got from server. If the file exists, append to the file. 
     if (!File.Exists(fileSavePath)) 
     { 
      writer = new BinaryWriter(File.Open(fileSavePath, FileMode.Create)); 
     } 
     else 
     { 
      writer = new BinaryWriter(File.Open(fileSavePath, FileMode.Append)); 
     } 


     writer.Write(state.Buffer, 0, bytesRead); 
     writer.Flush(); 
     writer.Close(); 


     // Notify the progressBar to change the position. 
     Client.BeginInvoke(new ProgressChangeHandler(Client.ProgressChanged)); 


     // Recursively receive the rest file. 
     try 
     { 
      clientSocket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); 
     } 
     catch 
     { 
      if (!clientSocket.Connected) 
      { 
       MessageBox.Show(Properties.Resources.DisconnectMsg); 
      } 
     } 
    } 
    else 
    { 
     // Signal if all the file received. 
     receiveDone.Set(); 
    } 
} 
+0

非常感謝您指出我。現在,當同時連接多個客戶端時,我認爲這個回調應該有一個單獨的線程,但我不確定回調實際上在低級別運行它自己的線程。 有人嗎? – hypheni 2015-03-13 14:26:04