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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:21:00+00:00 2026-06-05T20:21:00+00:00

This is my socket server with fork: #include <stdio.h> #include <string.h> #include <stdlib.h> #include

  • 0

This is my socket server with fork:

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

void do_child(int sock);

int main(int argc, char *argv[]){
    if(argc != 2){
        perror("./server <numero porta>\n");
        exit(1);
    }
    pid_t pid;
    int DescrittoreServer, DescrittoreClient, LunghezzaClient;
    int NumPorta = atoi(argv[1]);
    struct sockaddr_in serv_addr, cli_addr; /* indirizzo del server e del client */
    char Buffer[1024] = {};
    DescrittoreServer = socket(AF_INET, SOCK_STREAM, 0);
    if(DescrittoreServer < 0){
        perror("Errore: creazione socket\n");
        exit(1);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr)); /* bzero scrive dei null bytes dove specificato per la lunghezza specificata */
    serv_addr.sin_family = AF_INET; /* la famiglia dei protocolli */
    serv_addr.sin_port = htons(NumPorta); /* porta */
    serv_addr.sin_addr.s_addr = INADDR_ANY; /* dato che è un server bisogna associargli l'indirizzo della macchina su cui sta girando */

    if(bind(DescrittoreServer, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
        perror("Errore di bind\n");
        close(DescrittoreServer);
        exit(1);
    }
    listen(DescrittoreServer, 5);
    LunghezzaClient = sizeof(cli_addr);
    while(1){
        DescrittoreClient = accept(DescrittoreServer, (struct sockaddr *) &cli_addr, &LunghezzaClient);
        if(DescrittoreClient < 0){
            perror("Errore: non è possibile stabilire la connessione\n");
            close(DescrittoreServer);
            close(DescrittoreClient);
            exit(1);
        }
        pid = fork();
        if(pid < 0){
            perror("Fork error");
            close(DescrittoreServer);
            close(DescrittoreClient);
            exit(1);
        }
        if(pid == 0){
            close(DescrittoreServer);
            do_child(DescrittoreClient);
            exit(0);
        }
        else{
            close(DescrittoreClient);
        }
    }
}

void do_child(int sock){
    int n;
    char Buffer[1024] = {};
    n=read(sock, Buffer, 255);
    if(n < 0){
        perror("Errore nella lettura dalla socket\n");
        exit(1);
    }
    recv(sock, Buffer, sizeof(Buffer), 0);
    printf("Dati ricevuti: %s\n", Buffer);
    strcpy(Buffer, "Dati ricevuti correttamente!");
    send(sock, Buffer, strlen(Buffer)+1, 0);
}

without fork the server and the client work perfectly but after i’ve introduced the fork i got a strange behaviour from them.
Let’s try to explain it:
Behaviour without fork: the client send the message “Ciao sono il client” and the server give the response “Dati ricevuti correttamente”.
Behaviour with fork: nothing happen until i press “CTRL-C” on the client (i’m on Ubuntu). After i’ve pressed CTRL-C the server print the message from the client.
I don’t understand why the messages sharing are not “on demand” but only when i stop the client or the server 🙁

  • 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-05T20:21:02+00:00Added an answer on June 5, 2026 at 8:21 pm

    Your do_child() code reads from the socket first (and then recv again after this), before writing, whereas your parent code contains no socket write code at all (at least the snippet you provided didn’t) so is it possible that your do_child() function is blocking while you read data that is not being sent???

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

Sidebar

Related Questions

I have created my web socket server and client using this simple tutorial here
I have a basic Socket.io server setup like this: var server = express.createServer().listen(port); this.io
I have this socket server script, import SocketServer import shelve import zlib class MyTCPHandler(SocketServer.BaseRequestHandler):
Please help me clear this concept. Say we have A socket port server implemented
i have this response from a socket server: [DataEvent type=data bubbles=false cancelable=false eventPhase=2 data=Segunda
For a TCP server with following structure: main(){ socket(); bind(); listen(); while(1){ accept(); fork();
I'm programming a C/C++ client/server sockets application. At this point, the client connects itselfs
Using Java sockets, I made a simple server. This works because it sends data
Why does this port/socket close once a connection has been made by a client?
Consider this code: import socket store = [] scount = 0 while True: scount+=1

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.