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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:26:55+00:00 2026-05-26T02:26:55+00:00

My goal is create an app client server, written in C++. When the server

  • 0

My goal is create an app client server, written in C++.
When the server read an input from the client, should process the string and give an output.
Basically, I have a simply echo server that send the same message.
But if the user types a special string (like “quit”), the program have to do something else.
My problem is that this one dont happend, because the comparison between strings is not working… I dunno why!
Here a simple code:

  while(1) {
            int num = recv(client,buffer,BUFSIZE,0);
            if (num < 1) break;
            send(client, ">> ", 3, 0);
            send(client, buffer, num, 0);

            char hello[6] ="hello";
            if(strcmp(hello,buffer)==0) {
                send(client, "hello dude! ", 12, 0);
            }

            buffer[num] = '\0';
            if (buffer[num-1] == '\n')
                buffer[num-1] = '\0';
            std::cout << buffer;
            strcpy(buffer, "");
        }

Why the comparison is not working?
I have tried many solutions…but all failed 🙁

  • 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-26T02:26:55+00:00Added an answer on May 26, 2026 at 2:26 am

    Your data in buf may not be NULL-terminated, because buf contains random data if not initialized. You only know the content of the first num bytes. Therefore you also have to check how much data you’ve received before comparing the strings:

    const char hello[6] ="hello";
    size_t hello_sz = sizeof hello - 1;
    if(num == hello_sz && memcmp(hello, buffer, hello_sz) == 0) { ...
    

    As a side note, this protocol will be fragile unless you delimit your messages, so in the event of fragmented reads (receive “hel” on first read, “lo” on the second) you can tell where one message starts and another one ends.

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

Sidebar

Related Questions

The goal: To create a .NET dll i can reference from inside SQL Server
My goal is to create a method with the following requirements: Output should be
My goal is to create a log app, apart from my main app, that
I have a server and a client app, my server listens on port 10015
My goal is to create a version of the app that will expire after
I'm quite new to sencha touch. The goal is to create an app which
Goal: I'd like to allow users to create questions and collect information from other
I am trying to create a simple test app that basically extends the Android
Goal I want to create a web app with a horizontal GUI bar that
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do

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.