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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:56:11+00:00 2026-05-24T19:56:11+00:00

I got this code from Andy Tanenbaum’s book. I am trying to run it.

  • 0

I got this code from Andy Tanenbaum’s book. I am trying to run it. It compiles and waits for connection. But when I type localhost:886, I see no effect either in the browser or the terminal. (It should echo a new connection string as per the code).

#define     SERVER_PORT         886     /* The port at which the server listens */
#define     BUF_SIZE        64032       /* The size of buffer for request and response */
#define     QUEUE_SIZE      10      /* Block transfer size */


int main (int argc, char* argv[]) {

    int     sock = 0, bnd = 0, lst = 0, fd = 0, sa = 0, bytes = 0, on = 1, conn_count = 0;
    char    buf[BUF_SIZE];
    struct  sockaddr_in channel;            /* IP address holder */

    /* The address structure that binds with the socket */

    memset (&channel, 0, sizeof(channel));

    channel.sin_family      =   AF_INET;
    channel.sin_addr.s_addr     =   htonl (INADDR_ANY);
    channel.sin_port        =   htons (SERVER_PORT);

    /* Parital opening of the socket, while waiting for connection */

    sock                =   socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); /* Creates a new socket */
    if (sock < 0)
        printf ("Partial opening of the socket failed! Error: %d", sock);

    setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));

    bnd             =   bind (sock, (struct sockaddr *) &channel, sizeof(channel));
    if (bnd < 0)
        printf ("Binding failure! Error: %d", bnd);

    lst             =   listen (sock, QUEUE_SIZE);
    if (lst < 0)
        printf ("Unable to listen on the socket! Error: %d", lst);

    /* The socket has been set-up. Wait for the connection and process it. */

    while (1) {

        conn_count      +=  1;
        printf ("Received connection: %d", conn_count); 
        sa          =   accept (sock, 0, 0);
        if (sa < 0)
            puts ("Unable to accept the socket opened for connection.");

        read (sa, buf, BUF_SIZE);

        puts (buf);     /* Output the string to the screen */

        close (sa);
    }
}
  • 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-24T19:56:12+00:00Added an answer on May 24, 2026 at 7:56 pm

    Are you running this as sudo/root? Your bind() might be failing because it is using privileged port.


    [update from comment:]

    Privileged ports are all ports below 1024.

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

Sidebar

Related Questions

I got this piece of code from a book viz. sams teach yourself android
I've got this code I'm trying to use to export data from Excel to
How can I fetch images from a server? I've got this bit of code
got this code from a website that helped me in creating buttons and stuff.
I got this code from w3schools, and tweaked it based on this one: Rename
I got some of this code from another stackoverflow answer of how to add
I am trying to figure out .net and got this code that when I
I got this code below from a tutorial I'm using to learn PHP. I
I got this bit of code from someone's blog years ago. It basically iterates
I have this code from a coworker (probably got it from the web somewhere)

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.