I am writing a simple IRC client, and I want to read from stdin. Currently I am selecting on file descriptor 0 thusly:
Code:
while(1) {
FD_SET(conn->sock, &rd);
FD_SET(0, &rd);
FD_SET(conn->sock, &wr);
FD_SET(conn->sock, &ex);
ret = select(conn->sock + 1, &rd, &wr, &ex, NULL);
if(ret == -1)
return 1;
if(FD_ISSET(conn->sock, &rd)) {
/* Process incoming messages */
}
if(FD_ISSET(0, &rd)) {
sz = read(0, &c, 1);
fprintf(stderr, "C\n");
}
if(FD_ISSET(conn->sock, &wr)) {
/* Send messages in send queue */
}
if(FD_ISSET(conn->sock, &ex)) {
fprintf(stderr, "Socket exception\n");
return 1;
}
}
I expected to see something like 'aCdCeC' if I typed ade. What actually happens is that 'ade' is echoed, then when I press enter 'CCCC' is printed (the extra C is for the line feed).
I have tried:
Code:
setvbuf(stdin, NULL, _IONBF, 0);
I was not surprised that this sis not work, as it is operating on the FILE* stream, not the file descriptor.
I want to read a character as soon as it is pressed. Any ideas?
Thanks