C printf und fprintf(stdout,...) sind nicht gedruckt

Dies ist ein bisschen seltsam. Mein code war nicht das ausgeben, was ich dachte, es sollte. Ich fügte hinzu, einige print-Anweisungen in den verschiedenen Stadien zu sehen, wo es schief lief. Immer noch nichts.
Also habe ich ein printf-Anweisung an den Anfang von main. Das ist, wo ich wirklich verwirrt.

Also nahm ich an, dass etwas komisch war, was mit den Datei-Deskriptoren. Ich habe die printf zu einem fprintf. Immer noch nichts. Auf stderr mit fprintf funktioniert! Warum ist das passiert?

Entfernen alle Körper aus Haupt-mit Ausnahme der ersten print-Anweisung und die Rückkehr druckt.

- Code

int main(void) {
    fprintf(stdout, "STARTED!");
    //Create an Internet domain socket
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    //If this fails exit and print the error
    if (sockfd == -1) {
        printf("Error %d, cannot create socket", errno);
        return 1;
    }
    printf("SOCKET CREATED!");

    //Creates a socket address
    struct sockaddr_in  addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080);
    addr.sin_addr.s_addr = INADDR_ANY;

    //Attempts to bind to the socket address, again prints to error if this fails.
    if (bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
    {
        printf("Error %d, cannot bind", errno);
        return 1;
    }

    //Starts Listening for a client
    if (listen(sockfd, 1) == -1)
    {
        printf("Error %d, cannot listen", errno);
        return 1;
    }

    //If all is successful, server is operational
    while(1)
    {
        //Creates a file descripter for the connection
        int connfd;
        //And a socket address for the client
        struct sockaddr_in cliaddr;
        socklen_t cliaddrlen = sizeof(cliaddr);
        //If a connection attempt is made accepts it.
        connfd = accept(sockfd, (struct sockaddr *) &cliaddr, &cliaddrlen);
        if (connfd == -1) {
            //If the connection fails print an error
            printf("Error %d, cannot accept connection", errno);
            continue;
        }

        //Otherwise process the request
        else {
            printf("CONNECTED!");
            char end;
            end = 1;
            while (end)
            {
                process_request(connfd);
                end = 0;
            }
        }
        close(connfd);

    }
    close(sockfd);
    return 0;
}
  • Oh, und ich bin sicher, dass der code selbst kann verbessert werden, so dass jegliche Verbesserungsvorschläge wäre auch genial!
  • command 1>&2 wird helfen, wenn Sie wollen, stdout werden nicht gepuffert, da stderr.
InformationsquelleAutor Hector | 2013-02-09
Schreibe einen Kommentar