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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:31:17+00:00 2026-06-13T11:31:17+00:00

i’m new to C socket programming and i want to build a server/client application

  • 0

i’m new to C socket programming and i want to build a server/client application in which client reads and sends partial data to the server from a text file (which is 46mb) and i want the server to print the received part of the file to the console.

Am i doing this all wrong? Server prints blank lines to the console at the moment. I couldnt figure out why :/

Here is the client:

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

void error(char *msg) {
  perror(msg);
  exit(1);
}

int main( int argc , char *argv[] ) {
  int sockfd , port_no , n;
  struct sockaddr_in server_addr;
  struct hostent *server;
  char buffer[256];
  FILE *fp;

  if(argc < 3) {
    fprintf(stderr , "usage %s hostname port" , argv[0] );
    exit(0);
  }

  port_no = atoi( argv[2] );
  sockfd = socket( AF_INET , SOCK_STREAM , 0);
  if( sockfd < 0 )
    error("ERROR opening socket");

  server = gethostbyname(argv[1]);
  if( server == NULL ) {
    fprintf(stderr , "ERROR, no such host.");
    exit(0);
  }

  bzero( (char *) &server_addr , sizeof(server_addr) );
  server_addr.sin_family = AF_INET;
  bcopy( (char *)server -> h_addr , (char *) &server_addr.sin_addr.s_addr , server -> h_length );
  server_addr.sin_port = htons(port_no);

  if( connect( sockfd , (char *) &server_addr , sizeof(server_addr)) < 0 ) {
    error("ERROR , connecting");
  }

  bzero(buffer , 256); 


  if( (fp = fopen("text.txt" , 'r')) != NULL ) {
    while(1) {
      if( (fgets(buffer , 255 , fp)) != NULL ) {
        n = write( sockfd , buffer , strlen(buffer) );
        if( n < 0 ) {
          error("ERROR writing to socket");
        }
      }
    }
    fclose(fp);
  }

}

Here is the server:

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

void error(char *msg) {
  perror(msg);
  exit(1);
}

int main(int argc , char *argv[]) {
  int sockfd , newsockfd , port_no , cli_length , n; 
  char buffer[256];
  struct sockaddr_in server_addr , client_addr;

  if(argc < 2) {
    fprintf(stderr , "ERROR , no port provided!");
    exit(1);
  }

  sockfd = socket(AF_INET , SOCK_STREAM , 0);

  if( sockfd < 0 )
    error("ERROR opening socket.");

  bzero( (char *) &server_addr , sizeof(server_addr) );
  port_no = atoi(argv[1]);

  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons( port_no );       
  server_addr.sin_addr.s_addr = INADDR_ANY;

  if( bind( sockfd , (struct sockaddr *) &server_addr , sizeof(server_addr)) < 0 ) {
    error("ERROR on binding.");
  }

  listen(sockfd , 5);

  cli_length = sizeof(&client_addr);
  newsockfd = accept( sockfd , (struct sockaddr *) &client_addr , &cli_length );
  if (newsockfd < 0 )
    error("ERROR on accept.");

  bzero(buffer , 256);
  while(1) {
    n = read(newsockfd , buffer , 255);
    if( n < 0 ) {
      error("ERROR reading from socket");
    }
    printf(" %s " , buffer);
  }


}
  • 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-13T11:31:18+00:00Added an answer on June 13, 2026 at 11:31 am

    I think your problem is with NULL termination of the buffer. Try

    printf( "%.256s" , buffer );
    

    If the buffer can contain legitimate \0 characters I would try printing a character at a time:

    { 
       for (int i=0; i<256; i++)
           printf("%c" , buffer[i]);
    }
    

    Joakim

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
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 used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.