The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #6 (permalink)  
Old 01-16-2002
thinker130 thinker130 is offline
Registered User
  
 

Join Date: Jan 2002
Posts: 6
the source files(the third part)

I have forgotten to include some functions needed in file tel_cli.c, please append the belowed source code to the end of file tel_cli.c.


/*connectsock - allocate & connect a socket using TCP or TCP*/
int connectsock(const char *host,const char *service, const char *transport)
/* arguments:
* host --- name of host to which connection is desired
* service -- service associated with the desired port
* transport --- name of transport protocol to use ("tcp" or "udp")
*/
{
struct hostent *phe; /* pointer to host information entry */
struct servent *pse; /* pointer to service information entry */
struct protoent *ppe; /* pointer to protocol information entry */
struct sockaddr_in sin; /* an Internet endpoint addredd */
int s,type; /* socket descriptor and socket type */

memset(&sin,0,sizeof(sin));
sin.sin_family=AF_INET;

/* Map service name to port number */
if (pse = getservbyname(service,transport))
sin.sin_port = pse->s_port;
else if ((sin.sin_port = htons((u_short)atoi(service))) == 0 )
{
//errexit("can't get \"%s\" service entry\n",service);
printf("can't get \"%s\" service entry\n",service);
return 0;
}

/* Map host name to IP address,allowing for dotted decimal */
if (phe = gethostbyname(host))
memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
{
//errexit("can't get \"%s\" protocol entry\n",transport);
printf("can't get \"%s\" protocol entry\n",transport);
return 0;
}

/* Map transport protocol name to protocol number */
if ((ppe = getprotobyname(transport))==0)
{
//errexit("can't get \"%s\" protocol entry\n",transport);
printf("can't get \"%s\" protocol entry\n",transport);
return 0;
}

/* User protocol to choose a socket type */
if (strcmp(transport,"udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;

/*Allocate a socket */
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
{
//errexit("can't create socket:%s\n", strerror(errno));
printf("can't create socket:%s\n", strerror(errno));
return 0;
}
/* connect the socket */
if (connect (s,(struct sockaddr *)&sin,sizeof(sin)) < 0)
{
//errexit("can't connect to %s.%s: %s\n",host,service,strerror(errno));
printf("can't connect to %s.%s: %s\n",host,service,strerror(errno));
return 0;
}
return s;

}


int connectTCP(const char *host, const char *service)
/* arguments:
* host --- name of host to which connection is desired
* service --- service associated with the desired port
*/
{
return connectsock(host,service,"tcp");
}




/* errexit --- print an error mesage and exit */

/*VARARGS1*/

int errexit(const char *format,...)
{
va_list args;
va_start(args,format);
vfprintf(stderr,format,args);
va_end(args);
exit(1);
}