Strange problem with select (or maybe me :-)) - Networking

This is a discussion on Strange problem with select (or maybe me :-)) - Networking ; EDIT: not sure if this got through the first time! I have written a server which accepts a connect and then reads ad nauseum until the other end closes the connection. This works fine when using netcat or telnet normally, ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Strange problem with select (or maybe me :-))

  1. Strange problem with select (or maybe me :-))

    EDIT: not sure if this got through the first time!

    I have written a server which accepts a connect and then reads ad
    nauseum until the other end closes the connection. This works fine
    when using netcat or telnet normally, except when I echo a value into
    the telnet program using a pipe, like so:
    echo Hello | telnet 127.0.0.1 8080

    The program reads the first input ok, but then when I type in
    subsequent input normally in the telnet session does not get read.
    I'm probably doing something stupid so I post a short example program
    for people to have a look at:

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    static char buffer[512];

    int main()
    {
    struct sockaddr_in sn;
    struct timeval tm;
    int r, _1, client, server = socket(AF_INET, SOCK_STREAM, 0);
    if (server < 0) {printf("errno = %s\n", strerror(errno)); exit(1);}

    _1 = 1;

    r = setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &_1, sizeof(_1));
    if (r != 0) { close(server); printf("errno = %s\n", strerror(errno));
    exit(1); }

    memset(&sn, 0, sizeof(sn));
    sn.sin_family = AF_INET;
    sn.sin_addr.s_addr = inet_addr("0.0.0.0");
    sn.sin_port = htons(8080);

    r = bind(server, (struct sockaddr*) &sn, sizeof(sn));
    if (r != 0) { close(server); printf("errno = %s\n", strerror(errno));
    exit(1); }

    r = listen(server, 5);
    if (r != 0) { close(server); printf("errno = %s\n", strerror(errno));
    exit(1); }

    tm.tv_usec = 1000;

    client = accept(server, NULL, NULL);
    if (client < 0) { close(server); printf("errno = %s\n",
    strerror(errno)); exit(1); }

    {
    fd_set in, out, err;
    FD_ZERO(&in);
    FD_ZERO(&out);
    FD_ZERO(&err);
    for(;
    {
    FD_SET(client, &in);
    FD_SET(client, &err);

    r = select(client+1, &in, &out, &err, NULL);

    printf("select returned %d\n", r);

    if (r > 0)
    {
    if (FD_ISSET(client, &err))
    {
    printf("errno = %s\n", strerror(errno));
    close(client);
    close(server);
    exit(5);
    }

    if (FD_ISSET(client, &in))
    {
    ssize_t bytes_transfered = read(client, buffer, 512);
    if (bytes_transfered == 0) { printf("errno = %s\n",
    strerror(errno)); close(client); close(server); exit(3); }
    if (bytes_transfered < 0) { printf("errno = %s\n",
    strerror(errno)); close(client); close(server); exit(4); }
    printf("received %d bytes\n", bytes_transfered);
    }

    } else if (r < 0)
    {
    printf("errno = %s\n", strerror(errno));
    close(client);
    close(server);
    exit(2);
    }
    }
    }

    return 0;
    }


  2. Re: Strange problem with select (or maybe me :-))

    In news:9fd3cdd0-e942-426f-b4f6-ff0f8f76f2c9@k37g2000hsf.googlegroups.com,
    pistmaster@googlemail.com typed:

    > echo Hello | telnet 127.0.0.1 8080
    >
    > The program reads the first input ok, but then when I type in
    > subsequent input normally in the telnet session does not get read.


    Of course not; you instructed telnet to accept stdin from the pipe, not the
    keyboard.



+ Reply to Thread