I'm trying to write a very basic chat client in C which communicates with another machine using sockets, and I'm having some issues with understanding select. Here's a quick snippet of the relevant code.
select(connectFD+1, &readfds, &writefds, NULL, NULL);
if(FD_ISSET(connectFD, &readfds) != 0)
memset(buf, 0, sizeof(buf));
lastBit = recv(connectFD, buf, sizeof(buf), 0);
if (lastBit > 0 && lastBit < 1024)
buf[lastBit] = '\0';
else if (FD_ISSET(STDIN, &writefds))
memset(msg, 0, sizeof(msg));
read(STDIN, msg, sizeof(msg));
You want to read from STDIN so you should add STDIN to the
&readfds and not
&writefds. Writing to STDIN is almost always possible so your code effectively got the information that writing is possible to STDIN but then attempts to read from STDIN and hangs there until the read actually gets possible.