I have written a server/client based c program. The basic function of the program is that client will send a character e.g to server, The server will increment it and send the character back. So if a client sends 'a' it will receive 'b'. But when the client sends a 'q' the server is supposed to close the connection. The program is doing what its supposed to do but on client side its printing "Enter a character" and "Character from server" twice. Also sending a 'q' does close the connection but only if you send it for the second time.
Here is the server side code
void *client_thread(void *client_sockfd){
int socket=*(int *)client_sockfd;
while(1){
read(socket, &ch, 1);
if(ch=='q')
{
close(socket);
}
else
{
ch++;
write(socket, &ch, 1);
}
}
//close(socket);
printf("Connection closed with %d",socket);
}//end fucntion
client side code
while(1)
{
printf("Enter a character");
scanf("%c",&ch);
write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
printf("char from server = %c\n", ch);
// close(sockfd);
//exit(0);
}
if(ch=='q'){
printf("Server closed the connection");
}