socket 异步发送信息出错!大家进来帮我顶顶!
AoooG 2007-09-28 10:31:31 看看以下的代码,发送不出去
IPAddress ipAddress = IPAddress.Parse(HostName);
IPEndPoint lep = new IPEndPoint(ipAddress, HostPort);
Socket s = new Socket(lep.Address.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
try
{
//while (true)
//{
//allDone.Reset();
byte[] buff = Encoding.ASCII.GetBytes(datagram);
s.BeginSendTo(buff, 0, buff.Length, SocketFlags.None, lep, new AsyncCallback(SendTo_Callback), s);
//allDone.WaitOne();
//}
}
protected virtual void SendTo_Callback(IAsyncResult ar)
{
StateObject so = (StateObject)ar.AsyncState;
Socket s = so.workSocket;
int send = s.EndSendTo(ar);
s.Close();
}