cout does not display!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <string.h>	//strlen
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>	//close
#include <arpa/inet.h>	//close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>	//FD_SET, FD_ISSET, FD_ZERO macros
#include <vector>
#include <pthread.h>
#include <iostream>
using namespace std;

#define BUFFSIZE 255
#define TRUE   1
#define FALSE  0

// ALL GLOBAL VARIABLES HERE

	int master_socket;
	int addrlen;
	struct sockaddr_in serverAddr;
	struct request{
			int socket;
			char* action;
		};

	vector<int> client_sockets;
	signed long int numOfClients = client_sockets.size();
	vector<request> client_requests;
// GLOBAL VARIABLES END
void ConnectionThread(){
 cout << "Starting connection thread..." << endl;
 fd_set sockets_set;
 while(TRUE){
	 cout << "This does not display!";
    FD_ZERO(&sockets_set);
    FD_SET(master_socket, &sockets_set);

   int activity = select(1, &sockets_set, NULL, NULL, NULL);
    if ((activity < 0) && errno != EINTR){
    	cout << "Could not select!";
    	exit(EXIT_FAILURE);
    }
    if(FD_ISSET(master_socket, &sockets_set)){
    	int new_socket;
    	if((new_socket = accept(master_socket, (struct sockaddr *)&serverAddr, (socklen_t*)&addrlen))<0){
    		cout << "Could not accept socket!";
    	}
    	cout << "NEW SOCKET!";
    }
 }
}


int main(int argc, char *argv[]){

cout << "[SERVER] Initializing.." << endl;

	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(8888);
	serverAddr.sin_addr.s_addr = INADDR_ANY;
	addrlen = sizeof(serverAddr);
	if((master_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0){
		printf("Could not create socket!");
	}
	if ((bind(master_socket, (struct sockaddr *)&serverAddr, sizeof(serverAddr))) < 0){
		printf("Could not bind address to socket!");
		exit(EXIT_FAILURE);
	}
	if (listen(master_socket, 20) < 0){
		printf("Could not listen on socket!");
		exit(EXIT_FAILURE);
	}
 cout << "[SERVER] Started up successfully." << endl;
 ConnectionThread();
return 0;
}


The cout in the while loop does not display. What could be the cause of this?
Well. I just figured out that select() had the wrong number of nfds--After increasing that number, cout started to display.

What I don't get is, the command cout is before select(), so why does it not show up regardless of the wrong usage of select()?
there was no flush, endl, cin, or cerr operation (flush points in C++) , and there was no \n even (extra flush point in linux), so your code didn't actually send anything through the stdout file descriptor at that time.
Last edited on
So should I put endl on all of them?

And how do I use flush?
If you want your output to appear before a pause, you should flush it in some manner. std::cout << whatever << std::flush (or std::cout.flush()) is the simplest
cout << "Something" << endl;
which is really the same as:
cout << "Something" << '\n' << flush;
Topic archived. No new replies allowed.