If I understand this system call "select" correctly, it will loop waiting for user input from the keyboard or from an outside server. Every time I call "message_loop", I'm going to type in the same few lines of input each time. Is there a way to hard code this in so I don't have to type it in each time?
void message_loop(FILE* fpin, FILE* fpout, Socket sock)
{
fd_set readfds, readfds_bak ;
int in, max_fd, n, ret ;
char buf[MAXMESG];
in = fileno(fpin) ;
FD_ZERO(&readfds) ;
FD_SET(in, &readfds) ;
FD_SET(sock.socketfd, &readfds) ;
readfds_bak = readfds ;
max_fd = ((in > sock.socketfd) ? in : sock.socketfd) + 1 ;
while(1){
readfds = readfds_bak ;
/* select function */
if((ret = select(max_fd, &readfds, NULL, NULL, NULL)) < 0){
perror("select") ;
break ;
}
else if (ret != 0) {
if(FD_ISSET(in, &readfds)){
/* keyboard input */
fgets(buf, MAXMESG, fpin) ;
if(send_message(buf, sock) == -1)
break ;
}
if(FD_ISSET(sock.socketfd, &readfds)){
/* messages from server */
n = receive_message(buf, MAXMESG, &sock) ;
if(n == -1)
break ;
else if(n > 0){
fputs(buf, fpout) ;
fputc('\n', fpout) ;
}
fflush(stdout) ;
}
}
}
}