/*
-----------------------------------------------------------
MakeConnection
Function allocates a socket and estabishes a connection
with remote host. Default port number 80.
Input : WWW server name (with port number, if it is not 80)
Output : file descriptor on success
-1 on error
-----------------------------------------------------------
*/
int MakeConnection(unsigned char* ServerName){
int s;
struct sockaddr_in ssin;
struct hostent* hp;
int PortNum;
unsigned char strHlp[STRNGLEN], *pch;
/* use default port number - 80 or specific number from the
server name */
strcpy(strHlp,ServerName);
pch = strchr(strHlp,':');
if(pch==NULL){
PortNum = 80;
}else{
pch[0] = '\0';
pch++;
PortNum = atoi(pch);
if(PortNum==0){
PortNum = 80;
}
}
/* get host by name - resolve host name into IP address */
if( (hp=gethostbyname(strHlp)) == NULL )
{
return -1;
}
bzero(&ssin, sizeof(ssin));
bcopy(hp->h_addr, &ssin.sin_addr, hp->h_length);
ssin.sin_family = hp->h_addrtype;
ssin.sin_port = htons(PortNum);
/* allocate a socket */
if((s=socket(AF_INET, SOCK_STREAM, 0))==-1)
{
return -1;
}
/* make a connection */
if(connect(s, &ssin, sizeof(ssin), 0)==-1){
return -1;
}
return s; /* socket descriptor */
}