home *** CD-ROM | disk | FTP | other *** search
- /* SOCKETS USING WINSOCK */
-
- #define sockets_c
-
- #include <dos.h>
-
- #include "winsock.h"
- #include "sockets.h"
-
- extern int recv_bufsize;
- extern int send_bufsize;
-
- int tcpinit = FALSE;
-
- SOCKET connect_to_server(host_name,port)
- char* host_name;
- int port;
- {
- SOCKET fd = 0;
- int lport, status;
- long host;
-
- int sizeInt = 2;
-
- struct sockaddr_in name;
- WSADATA wsaData;
-
- struct hostent far *host_info;
- struct in_addr far *addr_info;
-
- // if (port == 0)
- // return LOCAL_DATABASE;
-
- if (!tcpinit)
- {
- if (0 != WSAStartup(0x0101, &wsaData))
- {
- msgbox("WSASTARTUP Error: %d");
- return 0;
- }
- tcpinit = TRUE;
- }
-
- if (INVALID_SOCKET == (fd = socket(PF_INET, SOCK_STREAM, 0)))
- {
- msgbox("SOCKET Error: %d");
- return 0;
- }
-
- getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char FAR *)&recv_bufsize, &sizeInt);
- getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char FAR *)&send_bufsize, &sizeInt);
-
- memset((char *)&name, 0, sizeof(name));
-
- name.sin_family = AF_INET;
- name.sin_addr.s_addr = INADDR_ANY;
- name.sin_port = 0;
-
- if (SOCKET_ERROR == bind(fd, (struct sockaddr FAR *)&name, sizeof(name)))
- {
- msgbox("BIND Error: %d");
- shutdown(fd, 2);
- return 0;
- }
-
- name.sin_addr.s_addr = inet_addr(host_name);
-
- if (name.sin_addr.s_addr == -1)
- {
- host_info = gethostbyname(host_name);
- if (host_info == NULL)
- return 0;
- addr_info = (struct in_addr *)(host_info->h_addr_list[0]);
- name.sin_addr.s_addr = addr_info->s_addr;
- }
-
- name.sin_port = htons(port);
-
- if (SOCKET_ERROR == connect(fd, (struct sockaddr FAR *)&name, sizeof(name)))
- {
- msgbox("CONNECT Error: %d");
- return 0;
- }
- return fd;
- }
-
- void close_connection_to_server(connection)
- SOCKET connection;
- {
- int status;
- // int optionInt;
-
- if (connection > 0) // && connection != LOCAL_DATABASE)
- // {
- // optionInt = 1;
- // if (SOCKET_ERROR == setsockopt(connection,
- // SOL_SOCKET,
- // SO_DONTLINGER,
- // (char FAR *)&optionInt,
- // sizeof(int)))
- // msgbox("SETSOCKOPT error during close: %d");
- closesocket(connection);
- // }
- return;
- }
-
- void close_all_connections()
- {
- if (tcpinit)
- WSACleanup();
- tcpinit = FALSE;
- }
-