home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3406 / soc.c < prev    next >
C/C++ Source or Header  |  1991-05-23  |  4KB  |  129 lines

  1.  
  2. /* Socket operations.
  3.  * int establish(int portnum)
  4.  * int get_connection(int s)
  5.  * int call_socket(char *hostname, int portnum)
  6.  * int read_data(int s, char *buf, int n)
  7.  * int write_data(int s, char *buf, int n)
  8.  */
  9.  
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <sys/param.h>
  14. #include <netinet/in.h>
  15. #include <netdb.h>
  16. #include <stdio.h>
  17. #include "soc.h"
  18.  
  19. extern int errno;
  20.  
  21. /* code to establish a socket; originally from bzs@bu-cs.bu.edu
  22.  */
  23.  
  24. int establish(portnum)
  25. u_short portnum;
  26. { char   myname[MAXHOSTNAMELEN+1];
  27.   int    s;
  28.   struct sockaddr_in sa;
  29.   struct hostent *hp;
  30.  
  31.   bzero((char *)&sa,sizeof(struct sockaddr_in));      /* clear our address */
  32.   gethostname(myname,MAXHOSTNAMELEN);            /* who are we? */
  33.   hp= gethostbyname(myname);                  /* get our address info */
  34.   if (hp == NULL)                             /* we don't exist !? */
  35.     return(-1);
  36.   sa.sin_family= hp->h_addrtype;              /* this is our host address */
  37.   sa.sin_port= htons(portnum);                /* this is our port number */
  38.   if ((s= socket(AF_INET,SOCK_STREAM,0)) < 0) /* create socket */
  39.     return(-1);
  40.   if (bind(s,(struct sockaddr *)&sa,sizeof sa) < 0) {
  41.     close(s);
  42.     return(-1);                               /* bind address to socket */
  43.     }
  44.   listen(s, 3);                               /* max # of queued connects */
  45.   return(s);
  46. }
  47.  
  48.  
  49. int get_connection(s)
  50. int s;                    /* socket created with establish() */
  51. { struct sockaddr_in isa; /* address of socket */
  52.   int i;                  /* size of address */
  53.   int t;                  /* socket of connection */
  54.  
  55.   i = sizeof(isa);                   /* find socket's address */
  56.   getsockname(s,(struct sockaddr *)&isa,&i);            /* for accept() */
  57.  
  58.   if ((t = accept(s,(struct sockaddr *)&isa,&i)) < 0) /* accept connection if ... */
  59.     return(-1);
  60.   return(t);
  61. }
  62.  
  63.  
  64. int call_socket(hostname, portnum)
  65. char *hostname;
  66. { struct sockaddr_in sa;
  67.   struct hostent     *hp;
  68.   int s;
  69.  
  70.   if ((hp= gethostbyname(hostname)) == NULL) { /* do we know the host's */
  71.     errno= ECONNREFUSED;                       /* address? */
  72.     return(-1);                                /* no */
  73. }
  74.  
  75.   bzero((char *)&sa,sizeof(sa));
  76.   bcopy(hp->h_addr,(char *)&sa.sin_addr,hp->h_length); /* set address */
  77.   sa.sin_family= hp->h_addrtype;
  78.   sa.sin_port= htons((u_short)portnum);
  79.  
  80.   if ((s= socket(hp->h_addrtype,SOCK_STREAM,0)) < 0)   /* get socket */
  81.     return(-1);
  82.   if (connect(s,(struct sockaddr *)&sa,sizeof sa) < 0)    /* connect */
  83.     return(-1);
  84.   return(s);
  85. }
  86.  
  87.  
  88. int read_data(s, buf, n)
  89. int  s;                /* connected socket */
  90. char *buf;             /* pointer to the buffer */
  91. int  n;                /* number of characters (bytes) we want */
  92. { int bcount,          /* counts bytes read */
  93.       br;              /* bytes read this pass */
  94.  
  95.   bcount= 0;
  96.   br= 0;
  97.   while (bcount < n) {             /* loop until full buffer */
  98.     if ((br= read(s, buf, n-bcount)) > 0) {
  99.       bcount += br;                /* increment byte counter */
  100.       buf += br;                   /* move buffer ptr for next read */
  101.     }
  102.     if (br <= 0)                    /* signal an error to the caller */
  103.       return(-1);
  104.   }
  105.   return(bcount);
  106. }
  107.  
  108.  
  109. int write_data(s, buf, n)
  110. int  s;                /* connected socket */
  111. char *buf;             /* pointer to the buffer */
  112. int  n;                /* number of characters (bytes) */
  113. { int bcount,          /* counts bytes written */
  114.       br;              /* bytes written this pass */
  115.  
  116.   bcount= 0;
  117.   br= 0;
  118.   while (bcount < n) {             /* loop until full buffer */
  119.     if ((br= write(s, buf, n-bcount)) > 0) {
  120.       bcount += br;                /* increment byte counter */
  121.       buf += br;                   /* move buffer ptr for next write */
  122.     }
  123.     if (br <= 0)                    /* signal an error to the caller */
  124.       return(-1);
  125.   }
  126.   return(bcount);
  127. }
  128.  
  129.