multiple recv() calls required
- From: "Simon Morgan" <nobody@xxxxxxxxxxx>
- Date: 21 Aug 2006 15:44:47 GMT
Can somebody please explain why it takes (at least) 2 calls to recv() to
fetch a definition from a DICT server, regardless of whether the DEFINE
command is sent before or after the first recv()?
The way I see it is:
* I connect() and the server sends the banner, but I don't recv() it. * I
send the DEFINE command.
* The server sends the definition.
* I call recv().
What I don't understand is why recv() only copies the server banner into
the buffer when there is enough room for part of the definition as well.
The man page mentions "messages" which hints at an explanation but is
somewhat at odds with my idea of TCP as being about sending and receiving
streams of data.
Here's some example code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
int main(int argc, char *argv[]) {
char *cmd = "DEFINE english hello\n";
char buffer[256];
int sockfd, n;
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(PF_INET, SOCK_STREAM, 0);
server = gethostbyname("dict.org");
bzero(&serv_addr, sizeof serv_addr);
serv_addr.sin_family = AF_INET;
bcopy(server->h_addr, &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(2628);
connect(sockfd, (struct sockaddr *)&serv_addr, sizeof serv_addr) < 0;
n = send(sockfd, cmd, strlen(cmd), 0);
/* first call only gets banner */
bzero(buffer, sizeof buffer);
n = recv(sockfd, buffer, 255, 0);
printf("%d\n%s", n, buffer);
/* this time we get (part of) the definition */
bzero(buffer, sizeof buffer);
n = recv(sockfd, buffer, 255, 0);
printf("%d\n%s", n, buffer);
return 0;
}
.
- Follow-Ups:
- Re: multiple recv() calls required
- From: Alex Fraser
- Re: multiple recv() calls required
- From: David Schwartz
- Re: multiple recv() calls required
- Prev by Date: Re: Serial port, termios.h, and dtr control questions
- Next by Date: Re: multiple recv() calls required
- Previous by thread: Problem related with dhcp client packet format.
- Next by thread: Re: multiple recv() calls required
- Index(es):
Relevant Pages
|