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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:46:32+00:00 2026-06-09T03:46:32+00:00

accept() is defined to always create another file descriptor to accept new connections from

  • 0

accept() is defined to always create another file descriptor to accept new connections from the client, but if it is known beforehand that we are only going to be accepting one client and one connection, why bother with creating a new file descriptor? Are there any descriptions of why this is the case in any defined standards?

  • 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-09T03:46:33+00:00Added an answer on June 9, 2026 at 3:46 am

    The TCP protocol described in RFC 793 describes the terms socket and connection. A socket is an IP address and port number pair. A connection is a pair of sockets. In this sense, the same socket can be used for multiple connections. It is in this sense that the socket being passed to accept() is being used. Since a socket can be used for multiple connections, and the socket passed to accept() represents that socket, the API creates a new socket to represent the connection.

    If you just want an easy way to make sure the one socket that accept() creates for you is the same socket you used to do the accept() call on, then use a wrapper FTW:

    int accept_one (int accept_sock, struct sockaddr *addr, socklen_t *addrlen) {
        int sock = accept(accept_sock, addr, addrlen);
        if (sock >= 0) {
            dup2(sock, accept_sock);
            close(sock);
            sock = accept_sock;
        }
        return sock;
    }
    

    If you are wanting a way for a client and server to connect to each other, without creating any more than just one socket on each side, such an API does exist. The API is connect(), and it succeeds when you achieve a simultaneous open.

    static struct sockaddr_in server_addr;
    static struct sockaddr_in client_addr;
    
    void init_addr (struct sockaddr_in *addr, short port) {
        struct sockaddr_in tmp = {
            .sin_family = AF_INET, .sin_port = htons(port),
            .sin_addr = { htonl(INADDR_LOOPBACK) } };
        *addr = tmp;
    }
    
    void connect_accept (int sock,
                         struct sockaddr_in *from, struct sockaddr_in *to) {
        const int one = 1;
        int r;
        setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
        bind(sock, (struct sockaddr *)from, sizeof(*from));
        do r = connect(sock, (struct sockaddr *)to, sizeof(*to)); while (r != 0);
    }
    
    void do_peer (char *who, const char *msg, size_t len,
                  struct sockaddr_in *from, struct sockaddr_in *to) {
        int sock = socket(PF_INET, SOCK_STREAM, 0);
        connect_accept(sock, from, to);
        write(sock, msg, len-1);
        shutdown(sock, SHUT_WR);
        char buf[256];
        int r = read(sock, buf, sizeof(buf));
        close(sock);
        if (r > 0) printf("%s received: %.*s%s", who, r, buf,
                          buf[r-1] == '\n' ? "" : "...\n");
        else if (r < 0) perror("read");
    }
    
    void do_client () {
        const char msg[] = "client says hi\n";
        do_peer("client", msg, sizeof(msg), &client_addr, &server_addr);
    }
    
    void do_server () {
        const char msg[] = "server says hi\n";
        do_peer("server", msg, sizeof(msg), &server_addr, &client_addr);
    }
    
    int main () {
        init_addr(&server_addr, 4321);
        init_addr(&client_addr, 4322);
        pid_t p = fork();
        switch (p) {
        case 0:  do_client(); break;
        case -1: perror("fork"); exit(EXIT_FAILURE);
        default: do_server(); waitpid(p, 0, 0);
        }
        return 0;
    }
    

    If instead you are worried about performance issues, I believe those worries are misguided. Using the TCP protocol, you already have to wait at least one full round trip on the network between the client and the server, so the extra overhead of dealing with another socket is negligible. A possible case where you might care about that overhead is if the client and server are on the same machine, but even then, it is only an issue if the connections are very short lived. If the connections are so short lived, then it would probably be better to redesign your solution to either use a cheaper communication medium (e.g., shared memory), or apply framing on your data and use a persistent connection.

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

Sidebar

Related Questions

This seems like a bug to me... I accept that automatic properties, defined as
I have a mule-config file where i have defined a http inbound to accept
In django, I defined url like that (r'^checkstring/(?P<string>\w+)/$',views.check_str,name='check str') But, When i enter string
I need to write a TSQL user defined function which will accept a string
I'm trying to define a method in my vb.net interface that will only accept
I have an accept function defined on the red box in the fiddle. Expected
I'm creating a custom time that should be used in an empty XAML file:
Sometimes when reading others' C# code I see a method that will accept multiple
The steps I go through... Add new ADO.NET Entity Data Model > Generate from
In a scenario where I have a UI that will be updated from a

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.