Socket programming problem: can't generate output in server socket

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ferbar

    #1

    Socket programming problem: can't generate output in server socket

    Hello all,

    This may sound pretty basic stuff.. but I'm working on a socket example
    whose client seems to work fine, but the server doesn't send to the
    client the expected result. The problem is that I want to trace what
    the server socket is doing, but I'm unable to see any of my fprintf or
    printf stuff.

    Please take a look to the example:

    #include <stdlib.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <string.h>
    #include <stdio.h>
    #include <unistd.h>

    //x ------------------------------------------------------------ x

    #define MAXLINE 4096 /* max text line length */

    int main(int argc, char **argv)
    {
    int listenfd, connfd; // socket file descriptor
    struct sockaddr_in servaddr; // IPv4 socket address structure
    char buff[MAXLINE];
    // *************** *****
    char file[32];
    FILE *fp;
    strcpy(file,"ou tput.txt");

    if ((fp = fopen(file, "w")) == NULL)
    {
    printf("Can't open %s\n", file);
    exit(1);
    }
    else
    fprintf(fp, "\nFirst step...");
    // *************** *****


    listenfd = socket(AF_INET, SOCK_STREAM, 0); // call to socket function

    bzero(&servaddr , sizeof(servaddr )); // initialization of socket
    structure to 0
    servaddr.sin_fa mily = AF_INET;
    servaddr.sin_po rt = htons(9877); // Port in host byte order must be
    converted
    // to network byte order
    servaddr.sin_ad dr.s_addr = htonl(INADDR_AN Y);
    // INADDR_ANY - wild card
    // This tells the kernel to choose the IP address
    bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr ));

    fprintf(fp, "\nWaiting for connection, BEFORE call to listen()");
    listen(listenfd , 5);
    fprintf(fp, "\nWaiting for connection, AFTER call to listen()");
    for ( ; ; ) {

    if (connfd = accept (listenfd, NULL, NULL) < 0)
    fprintf(fp, "\nERROR on accept");
    else
    fprintf(fp, "\nSUCCESS on accept");
    // We are not interested in knowing the identity of the client
    // Therefore, 2nd and 3rd param. to NULL
    strcpy(buff, "Output to the client");
    snprintf(buff, sizeof(buff), "%%" );
    write(connfd, buff, strlen(buff));
    close(connfd);
    }
    // exit(0);

    // *************** *****
    fclose(fp);
    // *************** *****
    }


    As you can see, there are many fprintf instructions which work fine in
    my socket client but not in the server. I guess I'm missing some
    conceptual stuff here. Any idea?

    Thanks!

    Fernando

  • Jack Klein

    #2
    Re: Socket programming problem: can't generate output in server socket

    On 20 Jun 2005 20:13:23 -0700, "ferbar" <fbarsoba@hotma il.com> wrote
    in comp.lang.c:
    [color=blue]
    > Hello all,
    >
    > This may sound pretty basic stuff.. but I'm working on a socket example
    > whose client seems to work fine, but the server doesn't send to the
    > client the expected result. The problem is that I want to trace what
    > the server socket is doing, but I'm unable to see any of my fprintf or
    > printf stuff.
    >
    > Please take a look to the example:
    >
    > #include <stdlib.h>
    > #include <netinet/in.h>[/color]

    Not a standard C header.
    [color=blue]
    > #include <sys/socket.h>[/color]

    Not a standard C header.
    [color=blue]
    > #include <sys/types.h>[/color]

    Not a standard C header.
    [color=blue]
    > #include <string.h>
    > #include <stdio.h>
    > #include <unistd.h>[/color]

    Not a standard C header.

    [snip]

    Your question and code are not topical here, they are full of
    non-standard extensions that are not part of the language. The C
    language has no built-in support for any sort of networking.

    Your best place to take this is most like news:comp.unix. programmer,
    but it is highly recommended that you read their socket FAQ before
    asking networking questions.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    • Fernando Barsoba

      #3
      Re: Socket programming problem: can't generate output in server socket

      Jack Klein wrote:[color=blue]
      > On 20 Jun 2005 20:13:23 -0700, "ferbar" <fbarsoba@hotma il.com> wrote
      > in comp.lang.c:
      >
      >[color=green]
      >>Hello all,
      >>
      >>This may sound pretty basic stuff.. but I'm working on a socket example
      >>whose client seems to work fine, but the server doesn't send to the
      >>client the expected result. The problem is that I want to trace what
      >>the server socket is doing, but I'm unable to see any of my fprintf or
      >>printf stuff.
      >>
      >>Please take a look to the example:
      >>
      >>#include <stdlib.h>
      >>#include <netinet/in.h>[/color]
      >
      >
      > Not a standard C header.
      >
      >[color=green]
      >>#include <sys/socket.h>[/color]
      >
      >
      > Not a standard C header.
      >
      >[color=green]
      >>#include <sys/types.h>[/color]
      >
      >
      > Not a standard C header.
      >
      >[color=green]
      >>#include <string.h>
      >>#include <stdio.h>
      >>#include <unistd.h>[/color]
      >
      >
      > Not a standard C header.
      >
      > [snip]
      >
      > Your question and code are not topical here, they are full of
      > non-standard extensions that are not part of the language. The C
      > language has no built-in support for any sort of networking.
      >
      > Your best place to take this is most like news:comp.unix. programmer,
      > but it is highly recommended that you read their socket FAQ before
      > asking networking questions.
      >[/color]
      Thanks for the reference. I will do so..

      Fernando

      Comment

      • Dave Thompson

        #4
        Re: Socket programming problem: can't generate output in server socket

        On 20 Jun 2005 20:13:23 -0700, "ferbar" <fbarsoba@hotma il.com> wrote:
        [color=blue]
        > Hello all,
        >
        > This may sound pretty basic stuff.. but I'm working on a socket example
        > whose client seems to work fine, but the server doesn't send to the
        > client the expected result. The problem is that I want to trace what
        > the server socket is doing, but I'm unable to see any of my fprintf or
        > printf stuff.[/color]

        Snipping much of the offtopic socket stuff:
        [color=blue]
        > char file[32];
        > FILE *fp;
        > strcpy(file,"ou tput.txt");
        >
        > if ((fp = fopen(file, "w")) == NULL)
        > {
        > printf("Can't open %s\n", file);
        > exit(1);
        > }
        > else
        > fprintf(fp, "\nFirst step...");[/color]
        <snip>[color=blue]
        > fprintf(fp, "\nWaiting for connection, BEFORE call to listen()");
        > listen(listenfd , 5);
        > fprintf(fp, "\nWaiting for connection, AFTER call to listen()");
        > for ( ; ; ) {
        >
        > if (connfd = accept (listenfd, NULL, NULL) < 0)
        > fprintf(fp, "\nERROR on accept");
        > else
        > fprintf(fp, "\nSUCCESS on accept");
        > // We are not interested in knowing the identity of the client
        > // Therefore, 2nd and 3rd param. to NULL
        > strcpy(buff, "Output to the client");
        > snprintf(buff, sizeof(buff), "%%" );
        > write(connfd, buff, strlen(buff));[/color]

        This snprintf call writes to (the beginning of) buff a string
        consisting of a single percent sign, so that's what your <offtopic>
        write() sends. If that's not what you wanted, that's your error.
        [color=blue]
        > close(connfd);
        > }
        > // exit(0);
        >
        > // *************** *****
        > fclose(fp);
        > // *************** *****
        > }[/color]

        You don't actually need to code an fclose() just before returning from
        main(), per below, but it is a good idea in general.
        [color=blue]
        >
        >
        > As you can see, there are many fprintf instructions which work fine in
        > my socket client but not in the server. I guess I'm missing some
        > conceptual stuff here. Any idea?
        >[/color]
        I don't see any client (code) in your post at all, and no printf (only
        fprintf). Most likely your client is coded (as most are) to do one
        connection and then exit the program, whereas your server is coded to
        loop accepting connections forever; you must have interrupted or
        killed it somehow when done. This is an important difference: stdio
        output to a file which is not an interactive terminal, and
        "output.txt " presumably isn't, is permitted to be and normally is
        buffered, and only written when the buffer is full (which probably
        didn't happen) or you call fclose() or fflush() which you don't (you
        coded fclose() but never reach it because of the infinite loop) or the
        program exits _normally_ (by calling exit() or returning from main)
        which you don't. "Killing" with control-C or similar causes this
        buffered data to be lost.

        You could just use printf(), or fprintf (stdout, ...) which per above
        to a terminal "should" not be more than line-buffered. (However, some
        C implementations might fail to characterize as interactive some kinds
        of terminals or pseudoterminals .) And you need to "dedicate" a
        terminal to the server as long as it runs, to avoid much confusion.

        Or sprinkle fflush(fp) at various places; or after fopen()ing do
        setvbuf (fp, NULL, 0, _IONBF) which effectively does a fflush for you
        on every output operation, or _IOLBF which flushes at each end-of-line
        and change your outputs to _end_ (not begin) with \n.

        - David.Thompson1 at worldnet.att.ne t

        Comment

        Working...