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);
    }
}
9
  • Try telnet localhost:886 on the command line. Commented Aug 18, 2011 at 15:35
  • You can confirm via netstat whether your program is actually listening on the appropriate port. Commented Aug 18, 2011 at 15:35
  • @Daniel telnet is disabled at my end. Commented Aug 18, 2011 at 15:40
  • @Michael @Flavius No item with 886 as port number features in netstat Commented Aug 18, 2011 at 15:40
  • I'm not suggesting you run a telnet server, I'm suggesting you use the telnet client to try to connect to the server you're already running. Alternatively, netcat could be used. Commented Aug 18, 2011 at 15:41

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

And combined with the missing \n you haven't seen the error-message of the bind which is failing.
It is. The test program binds to some random port above 1024 (36969 in one of my launches), and when you connect to the port and send a string, it outputs: Binding failure! Error: -1Received connection: 1test. I guess, -1 is EACCES.
@Duck I was getting this error (after altering as per Jason's answer) but just at that time your answer poped and things seem to be working fine.
@Duck BTW, is there a way to know which are privileged ports and those which are not?
Or, better yet, change the SERVER_PORT constant to make it above 1024, rather than using root.
|
1

Try to put a line-break '\n' at the end of your

 printf ("Received connection: %d\n", conn_count); 

line.

Otherwise the stdout is not flushed directly and you see nothing.

1 Comment

Do that for the other lines as well, otherwise you won't see the error messages :/
0

Rather than using printf() and stdout, try changing all your outputs to fprintf() and stderr which has no buffering ... I would do the same for the error messages as well so that they are output immediately when called and not buffered by stdout.

Finally, if you want to see what the error messages mean, use strerror() with the error number. So you could so something like:

if (bnd < 0)
{
    fprintf(stderr, "Binding failure! Error: %s", strerror(bnd));
    exit(1);
}

Make sure for strerror() you also do an include<string.h> somewhere at the start of your code module.

4 Comments

also, do quit after errors, there is no reason to continue.. this seems to be a small toy server, something you shouldn't use in production. check unpbook.com for more serious stuff.
@Jason This helped now I am getting this: Binding failure! Error: -1 Received connection: 1** ** I am a bit perplexed, it is unable to bind the socket but it still functions!
it doesn't work. But a simple printf does not stop your program, you need some exits/returns or other things around the printed error message.
@john: I've updated my answer with some additional info that will help you debug the error messages a bit better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.