0

Can someone explain why for example this printf isn't being executed before calling the ConnectionSocket() function?

I know sockets are blocking by default, so it wait's until it receives something. But the function at that point isn't executed yet, why doesn't it print "test" on the screen first?

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <stdbool.h>
#include <sys/time.h>
/* Global variables */
unsigned char buffer[1];
struct sockaddr_in serverAddr, clientAddr;
struct timeval timeout={10,0}; //set timeout for 10 seconds 
int udpSocket, slen = sizeof(clientAddr);


int main(){
    printf("test"); // -> for the first time executed when receiving a byte
    initialize_pins();
    ConnectionSocket();
    loop();

    return 0;
}

void ConnectionSocket(){

    /*Create UDP socket*/
    udpSocket = socket(AF_INET, SOCK_DGRAM, 0);

    /*Configure settings in address struct*/
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(7891);
    serverAddr.sin_addr.s_addr = inet_addr("192.168.0.184");
    memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

    /* set receive UDP message timeout */
    setsockopt(buffer,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,sizeof(struct timeval));

    bind(udpSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr));

}
stickfigure4
  • 135
  • 1
  • 3
  • 13

1 Answers1

1

printf() stores the string you want to write in a buffer, and the buffer will be printed on the screen if it is full, or if a '\n' is inserted or if you call fflush(), or if a standard function calls fflush() or if the end of the program is reached.

I wrote a example with write() who prints directly on the screen.

#include <stdio.h>
#include <unistd.h>

int     main(void)
{
    printf("1");                    // stored in a buffer but not printed on screen
    write(STDOUT_FILENO, "2", 1);   // directly printed on screen (2)
    printf("3");                    // stored in a buffer but not printed on screen
    write(STDOUT_FILENO, "4", 1);   // directly printed on screen (4)
    fflush(stdout);                 // flushing stdout, 1 and 3 are printed
    write(STDOUT_FILENO, "5", 1);   // directly printed on screen (5)
    printf("6\n");                  // stored in a buffer then the buffer is printed on the screen because of '\n' (6\n)
    printf("7");                    // stored in a buffer but not printed on screen

    return (0);                     // end of the program, 7 is printed
}

The output is 241356\n7

Gam
  • 634
  • 1
  • 7
  • 16
  • I understand, but why isn't this needed when for example just doing a printf() in the main function, without any other lines of code. – stickfigure4 May 19 '17 at 19:55
  • Because printf's buffer is printed on the screen at the end of the program (if there is something to print). I added an exemple :) – Gam May 19 '17 at 20:21
  • Thanks a lot! because of your example it is very clear now. But what I wanted to mention is, on my Raspberry Pi for example which is Linux it will indeed only print it when a new line character is present, or stdout is flushed or at the end of the program ofcourse. But in windows (tried using visual studio) it does seem to print it directly. So does this behave differently on Windows? – stickfigure4 May 19 '17 at 20:42
  • Actually I thought that it was the same behavior on Windows ... sorry I guess I will try it and do some research too :) – Gam May 19 '17 at 21:04