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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:01:28+00:00 2026-06-13T13:01:28+00:00

I’m having some strange things happen with my program, and I’m not sure what

  • 0

I’m having some strange things happen with my program, and I’m not sure what I should be doing. This is a pseudocode version of what my code looks like so far:

Server:

//Set up Server sockets

int maximum;

// Collect the maximum
cout << "\nEnter a Maximum:";
cin >> maximum;
cout << "\n";

int *array = new int[maximum + 1];

memset(array, 0, sizeof(array));

while(array[0] < anInt){

    //receive the array from the client
    if(recv(newsockfd, array, maximum, 0) < 0){
        perror("ERROR receiving from socket");
    }

    mathFunction(array);  //A function that alters the contents of array
    array[0]++;

    //If array[0] isn't too big
    if(array[0] < anInt){
        // Send the array to the client
        if(send(newsockfd, array, maximum, 0) < 0){
            perror("ERROR sending to socket");
        }
    }
}

Client:

//Set up Client sockets

//The maximum was already sent over earlier
int *array = new int[maximum + 1];

while(array[0] < anInt){

    //receive the array from the server
    if(recv(sockfd, array, maximum, 0) < 0){
        perror("ERROR receiving from socket");
    }

    mathFunction(array);  //A function that alters the contents of array
    array[0]++;

    if(send(sockfd, array, maximum, 0) < 0){
        perror("ERROR sending to socket");
    }
}

My problem is that I keep getting a “Connection reset by peer” error, which leads to a Segmentation Fault, crashing my program. Also, when playing around with the 3rd argument of the send/recv functions (currently set as maximum), my program acts differently. It will actually work perfectly if the user enters a maximum of 100, but anything more than that screws it up.

I know this is a long shot, but can anyone see something that I’m doing wrong?

  • 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-06-13T13:01:29+00:00Added an answer on June 13, 2026 at 1:01 pm

    First of all code that posted by you has a logical error:

    Server first receive data from the client, do something with it and then send its result back to the client.

    In the other side client also receive data from server, do something with it and then send it back to the server.

    And that’s obviously a race condition, no one send data to other side to begin communication.

    Beside that logical error you have some C++ errors:

    1) memset(array, 0, sizeof(array)) only 0 initialize sizeof(int*) bytes from your array not entire array, since sizeof(array) is always sizeof(int*) if you want to 0 initialize entire array (and I think you want it) you should call:

    memset(array, 0, (maximum + 1) * sizeof(int));
    

    or even better:

    std::fill( array, array + maximum + 1, 0 );
    

    And in C++ it is much better to use classes like std::vector instead of raw pointers:

    std::vector<int> array( maximum + 1 ); // automatically initialize to 0
    

    2) Your array type is int* and send/recv count its input by byte, so if you want to send/recv entire array you must have something like:

    send(sockfd, (char*)array, maximum * sizeof(int), 0);
    

    3) You should check return value of send/recv, specially recv since it may recv less data in each call, for example you send 8K data and recv only receive first 1K and rest of it remain in the network buffer, so you should call it repeatedly until you read your buffer completely.

    • 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 have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

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.