Does this code look like it will work as expected for asynchronous
operations? Or, do my calls to stream.Read and socket.Send need to be
changed to stream.BeginRead and socket.BeginSend?
Thanks
// AsyncTest.cs
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace JLCoady
{
public class AsyncTest
{
#region Fields
private int dataBufferSize = 512;
private int totalBytes;
#endregion
#region Properties
//--------------------------------------------------------------------------
---------
public int DataBufferSize
{
get { return dataBufferSize; }
set { dataBufferSize = value; }
}
//--------------------------------------------------------------------------
---------
public int TotalBytes
{
get { return totalBytes; }
}
#endregion
#region Constructors
//--------------------------------------------------------------------------
---------
public AsyncTest()
{
}
#endregion
#region Methods
//--------------------------------------------------------------------------
---------
private Socket OpenSocket(string hostName, int port)
{
if(hostName == null)
throw new ArgumentNullException("hostName");
Trace.WriteLine(
string.Format("{0}:{1}", hostName, port), "OpenSocket");
Socket result = null;
try
{
result = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
result.Connect(
new IPEndPoint(Dns.Resolve(hostName).AddressList[0], port));
}
catch(Exception exc)
{
CloseSocket(result);
throw new FtpException(string.Format(
"Could not connect to {0} on port {1}.", hostName, port),
exc);
}
return result;
}
//--------------------------------------------------------------------------
---------
private void CloseSocket(Socket socket)
{
Trace.WriteLine("CloseSocket");
if(socket != null && socket.Connected)
{
socket.Close();
socket = null;
}
}
//--------------------------------------------------------------------------
---------
public void Send(string hostName, int port, Stream stream)
{
if(hostName == null)
throw new ArgumentNullException("hostName");
if(stream == null)
throw new ArgumentNullException("stream");
Trace.WriteLine("Send");
totalBytes = 0;
byte[] buffer = new byte[DataBufferSize];
Socket socket = OpenSocket(hostName, port);
int bytes = stream.Read(buffer, 0, buffer.Length);
while(bytes > 0)
{
socket.Send(buffer, bytes, SocketFlags.None);
totalBytes += bytes;
bytes = stream.Read(buffer, 0, buffer.Length);
}
CloseSocket(socket);
}
//--------------------------------------------------------------------------
---------
private delegate void SendCallback(string hostName, int port, Stream
stream);
private SendCallback send;
//--------------------------------------------------------------------------
---------
public IAsyncResult BeginSend(string hostName, int port, Stream
stream,
AsyncCallback callback, object state)
{
if(hostName == null)
throw new ArgumentNullException("hostName");
if(stream == null)
throw new ArgumentNullException("stream");
Trace.WriteLine("BeginSend");
if(send == null)
send = new SendCallback(this.Send);
return send.BeginInvoke(hostName, port, stream, callback, state);
}
//--------------------------------------------------------------------------
---------
public void EndSend(IAsyncResult asyncResult)
{
if(asyncResult == null)
throw new ArgumentNullException("asyncResult");
Trace.WriteLine("EndSend");
if(!asyncResult.IsCompleted)
asyncResult.AsyncWaitHandle.WaitOne();
send.EndInvoke(asyncResult);
}
#endregion
}
}