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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:28:27+00:00 2026-05-18T11:28:27+00:00

I just began learning network programming. My first program is very simple: once connected,

  • 0

I just began learning network programming. My first program is very simple: once connected, the server program displays what the client program sends (characters). Everything works fine until they get connected. At first, I did the sending loop using putc:

while((c = getc(stdin)) != '\n')
    putc(c, (FILE *) connection);

and the receiving one with getc:

while((c = getc((FILE *) connection)) != '\n')
    putc(c, stdout);

Once connected, I get a segmentation fault. Then I tried using send:

while(1) {
    memset(str, null, strlen(memset));

    while(((c = getc(stdin)) != '\n') && sizeof(str) <= 100) {
        strcat(str, (char *) c);       // cast to char * is to make gcc happy
    }

    send(connection, (void *) str, sizeof(str), (int) NULL);
}

and recv:

while((stat = recv(connection,str,100,0)) != 0) {
    printf("\nReceived %d bytes of data from %s: %s", stat, client, str);
}

Now recv() keeps returning -1 and errno is set to 107. What does this mean? Where am I doing wrong? By the way I’m using Linux with gcc.

Thanks a lot for your help! 🙂


EDIT: Even if I used getc() and putc(), I get connection with socket(), then I connect() (client). The server, after obtaining connection (with socket()) bind()s, then listen()s and accept()s. Sorry if I ometted.


Here’s the server code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>

void main(int argc, char *argv[])
{
    struct addrinfo hint;
    struct addrinfo *servinfo, *count;
    struct sockaddr_storage conn_addr;
    struct sockaddr host;

    int connessione;
    int stat;
    int conn_sock;
    int conn_size;
    int success;
    int c;

    char t[INET_ADDRSTRLEN];
    char str[100];
    char *client;

    if(argc != 1 && strcmp(argv[1], "aiuto") == 0) {
        printf("%s(porta)\n",argv[0]);
        printf("porta: specifica su quale porta installarsi\n");
        exit(-1);
    }

    memset(&hint,0,sizeof hint);


    hint.ai_family = AF_INET;
    hint.ai_socktype = SOCK_STREAM;
    hint.ai_flags = AI_PASSIVE;

    if((stat = getaddrinfo(NULL, argv[1], &hint, &servinfo)) != 0)  {
        printf("\n[FATAL]: getaddrinfo: %s",gai_strerror(stat));
        exit(-1);
    }

    success = 0;

    for(count = servinfo; count != NULL; count = count->ai_next) {
        if((connessione = socket(count->ai_family, count->ai_socktype, count->ai_protocol)) <= 0)
            continue;

        if((stat = bind(connessione, count->ai_addr, count->ai_addrlen)) == 0) {
            success = 1;
            break;
        }
        else {
            continue;
        }
    }

    if(success == 0) {
        printf("\n[FATAL]: Unable to bind()!\n");
        exit(-1);
    }
    else {
        printf("\n[DEBUG]: %s: listening...", argv[0]);
    }

    servinfo = count;
    freeaddrinfo(count);

    if(listen(connessione, 20) == -1) {
        printf("\n[FATAL]: listen: %s (%d)\n", gai_strerror(errno), errno);
        close(connessione);
        exit(-1);
    }

    conn_size = sizeof(conn_addr);

    if(accept(connessione, (struct sockaddr *) &conn_addr, &conn_size) == -1) {
        printf("\n[FATAL]: accept: %s", gai_strerror(errno));
        close(connessione);
        exit(-1);
    }

    client = (char *) malloc(INET_ADDRSTRLEN * sizeof(char));
    client = inet_ntop(servinfo->ai_family, &conn_addr, t, INET_ADDRSTRLEN);

    printf("\n[DEBUG]: %s: connection accepted: %s", argv[0], client);

    while((stat = recv(connessione,str,100,0)) != 0) {
        printf("\nReceived %d bytes of data from %s: %s", stat,client, str);
    }

    printf("\n[DEBUG]: connection closed by %s\n",client);
}

And the client’s code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>

void main(int argc, char *argv[])
{
    struct addrinfo hint;
    struct addrinfo *servinfo, *count;
    struct sockaddr_storage conn_addr;
    struct sockaddr host;

    int connessione;
    int stat;
    int conn_sock;
    int conn_size;
    int success;
    int c;

    char t[INET_ADDRSTRLEN];
    char *indirizzo;
    char str[100];

    memset(&hint,0,sizeof hint);

    host.sa_family = AF_INET;
    if(inet_pton(host.sa_family, argv[1], (void *) host.sa_data) == -1) {
        printf("\n[FATAL]: pton: %s (%d)\n", gai_strerror(errno), errno);
        exit(-1);
    }

    hint.ai_family = AF_INET;
    hint.ai_socktype = SOCK_STREAM;
    hint.ai_flags = AI_PASSIVE;
    hint.ai_addr = &host;

    if((stat = getaddrinfo(argv[1], argv[2], &hint, &servinfo)) != 0)  {
        printf("[FATAL]: getaddrinfo: %s\n",gai_strerror(stat));
        exit(-1);
    }

    success = 0;
    for(count = servinfo; count != NULL; count = count->ai_next) {
        if((connessione = socket(count->ai_family, count->ai_socktype, count->ai_protocol)) == -1)
            continue;

        if(connect(connessione, count->ai_addr, count->ai_addrlen) != -1) {
            success = 1;
            break; 
        }
    }

    if(success == 0) {
        printf("\n[FATAL]: impossibile trovare l'host specificato\n");
        exit(-1);
    }

    servinfo = count;
    freeaddrinfo(count);

    indirizzo = (char *) malloc(INET_ADDRSTRLEN * sizeof(char));
    indirizzo = inet_ntop(servinfo->ai_family, &conn_addr, t, INET_ADDRSTRLEN);

    printf("\n[DEBUG]: %s: connesso a: %s\n",argv[0], indirizzo);

    while(1) {
        strcpy(str,"buffer for data to send");

        while(((c = getc(stdin)) != '\n') && sizeof(str) <= 100) {
            strcat(str, (char *) c);
        }

        send(connessione, (void *) str, sizeof(str), (int) NULL);
    }
}

At the end…

It works perfectly! Thanks everyone. 🙂

  • 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-18T11:28:28+00:00Added an answer on May 18, 2026 at 11:28 am

    Normally you don’t use a FILE * handle to do network programming. Rather, you need a socket descriptor returned by the socket(2) system call. Once you have it, you can either connect(2) it (the client) or bind(2) it (the server). The ongoing communication can be performed using recv(2) and send(2).

    If you really want to use a FILE * handle to do network programming, take a look at the fdopen(3) library function. It takes a descriptor that could be returned by socket(2) and returns a FILE * handle that is compatible with fread / fwrite / fprintf.

    After posting your full code, the most significant error that I could see was:

    if (accept(connessione, (struct sockaddr *) &conn_addr, &conn_size))

    accept(2) returns a socket which you have to use with recv(2) / send(2), and you discard it, using the server socket instead.

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

Sidebar

Related Questions

I've just began learning Python and I've ran into a small problem. I need
I am a newb to F#, just began learning it this afternoon. What I've
I've only just recent began learning C++ and am having a little issue with
I've just started one of my courses, as classes just began 2 weeks ago,
I've recently began using dTrace and have noticed just how awesome it is. Its
I took the plunge this afternoon and began studying LINQ, so far just mucking
Just looking for the first step basic solution here that keeps the honest people
I'm just learning DDD (Eric Evans book is open in front of me) and
I just began using Mongoid last week. I am running into this association problem
I just began to learn javascript, so here is a silly question : What

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.