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