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 6021681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:44:55+00:00 2026-05-23T03:44:55+00:00

I have a TCP socket application where I have to read several types of

  • 0

I have a TCP socket application where I have to read several types of replies being the max size of a buffer 8192 some replies are splitted in more packets.

Currently I receive a list of members at reply 44, so the first idea I had to be able to deal with splitted packets was to define a stream out side of it to stored the incoming data until it is complete with a bool and current size variable.

Once it hits the reply 44 it will check if extraList is true or false, if false it means it is an initial request to incoming members list.

If the 4 initial bytes of the packet is bigger then bytes.Legth which is 8192 it will trigger the extraList to true and fill the initial data to the buffer I had previously set with the with the total packet size as it is size.

Since extraList has been trigged and turned into true, the packet reading will fall into it until the data is complete, which will then set it back to false and trigger the MemberList function with the complete list.

Would like some advices, suggestions, etc to improve this code.

int storedCurSize = 0;
MemoryStream stored = null;
bool extraList = false;

while (roomSocket.Connected)
{
    byte[] bytes = new byte[roomSocket.ReceiveBufferSize];
    roomSocket.Receive(bytes);

    MemoryStream bufferReceived = new MemoryStream(bytes, 0, bytes.Length);
    using (var reader = new BinaryReader(bufferReceived))
    {
        int packetSize = (int)reader.ReadInt32() + 9;
        int reply = (int)reader.ReadByte();
        if (reply == 44 || extraList)
        {
            if (!extraList && packetSize <= bytes.Length)
            {
                MemberList(bytes);
            }
            else
            {
                if (!extraList)
                {
                    stored = new MemoryStream(new byte[packetSize], 0, packetSize);
                    stored.Write(bytes, 0, bytes.Length);
                    storedCurSize = bytes.Length;
                    extraList = true;
                }
                else
                {
                    if (storedCurSize < stored.Length)
                    {
                        int storedLeftSize = (int)stored.Length - storedCurSize;
                        stored.Write(bytes, 0, (storedLeftSzie < bytes.Length) ? storedLeftSize : bytes.Length);
                        storedCurSize += (storedLeftSize < bytes.Length) ? storedLeftSize : bytes.Length;
                        if (storedCurSize >= stored.Length)
                        {
                            extraList = false;
                            MemberList(stored.ToArray());
                            stored.Close();
                        }
                    }
                }
            }
        }
    }
}
  • 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-23T03:44:56+00:00Added an answer on May 23, 2026 at 3:44 am

    While reading code briefly what is flaring is magic numbers (9, 44) and very deep nesting.
    Replace numbers with good-named constants and move out some parts of code as methods.

    In case they are tightly twisted by used local variables – probably all this method worth moving out to worker class with single responsibility – to read the message. Thus local variables becomes class fields and methods wouldn’t be so inflexible to refactoring.

    Also MemberList(…) is poor name for a method as for me. Make it a verb that will describe what method is doing.

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

Sidebar

Related Questions

When using a blocking TCP socket, I don't have to specify a buffer size.
I have been developing my first TCP/Socket based application with Apache Mina, it looks
I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out
I am working with socket application. I have a JAVA TCP listener as a
I have an java application using Socket TCP/IP and GUI. Server always listens connection
I have a simple TCP socket client and server application. They are communicating using
I have a Python application which opens a simple TCP socket to communicate with
I have a server-client application [TCP sockets, .NET 4.0].. the application about : do
I have a tcp socket sending three lines like this out2.println(message1\n); out2.println(message2\n); out2.println(message3\n); and
I have problem with receiving an image over TCP socket [.net 4.0] Server: Socket

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.