home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / x / xhearts.zip / SOCKIO.C < prev    next >
C/C++ Source or Header  |  1992-01-07  |  838b  |  43 lines

  1. #include <stdio.h>
  2. /*
  3.  * Routines to send and receive on sockets.  Four bytes of length are
  4.  * sent, followed by the null terminated string.
  5.  *
  6.  */
  7. read_socket(s, buf)
  8. int    s;            /* socket to talk on */
  9. char    *buf;            /* string to send */
  10. {
  11.     int nbytes;
  12.     int    partial, this;
  13.  
  14.     if (read(s, (char *) &nbytes, sizeof(int)) != sizeof (int))
  15.       return 0;
  16.     nbytes = ntohl(nbytes);
  17.     
  18.     partial = 0;
  19.     while (partial < nbytes) {
  20.       this = read(s, buf+partial, nbytes);
  21.       if (!this)
  22.         return 0;
  23.       partial = partial + this;
  24.     }
  25.     if (partial != nbytes)
  26.       return 0;
  27.     else
  28.       return nbytes;
  29. }
  30.  
  31.  
  32. write_socket(s, buf)
  33. int    s;            /* socket to talk on */
  34. char    *buf;            /* string to read on */
  35. {
  36.     int nbytes, netnbytes;
  37.  
  38.     nbytes = strlen(buf) + 1;
  39.     netnbytes = htonl(nbytes);
  40.     (void) write(s, (char *) &netnbytes, sizeof(int));
  41.     (void) write(s, buf, nbytes);
  42. }
  43.