I wrote a simple TCP Server to serve many clients (it works brilliant with telnet). But, I wanted to write also my own simple TCP client with multiplexing socket and stdin input. Connection with server I wrote as usual, the connection code is ok.
I have a problem with multiplexing socket and stdin input. My code which makes the biggest problem:
void multiplexClient(FILE *fp, int sockfd, char inbuffer[], char outbuffer[])
{
int maxfd;
fd_set rset;
FD_ZERO(&rset);
for(;;)
{
FD_SET(fileno(fp), &rset);
FD_SET(sockfd, &rset);
maxfd = std::max(fileno(fp), sockfd) + 1;
int res = select(maxfd, &rset, NULL, NULL, NULL);
if(res < 0)
{
printf("'select error\n");
}
if(FD_ISSET(sockfd, &rset))
{
if(read(sockfd, inbuffer, sizeof(inbuffer)) == 0)
printf("read error\n");
printf("Received: %s\n", inbuffer);
fflush(stdout);
memset(inbuffer, 0, sizeof(inbuffer));
}
else if(FD_ISSET(fileno(fp), &rset))
{
fprintf(stdout, "\n> ");
if(fgets(outbuffer, sizeof(outbuffer), fp) == NULL)
return;
write(sockfd, outbuffer, strlen(outbuffer));
printf("Send: %s\n", outbuffer);
fflush(stdout);
memset(outbuffer, 0, sizeof(outbuffer));
}
}
}
// int main
// sockfd = socket...
// connect
while(1)
{
multiplexClient(stdin, socket_fd, inbuffer, outbuffer);
}
// ...
return 0;
}
My first client:
$ ./client localhost 9034
hello, other client!
> Send: hel
Received: hel
Received: lo! ��~�~���~�
Received: :)
Received: wha
My second client:
$ ./client localhost 9034
hello! :)
> Send: hel
whats is wrong with this chat?!
> Send: lo!
> Send: :)
> Send:
> Send: wha
How to fix this?
Your problem lies here (slightly paraphrased):
read(sockfd, inbuffer, sizeof(inbuffer));
printf("Received: %s\n", inbuffer);
The read
call returns the number of bytes written (which you ignore) and the buffer is not a C string (ie, it's not null terminated).
What you should have is something along the following lines:
quant = read (sockfd, inbuffer, sizeof(inbuffer));
printf ("Received: %*.*s\n", quant, quant, inbuffer);