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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:54:49+00:00 2026-05-25T13:54:49+00:00

im making a simple TCP client-server in c and im trying to send a

  • 0

im making a simple TCP client-server in c and im trying to send a message from the client to the server, but im having some problems with it.
The server does send the message (integer value > 0) but the client is unable to receive it (integer value > 0)
here is the code:
Client

#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>




int main()
{
    int s_id;
    char *msg = "hello";
    struct sockaddr_in serv_addr;
    s_id = socket (AF_INET, SOCK_STREAM, 0);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons (1156);
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    connect(s_id,(struct sockaddr *) &serv_addr, sizeof (struct sockaddr));
    int r = recv (s_id, (char *) msg, 9, 0);
    printf("%d \n", r );
    printf("%s \n", msg );

    return 0;
}

Server:

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

int main()
{
       int s_id;
       char *msg = "connected";
       struct sockaddr_in my_addr, remote_addr;
       s_id = socket (PF_INET,SOCK_STREAM,0);
       my_addr.sin_family = AF_INET;
       my_addr.sin_port = htons(1156);
       my_addr.sin_addr.s_addr  = inet_addr("127.0.0.1");

       bind(s_id,(struct sockaddr *) &my_addr, sizeof(struct sockaddr));
       listen (s_id,5);
       int size = sizeof (struct sockaddr_in);
       int new_sd = accept (s_id, (struct sockaddr *) &remote_addr, &size);
       int s= send(new_sd, (void *)msg, 9, 0);
       printf("%d \n", s );
       return 0;
}

The outputs i get (after first starting the server, and then the client) are
server side: 9
client-side: -1
hello

I am using Ubuntu 11.04 and the gcc compiler.

I hope someone out there can help.
Thank you

Umar

  • 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-25T13:54:49+00:00Added an answer on May 25, 2026 at 1:54 pm
    char *msg = "hello";
    

    This is a string literal. It’s a constant, and you can’t change it.

    int r = recv (s_id, (char *) msg, 9, 0);
    

    And there you’re trying to write to it.

    Change your declaration to:

    char msg[20];
    memset(msg, 0, sizeof(msg));
    

    If you make that change, your code works as expected.

    In C you’re going to have to allocate and manage buffers – there’s no free lunch 🙂

    Also take note of the other answer from Nikolai N Fetissov – you really should be checking return codes from all the system calls.

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

Sidebar

Related Questions

I'm making simple XMPP client using Jabber-Net. To send simple message i'm using a
I am making simple one on server side & one on client side application
i am trying install sql server 2005 from last hour but i can't create
I'm making a simple scheduler with C# in .Net. All it does is execute
i have a tcp server that, when a client connects, it makes a new
I'm making a simple cart program in php. What the code below does is
I am making a simple select from mySQL using PHP. I am doing something
This is similar to my last question. I'm making a simple tcp/ip chat program
Making a simple Java code for Blackjack and need some advice. OK I am
I making simple silverlight application. I need to access and use an image from

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.