I’ve developed a client that connects to a server. The client writes something to the server and the server reads and responds back to the client. The client then reads what the server said and both terminate. I wish to extend this allowing the client and server to continue talking for say 3 more time and then terminate. I produced a for loop where inside this loop I handled the code read and write code on both of the server and client, but it is ending up in an infinite loop and writing the text inputted in the first time rather than re asking the user to enter new data.
The server code where the read/write is handled is as follows:
int temp = 0;
char cha [10] = "helloworld";
while(temp = 3){
int rc;
char ch[100];
rc = read(newsockfd, &ch, 100);
printf("Client sent: %s\n", ch ) ;
write(newsockfd, cha + temp, 100);
temp ++;
}
close(sockfd);
close(newsockfd);
This is the client code where read/write is handled:
int temp = 0;
char cha[10] = "abcdefhijk";
while(temp = 3)
{
int rc;
char ch2 [100];
rc = write(sockfd, cha + temp, 100);
read(sockfd, &ch2, 100);
printf("Server said: %s\n", ch2);
temp ++;
}
sleep(2);
close(sockfd);
Also, I am closing the file descriptors after loop is completed.
Any help is greatly appreciated
Could you provide the loop code also? And are you using blocking or non-blocking calls to read() and write()? (that is, do they return immediately independently of whether data is available (non-blocking), or do they block until something is available (blocking))?
Update:
If the code you posted is the actual code, then your while loop is at fault. You check for
Which is always true, as you are just assigning ‘3’ to temp. You probably mean
?