各位大大,小弟采用C# 做Asynchronous socket编程,程序如下:public static void StartClient(string servername)
{
try
{
IPHostEntry ipHostInfo = Dns.Resolve(servername);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); client.BeginConnect( remoteEP,new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne(); Send(client,"Username "+"Username");
sendDone.WaitOne(); Send(client,"Passname "+"root");
sendDone.WaitOne();
Receive(client);
receiveDone.WaitOne(); // Write the response to the console.
Console.WriteLine("Response received : {0}", response); // Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine("无法连接邮件服务器 "+e.ToString());
}
}
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client; // Begin receiving the data from the remote device.
client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
} private static void ReceiveCallback( IAsyncResult ar )
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket; // Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
Console.WriteLine("kkk"+Encoding.ASCII.GetString(state.buffer,0,bytesRead));
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback), state);
}
else
{ Console.WriteLine("测试"+bytesRead.ToString());
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine("接受数据错误 "+e.ToString());
}
}
这之前定义了
private static ManualResetEvent connectDone =new ManualResetEvent(false);
private static ManualResetEvent sendDone =new ManualResetEvent(false);
private static ManualResetEvent receiveDone =new ManualResetEvent(false);我的问题是程序死在receiveDone.waitone()这条上面
我的程序是从微软的msdn上弄过来的,不知道怎么死在这句上面
{
try
{
IPHostEntry ipHostInfo = Dns.Resolve(servername);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); client.BeginConnect( remoteEP,new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne(); Send(client,"Username "+"Username");
sendDone.WaitOne(); Send(client,"Passname "+"root");
sendDone.WaitOne();
Receive(client);
receiveDone.WaitOne(); // Write the response to the console.
Console.WriteLine("Response received : {0}", response); // Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine("无法连接邮件服务器 "+e.ToString());
}
}
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client; // Begin receiving the data from the remote device.
client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
} private static void ReceiveCallback( IAsyncResult ar )
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket; // Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
Console.WriteLine("kkk"+Encoding.ASCII.GetString(state.buffer,0,bytesRead));
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback), state);
}
else
{ Console.WriteLine("测试"+bytesRead.ToString());
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine("接受数据错误 "+e.ToString());
}
}
这之前定义了
private static ManualResetEvent connectDone =new ManualResetEvent(false);
private static ManualResetEvent sendDone =new ManualResetEvent(false);
private static ManualResetEvent receiveDone =new ManualResetEvent(false);我的问题是程序死在receiveDone.waitone()这条上面
我的程序是从微软的msdn上弄过来的,不知道怎么死在这句上面
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货