监听public void StartListening()
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket.
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(100); while (list)
{
// Set the event to nonsignaled state.
allDone.Reset(); // Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); // Wait until a connection is made before continuing.
allDone.WaitOne();
} }
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
异步接收public void ReadCallback(IAsyncResult ar)
{
String content = String.Empty; // Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket; // Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
string s = Encoding.Unicode.GetString(state.buffer, 0, bytesRead);
txtContext.Invoke(new CBIntoTextBox(IntoText), new object[] { s }); handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
}我已经停掉了监听的循环,并且执行了listener.Shutdown, listener.Close, 但是异步接收线程仍然在工作,客户端仍然能够正常发送信息。这部份应该怎样才能关闭掉昵?谢谢。
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket.
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(100); while (list)
{
// Set the event to nonsignaled state.
allDone.Reset(); // Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); // Wait until a connection is made before continuing.
allDone.WaitOne();
} }
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
异步接收public void ReadCallback(IAsyncResult ar)
{
String content = String.Empty; // Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket; // Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
string s = Encoding.Unicode.GetString(state.buffer, 0, bytesRead);
txtContext.Invoke(new CBIntoTextBox(IntoText), new object[] { s }); handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
}我已经停掉了监听的循环,并且执行了listener.Shutdown, listener.Close, 但是异步接收线程仍然在工作,客户端仍然能够正常发送信息。这部份应该怎样才能关闭掉昵?谢谢。
socket.shutdowm()
socket.close
你在哪创建的就在哪找,socket对象关闭,异步操作同时也会终止
每当客户端与服务端建立连接时,服务端会NEW一个新的SOCKET。public void AcceptCallback(IAsyncResult ar)
{
try
{
// Signal the main thread to continue.
allDone.Set(); // Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar); // Create the state object.
StateObject state = new StateObject(); // 会NEW 一个新的, state.workSocket = handler; // 我应该怎样关闭他呢?
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
catch
{
} }
hashtable.add(这个随你喜欢放什么比如ID啊之类的标记,socket)
当然用泛型的最好~
然后根据标记找到想对应的SOCKET然后CLOSE
{ String content = String.Empty; // Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket; // Read data from the client socket.
int bytesRead = handler.EndReceive(ar); // 这一句报错了,无法访问已释放的对象。。
如果是某个连接想停止接收数据
“异步接收”的那个方法里面就不要再调用handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
并把handler关闭掉。你监听连接时的AcceptCallback里面应该调用handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
{
try
{
// Signal the main thread to continue.
allDone.Set(); // Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar); // Create the state object.
StateObject state = new StateObject(); // 会NEW 一个新的, state.workSocket = handler; // 我应该怎样关闭他呢?
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
catch
{
} }那你把异步接收的代码改成这样:public void ReadCallback(IAsyncResult ar)
{
String content = String.Empty; // Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket; // Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
string s = Encoding.Unicode.GetString(state.buffer, 0, bytesRead);
txtContext.Invoke(new CBIntoTextBox(IntoText), new object[] { s }); //handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);//这里不要了,
}
handler.shutdowm();
handler.close();
}