home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / games / volume15 / xtb / part02 / dgram.c < prev    next >
C/C++ Source or Header  |  1993-01-27  |  1KB  |  99 lines

  1. /*
  2.  * Comms.
  3.  *
  4.  * $Header: /morpork/home/bmh/xtest2/RCS/dgram.c,v 1.10 92/10/19 15:34:27 bmh Exp Locker: bmh $
  5.  *
  6.  * Bernard Hatt
  7.  * Camtec Electronics (Ericsson), Leicester, England, LE1 4SA
  8.  * bmh@terminus.ericsson.se
  9.  *
  10.  */
  11.  
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <netdb.h>
  16. #include <stdio.h>
  17.  
  18. #include "defs.h"
  19. #include "comms.h"
  20.  
  21. #define STREAM_TYPE    SOCK_DGRAM
  22.  
  23. struct hostent *gethostbyname();
  24.  
  25. int
  26. openread(port)
  27. int port;
  28. {
  29.     int sd;
  30.     struct sockaddr_in name;
  31.  
  32.     sd=socket(AF_INET,STREAM_TYPE,0);
  33.     if(sd==-1)
  34.     {
  35.         perror("openread()/socket()");
  36.         return(-1);
  37.     }
  38.      name.sin_family=AF_INET;
  39.     name.sin_addr.s_addr=INADDR_ANY;
  40.     name.sin_port=htons(port);
  41.  
  42.     if(bind(sd,&name,sizeof(name)))
  43.     {
  44.         perror("openread()/bind()");
  45.         return(-1);
  46.     }
  47.     return(sd);
  48. }
  49.  
  50. int
  51. readdata(sd,data)
  52. int sd;
  53. DATA *data;
  54. {
  55.     return(read(sd,data,sizeof(DATA)));
  56. }
  57.  
  58. int
  59. opensend(host,port)
  60. char *host;
  61. int port;
  62. {
  63.     int ret,sd;
  64.     struct sockaddr_in name;
  65.     struct hostent *hent;
  66.  
  67.     sd=socket(AF_INET,STREAM_TYPE,0);
  68.     if(sd==-1)
  69.     {
  70.         perror("opensend()/socket()");
  71.         return(-1);
  72.     }
  73.  
  74.     hent=gethostbyname(host);
  75.     if(hent==NULL)
  76.         return(-1);
  77.  
  78.     bcopy(hent->h_addr,&name.sin_addr,hent->h_length);
  79.     name.sin_family=AF_INET;
  80.     name.sin_port=htons(port);
  81.  
  82.         /* connect port */
  83.     ret=connect(sd,&name,sizeof(name));
  84.     if(ret==-1)
  85.     {
  86.         perror("opensend()/connect()");
  87.         return(-1);
  88.     }
  89.     return(sd);
  90. }
  91.  
  92. int
  93. senddata(sd,data)
  94. int sd;
  95. DATA *data;
  96. {
  97.     return(write(sd,data,sizeof(DATA)));
  98. }
  99.