private void SendFileCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. DataStateObject state = (DataStateObject)ar.AsyncState; Socket sock = state.workSocket; // Complete sending the data to the remote device. int bytesSent = sock.EndSend(ar); state.BytesTransfered += bytesSent; if (state.BytesTransfered >= m_filesize) { sock.Close(); m_endTick = Environment.TickCount; m_ftpmonitor.TransferCompleted(state.BytesTransfered, m_endTick - m_startTick); } else { m_sock.BeginSend(state.bufferupload, state.BytesTransfered, (int)state.fs.Length, 0, new AsyncCallback(SendFileCallback), state); } } catch (Exception e) { m_lasterror = e.Message; ErrorNotify("DataAsyncSocket.SendFileCallBack"); } }
public void SendFile() { try { m_startTick = Environment.TickCount; // Create the state object. DataStateObject state = new DataStateObject(); state.workSocket = m_sock; state.fs = new FileStream(m_filename, FileMode.Open); state.BytesTransfered = 0; m_filesize = (int)state.fs.Length; state.bufferupload = new byte[state.fs.Length]; state.fs.Read(state.bufferupload, 0, (int)state.fs.Length); state.fs.Close(); // Begin sending the data. m_sock.BeginSend(state.bufferupload, 0, (int)state.bufferupload.Length, 0, new AsyncCallback(SendFileCallback), state); } catch (Exception e) { m_lasterror = e.Message; ErrorNotify("DataAsyncSocket.SendFile"); } }
public void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the async state object. DataStateObject state = (DataStateObject)ar.AsyncState; Socket sock = state.workSocket; // Read data from the remote device. int bytesRead = sock.EndReceive(ar); state.BytesTransfered += bytesRead; if (bytesRead > 0) { // There might be more data, so store the data received so far. string s = Encoding.ASCII.GetString(state.buffer, 0, bytesRead); state.sb.Append(s); if (state.sb.ToString().EndsWith(CRLF)) { m_endTick = Environment.TickCount; m_response = state.sb.ToString(); m_ftpmonitor.TransferCompleted(state.BytesTransfered, m_endTick - m_startTick); } else { // Get the rest of the data. sock.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } } else { m_endTick = Environment.TickCount; m_ftpmonitor.TransferCompleted(state.BytesTransfered, m_endTick - m_startTick); } } catch (Exception e) { m_lasterror = e.Message; ErrorNotify("DataAsyncSocket.ReceiveCallback"); } }
public void Receive() { try { m_startTick = Environment.TickCount; // Create the state object. DataStateObject state = new DataStateObject(); state.workSocket = m_sock; // Begin receiving the data from the remote device. m_sock.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { m_lasterror = e.Message; ErrorNotify("DataAsyncSocket.Receive"); } }
private void ReceiveFileCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the async state object. DataStateObject state = (DataStateObject)ar.AsyncState; Socket sock = state.workSocket; // Read data from the remote device. int bytesRead = sock.EndReceive(ar); // There might be more data, so store the data received so far. state.fs.Write(state.buffer, 0, bytesRead); state.BytesTransfered += bytesRead; //m_ftpmonitor.PourcentDownload(state.BytesTransfered, m_filesize); if (state.BytesTransfered >= m_filesize) { sock.Close(); state.fs.Close(); m_endTick = Environment.TickCount; m_ftpmonitor.TransferCompleted(state.BytesTransfered, m_endTick - m_startTick); } else { // Get the rest of the data. sock.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveFileCallback), state); } } catch (Exception e) { m_lasterror = e.Message; ErrorNotify("DataAsyncSocket.ReceiveFileCallback"); } }
public void ReceiveFile() { try { m_startTick = Environment.TickCount; // Create the state object. DataStateObject state = new DataStateObject(); state.workSocket = m_sock; state.fs = new FileStream(m_filename, FileMode.Create); state.BytesTransfered = 0; // Begin receiving the data from the remote device. m_sock.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveFileCallback), state); } catch (Exception e) { m_lasterror = e.Message; ErrorNotify("DataAsyncSocket.ReceiveFile"); } }
public void SendFile() { try { m_startTick = Environment.TickCount; // Create the state object. DataStateObject state = new DataStateObject(); state.workSocket = m_sock; state.fs = new FileStream(m_filename, FileMode.Open); state.BytesTransfered = 0; m_filesize = (int)state.fs.Length; state.bufferupload = new byte[state.fs.Length]; state.fs.Read(state.bufferupload, 0, (int)state.fs.Length); state.fs.Close(); // Begin sending the data. m_sock.BeginSend( state.bufferupload, 0, (int)state.bufferupload.Length, 0, new AsyncCallback(SendFileCallback), state); } catch (Exception e) { m_lasterror = e.Message; ErrorNotify("DataAsyncSocket.SendFile"); } }