hi..
I am wanting my TCP server and client to "talk" to each other depending on what the other says. But when I run, it seems to go through one iteration, but then both the server and client seem to just "hang" there. Once I kill it on the client side, I end up getting a broken pipe error.
Can anyone interpret what I am doing wrong here?
SERVER code snippet:
while(feof(file_ptr) == 0)
{
fscanf(file_ptr, "%s", supplies);
printf("%s\n", supplies);
//SEND supplies to all clients (currently just one client)
n = write(newsockfd, supplies, strlen(supplies));
if (n < 0)
{
error("ERROR writing to socket");
}
//Receive a request from client that they can use supply
bzero(buffer,2);
n = read(newsockfd,buffer,2);
if (n < 0)
{
error("ERROR reading from socket");
}
printf("Who is using supply? %s\n", buffer);
if(strcmp(buffer, "A"))
{
aRounds++;
}
while(done != 1)
{
//WAIT loop until message CO is received from client
bzero(buffer,2);
n = read(newsockfd,buffer,2);
if (n < 0)
{
error("ERROR reading from socket");
}
printf("Done? %s\n", buffer);
if(strcmp(buffer, "CO"))
{
done = 1;
}
}
done = 0;
}
fclose(file_ptr);
n = write(newsockfd, "DN", 2);
CLIENT code snippet:
while(noSupplies != 1)
{
//RECEIVE MSG from server about supplies
bzero(buffer,2);
n = read(sockfd,buffer,2);
if (n < 0)
{
error("ERROR reading from socket");
}
printf("%s\n",buffer);
if(strcmp(buffer, "BC") == 0 || strcmp(buffer, "CB") == 0)
{
//SEND server msg that you are using supply
n = write(sockfd,"A", 1);
if (n < 0)
{
error("ERROR writing to socket");
}
printf("Client A has received components %s during round %d.\n", buffer, round);
n = write(sockfd,"CO", 2);
if (n < 0)
{
error("ERROR writing to socket");
}
}
else if(strcmp(buffer, "DN"))
{
noSupplies = 1;
}
round++;
}
I get the following from the server (before killing):
BC
Who is smoking? A
Done smoking? CO
And the following from the client (before killing):
BC
Client A has received components BC during round 1.
Then after killing from the client.. I get the following from the server:
BC
Who is using supply? A
Done? CO
Done?
CB
Who is using supply?
Done?
CB
Who is using supply?
Done?
BC
Broken pipe
Does anyone understand what I am doing wrong? (looking for code fix!)
Just an FYI.. I will be changing the server code eventually to handle listening to mulitple clients (TCP) but one hurdle at a time right? :S
Thanks!