multiple recv() calls required



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;
}
.



Relevant Pages

  • Re: 2 question or ask for the links
    ... > Now starnge situation on the one server all goes ok, ... > I send command to continuation some process on the this new file. ... > Fany thing is that order my log's is bad becouse info about reading is ... > early than info about buffer, as you see code printing buffer is early ...
    (comp.lang.tcl)
  • [NEWS] Doomsday Multiple Vulnerabilities
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... for copying the message from the packet to the new allocated buffer called ... If the message is directed to the server it's displayed in the ... // The first command. ...
    (Securiteam)
  • Re: 2 question or ask for the links
    ... Now starnge situation on the one server all goes ok, but when I moved this code without any changes on the another server this code started behave in inexplicable way for me. ... I send command to continuation some process on the this new file. ... I observe what is happen during procesing file by reading log's file. ... Fany thing is that order my log's is bad becouse info about reading is early than info about buffer, as you see code printing buffer is early that procedure READ. ...
    (comp.lang.tcl)
  • Receive binary file through recv
    ... I am trying to send a binary file from the client to the server. ... in a buffer,, and on the server side, I wonder when I ... Directly recv to file: ...
    (comp.unix.programmer)
  • Re: multiple recv() calls required
    ... I connectand the server sends the banner, but I don't recv() it. ... I send the DEFINE command. ... somewhat at odds with my idea of TCP as being about sending and receiving ...
    (comp.unix.programmer)