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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:12:02+00:00 2026-06-17T15:12:02+00:00

Im learning about Winsock and Im having a strange issue when sending and receiving

  • 0

Im learning about Winsock and Im having a strange issue when sending and receiving a simple string. Here’s my code (pure C):

Client:



//...
//Declarations and stuff

//----------- SEND SOME DATA -------------------------------------------------

    char string1[] = "string-1";
    int bytes_sent = 0;

    bytes_sent = send(client_socket, string1, strlen(string1), 0);  

    printf("BYTES SENT: %i\n", bytes_sent);
    printf("\n-----------------------------------------------\n\n");

    system("pause");

//...

Server:



//...
//Declarations and stuff

//----------- START LISTENING FOR REQUESTS ------------------------------------

    SOCKET ClientSocket;

    #define BUFFER_SIZE 256

    int size;
    struct sockaddr_in client_info;
    char client_ip[16];
    char data_received[BUFFER_SIZE];    
    int bytes_received = 0; 

    listen(ListenSocket, SOMAXCONN);

    while(1){           

        ClientSocket = accept(ListenSocket, (struct sockaddr *)&client_info, &size);        
        strcpy(client_ip, inet_ntoa(client_info.sin_addr));     

        do{

            bytes_received = recv(ClientSocket, data_received, BUFFER_SIZE, 0);

            if(bytes_received > 0){
                printf("DATA RECEIVED FROM %s: %s (%i bytes)\n", client_ip, data_received, bytes_received);
            }


        }while(bytes_received > 0);

        printf("\n-----------------------------------------------\n\n");


    }

//...

The problem is that the server prints my string + some strange symbols (see the pic).

Strange symbols

Im using a stream socket. The example is very simple so I dont know what could be wrong. The problem disappears (server prints OK the string) if I randomly modify the string, or the server’s buffer size, or both. The problem fixes if in the send() call i use sizeof() instead of strlen(). Im a little bit lost here. Please be kind if i have missed something, this is my very first post here. I can provide the whole code (its basically the winsock start and the socket definition) .

  • 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-17T15:12:04+00:00Added an answer on June 17, 2026 at 3:12 pm

    The data you send does not contain a terminating null character:

    bytes_sent = send(client_socket, string1, strlen(string1), 0);
    

    …because strlen does not count the terminating null. This isn’t exactly the problem in itself, but rather is coupled with the fact that on the receiving side:

    char data_received[BUFFER_SIZE];
    // ...
    bytes_received = recv(ClientSocket, data_received, BUFFER_SIZE, 0);
    

    data_received is not initialized, and you can receive up to BUFFER_SIZE bytes. This means that since the data you send is not null-terminated:

    • If bytes_received < BUFFER_SIZE, the rest of data_received may not be initialized so accessing/printing would be undefined behavior. It’s actually not 100% clear, as the documentation says:

      […] calling recv will return as much data as is currently available—up to the size of the buffer specified […]

      …so it may mean that the rest of the buffer is left untouched.

    • If bytes_received == BUFFER_SIZE, there is no null-terminator, so printf will invoke undefined behavior by trying to print it, since it doesn’t know where the string stops and will overrun the array.

    The easiest ways to fix these are either to send a null terminator:

    bytes_sent = send(client_socket, string1, strlen(string1)+1, 0); // +1 here
    bytes_sent = send(client_socket, string1, sizeof(string1), 0);   // same as above
    

    …or receive a byte less and put the null terminator on the receiving size:

    bytes_received = recv(ClientSocket, data_received, BUFFER_SIZE-1, 0); // -1 here
    data_received[bytes_received] = 0;
    

    I’d personally go with the first.

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

Sidebar

Related Questions

so here's my situation. I'm working with winsock, learning some things about networking and
I was learning about Winsock and was looking at code from the page :
I started learning about trees and tried writing the code for BST. Unfortunately i
learning about loops (still a beginner) in VB.net. I have got the below code
While learning about python, I came upon this code, which takes a text file,
I'm just learning about JPA and session beans. I've worked through a simple database
I'm learning about deadlocks in Java, and there's this sample code from Sun's official
As I'm learning about networking and io in Java, I'm slowly building client/server apps
I am learning about LINQ-to-SQL and everything was going well until something strange happened:
I'm interested in learning about the available choices of high-quality, stand-alone source code formatters

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.