home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / WINWAIS / IR / SOCKETS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-14  |  2.4 KB  |  113 lines

  1. /* SOCKETS USING WINSOCK */
  2.  
  3. #define sockets_c
  4.  
  5. #include <dos.h>
  6.  
  7. #include "winsock.h"
  8. #include "sockets.h"
  9.  
  10. extern int recv_bufsize;
  11. extern int send_bufsize;
  12.  
  13. int tcpinit = FALSE;
  14.  
  15. SOCKET connect_to_server(host_name,port)
  16. char* host_name;
  17. int port;
  18. {
  19.   SOCKET fd = 0;
  20.   int lport, status;
  21.   long host;
  22.   
  23.   int sizeInt = 2;
  24.  
  25.   struct sockaddr_in name;
  26.   WSADATA wsaData;
  27.   
  28.   struct hostent far *host_info; 
  29.   struct in_addr far *addr_info;
  30.  
  31. //  if (port == 0)
  32. //    return LOCAL_DATABASE;
  33.  
  34.   if (!tcpinit)
  35.   {
  36.     if (0 != WSAStartup(0x0101, &wsaData))
  37.     {  
  38.       msgbox("WSASTARTUP Error: %d");
  39.       return 0;
  40.     }
  41.     tcpinit = TRUE;
  42.   }
  43.     
  44.   if (INVALID_SOCKET == (fd = socket(PF_INET, SOCK_STREAM, 0)))
  45.   {  
  46.     msgbox("SOCKET Error: %d");
  47.     return 0;
  48.   }
  49.  
  50.   getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char FAR *)&recv_bufsize, &sizeInt);
  51.   getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char FAR *)&send_bufsize, &sizeInt);
  52.  
  53.   memset((char *)&name, 0, sizeof(name)); 
  54.     
  55.   name.sin_family = AF_INET;
  56.   name.sin_addr.s_addr = INADDR_ANY;
  57.   name.sin_port = 0;
  58.     
  59.   if (SOCKET_ERROR == bind(fd, (struct sockaddr FAR *)&name, sizeof(name))) 
  60.   {  
  61.     msgbox("BIND Error: %d");
  62.     shutdown(fd, 2);
  63.     return 0;
  64.   }
  65.     
  66.   name.sin_addr.s_addr = inet_addr(host_name);
  67.     
  68.   if (name.sin_addr.s_addr == -1)
  69.   {
  70.     host_info = gethostbyname(host_name);
  71.     if (host_info == NULL)
  72.       return 0;
  73.     addr_info = (struct in_addr *)(host_info->h_addr_list[0]);
  74.     name.sin_addr.s_addr = addr_info->s_addr;
  75.   }
  76.  
  77.   name.sin_port = htons(port);
  78.       
  79.   if (SOCKET_ERROR == connect(fd, (struct sockaddr FAR *)&name, sizeof(name)))
  80.   {  
  81.     msgbox("CONNECT Error: %d");
  82.     return 0;
  83.   }
  84.   return fd;
  85. }
  86.  
  87. void close_connection_to_server(connection)
  88. SOCKET connection;
  89. {
  90.   int status;
  91. //  int optionInt;
  92.  
  93.   if (connection > 0) // && connection != LOCAL_DATABASE)
  94. //  {  
  95. //    optionInt = 1;
  96. //    if (SOCKET_ERROR == setsockopt(connection, 
  97. //                                   SOL_SOCKET, 
  98. //                                   SO_DONTLINGER, 
  99. //                                   (char FAR *)&optionInt, 
  100. //                                   sizeof(int)))
  101. //      msgbox("SETSOCKOPT error during close: %d");
  102.     closesocket(connection); 
  103. //  }
  104.   return;
  105. }
  106.  
  107. void close_all_connections()
  108. {
  109.   if (tcpinit)
  110.     WSACleanup();
  111.   tcpinit = FALSE;
  112. }
  113.