#include #include #include //--------------------------------------------------------------------------- /* ------------------------------------------------------------------------------- * Fills in structure SOCKADDR_IN. * Input arguments: * host - string containing either a host name, or IP address * serviceport - string containing eihter port number or service name * protocol - string containing name of the transport layer protocol * ex.: "tcp", "udp" * Output: * addr - structure containing IP and port for the internet process * ------------------------------------------------------------------------------- */ void set_address(char *host, char* serviceport, char* protocol, SOCKADDR_IN* addr) { SERVENT *pservice; HOSTENT *phost; short port; char *endport; memset(addr, 0, sizeof(*addr)); addr->sin_family = AF_INET; // working with IP address if( host != NULL ){ if( (addr->sin_addr.s_addr=inet_addr(host)) == INADDR_NONE ){ phost = gethostbyname(host); if( phost != NULL ) addr->sin_addr = *(IN_ADDR*)phost->h_addr; else{ cerr<<"Error: cannot resolve '"<sin_addr.s_addr = htonl(INADDR_ANY); } // working with port number if( serviceport != NULL ){ port = strtol(serviceport, &endport, 0); if( *endport == '\0' ) addr->sin_port = htons(port); else{ pservice = getservbyname(serviceport, protocol); if( pservice != NULL ) addr->sin_port = pservice->s_port; else{ cerr<<"Error: unknown service '"<sin_port = 0; } } // ------------------------------------------------------------------------- void init(void) { WORD ver; /* socket version */ WSADATA wsaData; /* detailed info about WSA */ /* init Windows Sockets DLL */ ver = MAKEWORD(2, 0); /* version 2.0 */ if( WSAStartup(ver, &wsaData) ){ cerr<<"Error: cannot initialise WinSock DLL\n"; exit(1); } /* check the WinSock info */ if( (LOBYTE(wsaData.wVersion)!=2) || (HIBYTE(wsaData.wVersion)!=0) ){ cerr<<"Error: cannot find a usable WinSock DLL\n"; exit(2); } } // ------------------------------------------------------------------------- void print_address(SOCKADDR_IN addr) { cout<<"Family: "<