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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:05:10+00:00 2026-05-21T02:05:10+00:00

I am currently writing two projects that have to communicate with each other over

  • 0

I am currently writing two projects that have to communicate with each other over TCP, with the client having to be implemented using System::Net::Sockets, and the server using c++ winsock. For a long time, I have been using this lovely function to receive text in winsock, where the client sends the message length first and then the message. I don’t want to have to change anything on the server side with this function, but with the .NET I will do anything. This is my attempt.

bool WinSockObject::receiveText(std::string &out)
{
    //Create a temporary buffer to work with
    char buf[4096];
    //Stores textLength of incoming data
    long textLength = 0;
    //Receive the textLength that was sent first
    _result = ::recv(
        _socket, //socket we are receiving on
        reinterpret_cast< char *>(&textLength), //the length of the incoming text
        sizeof(textLength), //number of bytes
        0 //no additional flags necessary
    );
    //Make sure we got the text length
    if (_result == SOCKET_ERROR)
    {
        set_error("Unable to receive text length.");
        return false;
    }
    //Convert the textLength back to host bytes
    textLength = ::ntohl(textLength);
    //Receive the actual message
    _result = ::recv(
        _socket, //socket we are receiving on
        buf, //the input buffer
        textLength, //text length
        0 //no additional flags are necessary
    );
    //Make sure we got the actual message
    if (_result == SOCKET_ERROR)
    {
        set_error("Unable to receive text.");
        return false;
    }
    //Manually terminate the buffer
    buf[textLength] = '\0';
    //Copy the buffer to the output string
    out.append(buf);
    return true;
}

But I am stuck sending the length first then the message

Socket^ s = gcnew Socket(
    AddressFamily::InterNetwork,
    SocketType::Stream,
    ProtocolType::Tcp
);
s->Connect(server, port); //message to send
String^ howdy = "howdy";
int strLength = howdy->Length; //length of message
String^ length = strLength.ToString(); //convert it to String
//get the bytes
array<Byte>^msgLength = Encoding::ASCII->GetBytes(length);
//send the message length
int bytes = s->Send(msgLength);
//send the actual message
array<Byte>^msg = Encoding::ASCII->GetBytes(howdy);
bytes = s->Send(msg);
  • 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-21T02:05:11+00:00Added an answer on May 21, 2026 at 2:05 am

    There are two problems that I see after a brief reading:

    1. You’re sending the string length as a string, but the server is reading it as binary.
    2. The server is assuming that recv will always read the number of bytes requested. This is wrong; recv may return 0 (if the other side has closed the connection gracefully), any value in the range [1, len] (if some data was received successfully), or SOCKET_ERROR (if there was an error).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on two social networking sites that have a lot in common,
Im currently writing my bachelor thesis with latex and using TexnicCenter. I want to
I am currently writing a simple, timer-based mini app in C# that performs an
I am currently writing unit tests for a ViewModel in my project that uses
I'm currently writing a game that uses 2D OpenGL output under sdl, and I'm
I'm currently writing unit tests for my backbone.js application, and I'm having a few
I've been using bzr for personal programming projects across my two computers for some
I'm currently writing a unit test project using the version 4.6.1 (Windows Vista +
I am currently writing a .Net application in c#, which has two main components:
My program that I am writing's purpose arose with this issue: There are two

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.