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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:07:11+00:00 2026-05-30T04:07:11+00:00

I have server and client to send/receive a string through windows sockets. Client code

  • 0

I have server and client to send/receive a string through windows sockets.

Client code (sender):

string c = "Haha";

sprintf(temp.Buffer, c.c_str());
temp.len = c.size();
int sent = 0;
if ((sent = sendto(s, temp.Buffer, temp.len + 1, 0, (SOCKADDR*)&sa_in, sizeof(sa_in))) == SOCKET_ERROR) {
    cout << "Send request method failed\n";
}

Server code (receiver):

if (!(outfds = select (1 , &readfds, NULL, NULL, NULL))) {//timed out
    cout << "timed out";
}
if (outfds == 1)
{
    fromlen = sizeof sa_in;
    received = recvfrom(s, buf, 2048, 0, (struct sockaddr*)&sa_in, &fromlen);
}

In the server, I’ve noticed if the string I send from the client is smaller than a number of bytes (like 0-8~) the ‘buf’ variable contains garbage at the end of it. For example, I’ll send the string “ahah” and the server will receive a string similar to “ahah0`”. If I send “Hello World”, it receives it correctly. The char buffers in the client and server are both of size 2048.

A solution I found to this was to add +1 to the length in the sendto() method. I don’t want this to bite me in the a** later if I send something bigger than the maximum allowed size.

Does anyone know where this is coming from and do you have a better solution?

Edit: The number of bytes sent and received is always the same.

Thanks

  • 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-30T04:07:12+00:00Added an answer on May 30, 2026 at 4:07 am

    You are not transmitting the trailing '\0' char. Does your protocol specify a nul-terminated string? If so, change your client thus:

    sendto( ..., temp.len+1, ...);
    

    If your protocol specifies that you do not send the nul termination, then modify your server thus:

    fromlen = sizeof sa_in;
    received = recvfrom(s, buf, 2048, 0, (struct sockaddr*)&sa_in, &fromlen);
    if(received >= 0)
        buf[received] = 0;
    

    If your protocol does not specify, then stop coding now and finish writing your protocol spec.

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

Sidebar

Related Questions

I have a client-server program. I'm sending data like this: private void Sender(string s,TcpClient
We have a client application that needs to send messages to a server for
We have a Server/Client cybercafe management application which used to work fine on Windows
I'm trying to implement basic communication through sockets, what I have now is: server
I want to have a message send & receive through 2 uni-direction FIFO Flow
I have a client-server Silverlight application, which is use Socets. I have server appliaction
i have a server - client application that runs on java 1.3; i want
I have client/server applications and a very simple protocol for communication. More precisely, it's
We have client server based app which saves user related data into a zip
I have Client-Server environment and developed a project for Client-Server. I need to share

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.