Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:36:30+00:00 2026-05-26T12:36:30+00:00

I currently have a Windows Application that uses TCP Sockets to connect users and

  • 0

I currently have a Windows Application that uses TCP Sockets to connect users and send/receive data. The application crashes when a new user tries to connect while several other users (never the same amount of users) are already connected and receiving data. My application needs to receive data from one person and send it out to many users. The application receives data couple times a second.

The last thing my code does before error error is trying to add a new client to the list. It seems like when trying to connect a new user it is interfering with the data attempting to be sent.

private static void EndAccept(IAsyncResult ar)
{
    Listener_Socket = (Socket)ar.AsyncState;
    ClientsList.Add(Listener_Socket.EndAccept(ar));
    Listener_Socket.BeginAccept(new AsyncCallback(EndAccept), Listener_Socket);
    ...
    AsyncCallback receiveData = new AsyncCallback(MyServer.OnReceivedData);
}

Event Viewer Error:

Application: xxxxxxxxxxxx.exe Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException Stack: at
System.Collections.ArrayList+ArrayListEnumeratorSimple.MoveNext() at
MyServer.OnRecievedData(System.IAsyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr) at
System.Threading.ExecutionContext.runTryCode(System.Object) at
System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode,
CleanupCode, System.Object) at
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,
System.Threading.ContextCallback, System.Object, Boolean) at
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,
System.Threading.ContextCallback, System.Object) at
System.Net.ContextAwareResult.Complete(IntPtr) at
System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32,
UInt32, System.Threading.NativeOverlapped*) at
System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32,
UInt32, System.Threading.NativeOverlapped*)

OnRecievedData:

private static void OnRecievedData(IAsyncResult ar)
    {
        SocketClient client = (SocketClient)ar.AsyncState;
        byte[] aryRet = client.GetRecievedData(ar);

        if (aryRet.Length < 1)
        {
            client.ReadOnlySocket.Close();
            ClientsList.Remove(client);
            return;
        }
        foreach (SocketClient clientSend in ClientsList)
        {
            if (client != clientSend)
                try
                {
                    clientSend.ReadOnlySocket.NoDelay = true;
                    clientSend.ReadOnlySocket.Send(aryRet);
                }
                catch
                {
                    clientSend.ReadOnlySocket.Close();
                    ClientsList.Remove(client);
                    return;
                }
        }
        client.SetupRecieveCallback();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:36:31+00:00Added an answer on May 26, 2026 at 12:36 pm

    When a client connects, EndAccept will usually be invoked on another thread than the thread OnRecievedData is running on. If EndAccept happens in the middle of the foreach, the enumerator is no longer valid (the collection has changed).

    You should use some kind of synchronization mechanism like lock to prevent this from happening.

    And I even missed what Suhas mentions; the collection is modified from within the same thread, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a distributed windows application (written in C#) that currently uses MSDE. The
I have a Windows GUI application that's using the Qt framework (currently version 3.3.5,
I currently have a c++ application that gets built on xp and windows vista/7
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView
I have a Windows Mobile application written in Java that uses AWT for the
I have a Sql Server that uses Windows Authentication. I want to connect to
I have a Windows Workflow application that uses classes I've written for COM automation.
I'm building an application that connects to SQL Server 2005. It currently uses Windows
I have a windows form application that uses a Shared class to house all
We have an application that uses Lucene.NET within a windows service to reindex our

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.