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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:05:37+00:00 2026-05-30T07:05:37+00:00

so I’m getting socket communication, AND I’m getting the proper message…however when I try

  • 0

so I’m getting socket communication, AND I’m getting the proper message…however when I try to extract it from the receive buffer…it doesn’t work. My code looks something like this:

void CClientSocket::OnReceive(int nErrorCode) 
{
    char buff[1000];
    int ibuf = Receive(buff,sizeof(buff));
    buff[sizeof(buff)-1] ='\0';
    CSocket::OnReceive(nErrorCode);
}

When I run in debug, the value of buff contains the proper message…along with a bunch of random characters filling up the unused portion of the array. However, I can’t use that data…if I try to convert to CString or even to transfer the characters to a different array…nothing works. I’m baffled and I really don’t know what to do.

Let me know if you need more info or if I am unclear in any way.

EDIT: some code I have tried to extract the data

CString string;
string = CString(buff);

AND

char *msg;
msg = new char[ibuf]
for(int i = 0; i < ibuf; i++){
    msg[i] = (char)buff[i];
}

EDIT: some clarification
when i said ‘nothing works’ I meant that when I tried to convert it, the result was and empty CString or empty char etc…there are no errors.

  • 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-30T07:05:39+00:00Added an answer on May 30, 2026 at 7:05 am
    int ibuf = Receive(buff,sizeof(buff));
    buff[sizeof(buff)-1] ='\0';
    

    Rather than that second line, put the zero byte at buff[ibuf]. But before that, add an error check (make sure ibuf > 0 and if not, treat it as a connection drop.) Also, if you do it this way you should Receive(buff, sizeof(buff)-1); (note the -1 – this accounts for the terminating character – thanks @Altnitak for pointing this out.)

    On the other hand… I would also argue that this is no way to treat socket code. Strings are not sent atomically over the network. They will be split. If you control the protocol, I suggest you depart the string-centered view of the universe, and structure your network communications so that messages begin with an integer (uint16_t or uint32_t, and make sure to call htons/htonl etc.) indicating the length of the payload that follows. Then keep buffering incoming bytes and process them when you have enough to satisfy the lengths specified.

    If you do not control the protocol, find a convenient marker that you can use to decide when to process content, and buffer bytes until you reach it. (For example if you’re doing HTTP, you might buffer chars until you see a newline while processing headers, then decide on a certain buffer size for the contents of the response…)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.