*BSD News Article 9870


Return to BSD News archive

Received: by minnie.vk1xwt.ampr.org with NNTP
	id AA6687 ; Tue, 12 Jan 93 05:21:45 EST
Xref: sserve comp.unix.bsd:9927 comp.unix.admin:8920 comp.unix.misc:6227 comp.sys.sgi:26143 comp.sys.sgi.apps:61 comp.sys.sgi.misc:268 comp.unix.internals:5518
Path: sserve!manuel.anu.edu.au!munnari.oz.au!sgiblab!spool.mu.edu!yale.edu!ira.uka.de!fhg!igd!mike
From: mike@igd.fhg.de (Mike Sokolewizc (Gast))
Newsgroups: comp.unix.bsd,comp.unix.admin,comp.unix.misc,comp.sys.sgi,comp.sys.sgi.apps,comp.sys.sgi.misc,comp.unix.internals
Subject: Re: Problem to transfer large data via socket communication - Need Help
Message-ID: <1829@igd.fhg.de>
Date: 14 Jan 93 18:23:14 GMT
References: <1993Jan8.071612.13716@news.uni-stuttgart.de>
Sender: news@igd.fhg.de
Followup-To: comp.unix.bsd
Organization: Haus der Graphischen Datenverarbeitung, 6100 Darmstadt
Lines: 52

>  Hey,
>
>  we develope a program with socket communication. We have large
>  datas to transfer. So we use the following routines
>  to send data from socket to socket (stream):
>
>        write(socket,(char*)data,buffer)
>        read(socket,(char*)data,buffer)
>
>  But now we've problems sending/receiving large data for
>  example 50*50*50 matrix of floats or larger. Is there any
>  transfer-bufferlimitation by using streamsockets, that we can
>  manipulate ? Exists there no more comfortable possibility to
>  transfer the data ? Please mail me your informations
>
>  Thanks for your great efforts
>
>     Oliver


I'm not exactly sure what problems you're having, but the answer might be something rather
simple (simple once you know about it, that is; it took me two days of debugging to figure
this out).

I always assumed that doing a read() on a socket would wait and fill the given buffer as long as
data was coming down the 'pipeline'; but that's not the case.  When you execute the read(),
the system gives you only exactly what's waiting at that socket at that instant, even if more
data is coming.  Therefore, you have to nest the read() within a loop and keep reading until
you get an error or the expected number of bytes:

  num_read = 0;
  do
    {
      l = read( socket, &buffer[num_read], BUF_SIZE - num_read );
      num_read += l;
    }
  while ( (num_read < BUF_SIZE) && (l > 0) );

Hope that helps.

--mike


+------------------------+----------------------------------------------------+
| Michael A. Sokolewicz  |      ZENTRUM FUER GRAPHISCHE DATENVERARBEITUNG     |
|    mike@igd.fhg.de     |                  Wilhelminenstr. 7                 |
| tel: 49 6151 155 243   |              W6100 Darmstadt, GERMANY              |
+------------------------+----------------------------------------------------+
|      "It's not that power corrupts, but rather that power attracts the      |
|                               corruptable."                                 |
|                                           -Frank Herbert, Chapterhouse Dune |
+-----------------------------------------------------------------------------+