private void CloseConnection(ConnectionInfo ci) { ci.Socket.Close(); lock (connections) connections.Remove(ci); }
private void AcceptCallback(IAsyncResult result) { ConnectionInfo connection = new ConnectionInfo(); log.printlog("[listen] start accept callback"); try { // Завершение операции Accept Socket s = (Socket)result.AsyncState; connection.Socket = s.EndAccept(result); connection.Msg = new Diloma.DataType.NetworkMessage(); connection.Buffer = new byte[100000]; log.printlog("[getdata] New data is available. buffer size = " + connection.Buffer.Length.ToString()+ " Call receive procedure"); lock (connections) connections.Add(connection); // Начало операции Receive и новой операции Accept connection.Socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), connection); s.BeginAccept(new AsyncCallback( AcceptCallback), result.AsyncState); } catch (SocketException exc) { CloseConnection(connection); MessageBox.Show("Socket exception: " + exc.SocketErrorCode, "Ошибка асинхронного вызова"); log.printlog("[ERROR][AcceptCallback]" + exc.Message); } catch (Exception exc) { CloseConnection(connection); MessageBox.Show("Exception: " + exc); log.printlog("[ERROR][AcceptCallback]" + exc.Message); } }