Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 16 Nov 1996 15:13:04 -0500 (EST)
From:      "Marc G. Fournier" <scrappy@ki.net>
To:        hackers@freebsd.org
Subject:   Hate to ask more about sockets...but...
Message-ID:  <Pine.NEB.3.95.961116144609.7019A-100000@quagmire.ki.net>

next in thread | raw e-mail | index | archive | help


After the ... discussion(?) I started with my last question, I kind of hate
to ask another one...but what the hell, here goes...:)

I've taken most of the suggestions that were given at the beginning of the
last discussion and improved (I think?) my code accordingly.  But I'm still
getting what seems to be a strange result, possibly because I've missed
something...again.

First off, the server opens a .jpg file, mmap's it, and sends it out through
the socket:

-----------------
    if( (fd = open(TESTFILE, O_RDONLY)) != -1) {
      fstat(fd, &fi); 
      fp = mmap(NULL, fi.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
      if(fp == (caddr_t) -1) {
        fprintf(stderr, "MMAP() failed\n");
        exit(1);
      }
      close(fd);
      while(cnt-- != 0) { /* set to 1 for initial test */
        start = time(0);
        nwritten = write(newsockfd, START, strlen(START));
        if(nwritten < 0) {
          fprintf(stderr, "failed to write to socket: [%d bytes]\n", nwritten);
          exit(1);
        } 
 
        nwritten = writen(newsockfd, fp, fi.st_size);
        if(nwritten < 0) {
          fprintf(stderr, "failed to write data to socket\n");
          exit(1);
        } 
      }
    } 
----------------- 

On the client side, it reads the buffer until there is no data left:

-----------------
  char ptr[8192];

  while(1) {
    n = read(sockfd, ptr, sizeof(ptr) - 1);
    if(n < 0) {
      fprintf(stderr, "error reading socket\n");
      exit(0);
    } else if(n == 0) break;
    fprintf(stderr, "read %d(%d) bytes\n", bytes, n);
    write(STDOUT_FILENO, ptr, n);
  }
-----------------

The 'fprintf' in client produces:

read 46(46) bytes
read 1486(1440) bytes
read 9677(8191) bytes
read 17868(8191) bytes
read 26059(8191) bytes
read 27340(1281) bytes

And the server reports having written:

0= START: wrote [46] to socket
WRITEN: bytes left to write == 27294
WRITEN: wrote 27294 bytes

And the file being read in is:
-rw-r--r--  1 scrappy  wheel  27294 Nov  9 00:23 ../public_html/POST4.JPG

Great, this works...sort of.  For some reason, the program reports that the
read completed correctly, with the exact number of bytes read as I wrote to
the socket, but I'm missing several lines out of the graphic when I view it.
(check out http://www.ki.net/~scrappy/test.html, if you want to see what I
mean by "missing several lines")

If I change 'cnt' in the server to be 2 instead of 1, the graphic will come
out completely...as if, for some reason, the last read didn't get written to
the screen if it only 'scans' the file once.

The other thing I found was weird was that if I changed the 'break' in 
the client side to a 'continue', so that it continued to scan the input
socket for data (ie. await the next image to be send down the socket), I 
lost even more data from the image.

I don't know if this makes any sense, or if there is enough data, but from
everything I've been able to read, this *should* work...shouldn't it?

Marc G. Fournier                                  scrappy@ki.net
Systems Administrator @ ki.net               scrappy@freebsd.org




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.NEB.3.95.961116144609.7019A-100000>