开发者

Linux server programming

开发者 https://www.devze.com 2023-03-28 20:34 出处:网络
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 shou

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);
    }
}


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消