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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:06:33+00:00 2026-06-13T02:06:33+00:00

I’ve been trying to receive data on a server socket from a client with

  • 0

I’ve been trying to receive data on a server socket from a client with the following code,

server.c

int startServer(uint16_t myPort)
{
  int listenFd = 0, connfd = 0;
  struct sockaddr_in serv_addr;

  listenFd = socket(AF_INET, SOCK_STREAM, 6);
  if (listenFd == -1) {
    perror("socket");
    return EXIT_FAILURE;
  }

  memset(&serv_addr, '0', sizeof(serv_addr));

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(myPort);

  if (-1 == bind(listenFd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) {
    perror("bind");
    return EXIT_FAILURE;
  }

  if (-1 == listen(listenFd, 10)) {
    perror("listen");
    return EXIT_FAILURE;
  }

  printf("Server listening on %d\n", myPort);

  int n = 0, k;
  char recvBuff[1024];
  struct sockaddr_in conn_addr;
  unsigned int len = sizeof(conn_addr);

  while(1)
  {
     connfd = accept(listenFd, (struct sockaddr*)NULL, NULL);

     n = recv(connfd, &recvBuff, 1023, 0);
     printf("Recd: %d bytes\n",n);
     for (k=0; k<16; ++k) { printf("%d: 0x%02X, ", k, recvBuff[k]); }

     sleep(1);
   }
}

and in client.c

int main(int argc, char *argv[])
{
    int sockfd = 0, n = 0;
    struct sockaddr_in serv_addr; 

    if(argc != 2)
    {
        printf("\n Usage: %s <ip of server> \n",argv[0]);
        return 1;
    } 

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
        return 1;
    } 

    memset(&serv_addr, '0', sizeof(serv_addr)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(5000); 

    if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
    {
        printf("\n inet_pton error occured\n");
        return 1;
    } 

    if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
       printf("\n Error : Connect Failed \n");
       return 1;
    } 

    uint32_t dataVal = 0xa0b0c0d0;
    int res = write(sockfd, (void*)&dataVal, sizeof(dataVal));
    printf("%d bytes sent\n", res);
    close(sockfd);
    return 0;
}

However with this I see the server saying it received the bytes but the line after that printing the bytes (in a for loop) doesn’t ever seem to be executed. Does anyone know what I am doing wrong here?

  • 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-13T02:06:34+00:00Added an answer on June 13, 2026 at 2:06 am

    Add printf("\n"); after the for loop, or “\n” to the printf inside the for loop.

    printf writes to stdout, which is buffered. This means that output will not display until the buffer is filled, flushed, or a newline is sent. As such, it is easiest to ensure output by sending a newline.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
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 am trying to understand how to use SyndicationItem to display feed which is
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 using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

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.