connectsock.c
上传用户:wei_4586
上传日期:2008-05-28
资源大小:18k
文件大小:2k
- /* connectsock.c - connectsock */
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <string.h>
- #include <stdlib.h>
- #ifndef INADDR_NONE
- #define INADDR_NONE 0xffffffff
- #endif /* INADDR_NONE */
- extern int errno;
- int errexit(const char *format, ...);
- /*------------------------------------------------------------------------
- * connectsock - allocate & connect a socket using TCP or UDP
- *------------------------------------------------------------------------
- */
- 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 address */
- 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((unsigned short)atoi(service))) == 0)
- errexit("can't get "%s" service entryn", service);
- /* 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" host entryn", host);
- /* Map transport protocol name to protocol number */
- if ( (ppe = getprotobyname(transport)) == 0)
- errexit("can't get "%s" protocol entryn", transport);
- /* Use 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: %sn", strerror(errno));
- /* Connect the socket */
- if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
- errexit("can't connect to %s.%s: %sn", host, service,
- strerror(errno));
- return s;
- }