#include <sys/socket.h>
#include<stdio.h>
#include <netinet/in.h>
#include<string.h>
int init_socket(int *fd, int port){
struct sockaddr_in serv_addr, cli_addr;
*fd = socket(AF_INET, SOCK_STREAM, 0);
memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
bind(*fd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr))
listen(*fd,1); //Change as per requirement
}
int connect_server()
{
int fd, j, result;
fd_set readset;
init_socket(&fd,6001);
while(1){
FD_ZERO(&readset);
FD_SET(fd, &readset);
select(3, &readset, NULL, NULL, NULL);
}
}
int main(){
printf("Main Start");
connect_server();
printf("Main End");
}
I am trying to execute this code but none of the printf() is displayed. When the select() is removed from the code the printf() works correctly. Why does this happen?
Try again by appending a newline at the end of the string:
int main(){
printf("Main Start\n");
connect_server();
printf("Main End\n");
}
Reason: stdout
is line buffered, and you'll need to give it a newline to flush the output.
Or explicitly flush it by fflush(stdout);
.