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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:30:10+00:00 2026-06-03T22:30:10+00:00

I have written a program to receive a piece of data from a network

  • 0

I have written a program to receive a piece of data from a network via socket and write it down to a file. I have used the following code for the purpose:

FILE *log;
log = fopen("time.log", "a"); 
fprintf(log,"HI all");
while(1)
{  
    sin_size = sizeof(struct sockaddr_in);
    connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
    printf("\n I got a connection from (%s , %d)", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    fflush(stdout);           
    while(1)
    {       
        fflush(stdout);
        fgets(send_data,1000,stdin);
        send(connected, send_data,strlen(send_data), 0);
        bytes_recieved = recv(connected,recv_data,1024,0);
        recv_data[bytes_recieved] = '\0';
        char newln[2]="\n";
        int len=strlen(recv_data), len1=strlen(newln);
        fwrite(recv_data, len, 1, log);
        fwrite(newln, len1, 1, log);
        fflush(stdout);
    }
}   
fclose(log);

If the file doesnot exist, the fopen succesfully creates the file, but after that nothing happens. No data is written into the file. neigther the “HI all” nor the received data. yes, the data is being received, i checked it by printing the received data. Please help me out. Thanks in advance. Operating platform is linux.

  • 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-03T22:30:11+00:00Added an answer on June 3, 2026 at 10:30 pm

    First of all you should check the return value of fopen() to ensure that, log is not NULL. Afterwards, you should use fflush(log) just before fflush(stdout).

    Another thing to mention is that you will never get out of the second while(1) loop, so you should also fix this.

    if (bytes_received == 0) {
        /* client closed the connection */
        close(connected);
        break;
    }
    recv_data[bytes_recieved] = '\0';
    char newln[2]="\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning java and following a book. I have a program written via
I have a requirement to send data from native program written in C through
I have written a C++ program for a University assignment. I used Netbeans 6.8
I need to write a program in 8086 Assembly that receives data from the
I have written a program which strips the tags from a HTML website which
I have written some piece of code for my program in Matlab 7.10.0 which
I have written a program in VB6. When I compile it and send it
I have written a program that uses qhttp to get a webpage. This works
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have written my program in python. It is written across seven files and

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.