Error and unexpected behavior with TCP connection
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!
Not sure if this is all of your problem, but
bzero(buffer, 2);
n = read(socket, buffer, 2);
...
printf("%s", buffer);
is not correct, since if the read actually did get 2 bytes and neither of them were 0 then buffer may not be a null terminated string (unless there is something earlier that made that so).
Several calls to strcmp
should also always fail for the same reason -- the read in string is not 0 terminated so strcmp thinks that it keeps on going and therefore is not the same as the compared to string, even if the first letters are the same.
edit
n = read(socket, buffer, 2);
if (n<0) {
die_horrible_death();
}
buffer[n] = 0; // since arrays are 0 indexed this should be after last read character
printf("I just read: \"%s\"\n", buffer);
Your server code writes only once something to the client, at the beginning. Later, in the loop - it just reads.
OTOH your client reads and writes in the loop.
Since you're working with sockets in a blocking mode - this means that your client eventually will become blocked in a call to recv
.
P.S. No offenses - but you have a lot to learn.
- TCP is a tunnel. When your server sends 2 bytes of data in a single call to
send
/write
- there's no guarantee your client will read this within a single call torecv
. - Hence - reading and parsing always must be done in a sort of a loop.
- You can't write a really robust client/server with ability to timeout/interrupt on demand using only blocking I/O.
- Learn, learn, learn
精彩评论