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

  • Home
  • SEARCH
  • 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 6322695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:22:52+00:00 2026-05-24T16:22:52+00:00

I am totally confused right now. Edit: Okay, nevermind. The Python socket as well

  • 0

I am totally confused right now.

Edit: Okay, nevermind. The Python socket as well is starting to do it now.

Edit 2: Well, not quite sure if this is causing high CPU usage, but something randomly is. Is there an efficient way to figure out what is causing spikes in the usage? This project is a bit large and has various threads.

I have an asynchronous server that listens and waits for incoming connections, then keeps them alive and waits for the socket to flush and give the server data. It is only closed when the user wants the socket to be closed.

However, whenever I let a socket & stream stay connected, it starts to go haywire and starts sending empty data on an endless loop… it may take anywhere from 15 seconds to over a minute before it starts going wack. If I let it go for a really long time, it starts to cause really high CPU usage.

Aside from the high CPU usage, oddly enough, everything works as it should; messages are sent & received fine.

This is my read callback function:

    protected void ReadCallback(IAsyncResult ar)
    {
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.SocketHandle;

        try
        {
            int bytesRead = (state.BytesRead += handler.EndReceive(ar)), offset = 0;
            string line = m_Encoder.GetString(state.Buffer, 0, bytesRead);
            if ( state.Buddy != null )
                Console.WriteLine(state.Buddy.Address);
            if (bytesRead > 0)
            {
                Console.WriteLine("!!!");
                /* A complete request? */
                if (line.EndsWith("\n") || line.EndsWith("\x00"))
                {
                    string[] lines = line.Split('\n'); // ... *facepalm*
                    foreach (string ln in lines)
                        this.MessageReceieved(ln, state);

                    state.Buffer = new byte[StateObject.BUFFER_SIZE];
                    state.BytesRead = 0; // reset
                }
                /* Incomplete; resize the array to accommodate more data... */
                else
                {
                    offset = bytesRead;
                    Array.Resize<byte>(ref state.Buffer, bytesRead + StateObject.BUFFER_SIZE);
                }
            }
            if (handler != null && handler.Connected )
                handler.BeginReceive(state.Buffer, offset, state.Buffer.Length - offset, SocketFlags.None, new AsyncCallback(ReadCallback), state);

        }
        catch (SocketException)
        {
            if (state.Buddy != null)
                state.Buddy.Kill();
            else
                handler.Close();
        }
    }

I know this is somehow caused by calling BeginReceive, but I don’t know how else to keep the connection alive.

  • 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-24T16:22:53+00:00Added an answer on May 24, 2026 at 4:22 pm

    There is nothing in that code that can make it go haywire.

    I do see some problems though.

    Connection detection

    No need to check if the socket is connected. You can detect disconnections in two ways in the receive callback:

    1. Zero bytes is returned by EndReceive
    2. An exception is being thrown.

    I would recommend that the first thing you do after EndReceive is to check the return value and handle disconnect accordingly. It makes the code clearer.

    Your code will currently do nothing if 0 bytes are received. The handler will just stop receiving and still think that the connection is open.

    Buffer handling

    Your buffer handling is very inneffecient. Do not resize the buffer every time. It will slow your server down a lot. Allocate a large buffer from start.

    String handling

    Don’t build a string every time you receive something. Check inside the byte buffer after new line or null instead. THEN build a string, and only make it as large as needed. You might receive more bytes that just a message (for instance one and a half message)

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

Sidebar

Related Questions

I'm totally confused as of why this is not working? I'm iterating through a
Totally confused by this one... We have a WAMPServer installation set up, running a
I'm totally confused with this small out of scope thing. So here is some
I know others have asked this question, but I'm totally confused by this: This
Totally confused about the data types required here. I have this Linq statement: var
I'm totally confused and have no idea how to do this, so please forgive
This could be something very simple but I'm totally confused. I have a JScrollPane
I am a totally blind programmer who would like to learn Python. Unfortunately the
Are there any good books for a relatively new but not totally new *nix
For some reason the script below is not working. This is the code I

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.