00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <errno.h>
00027 #include <netdb.h>
00028 #include <sys/types.h>
00029 #include <sys/socket.h>
00030 #include <sys/un.h>
00031 #include <netinet/in.h>
00032 #include <arpa/inet.h>
00033
00034 #include "Config.h"
00035 #include "SockClie.hh"
00036
00037
00038 #ifndef UNIX_PATH_MAX
00039 #define UNIX_PATH_MAX 108
00040 #endif
00041
00042
00043 clSockClie::clSockClie()
00044 {
00045 iErrno = 0;
00046 }
00047
00048
00049 clSockClie::~clSockClie()
00050 {
00051 }
00052
00053
00054 int clSockClie::Connect(const char *cpHostName, const char *cpHostAddr,
00055 int iHostPort)
00056 {
00057 int iServH;
00058 struct sockaddr_in sClientAddr;
00059 struct in_addr *spInAddr;
00060 struct hostent *spHostEntry;
00061
00062 memset(&sClientAddr, 0x00, sizeof(sClientAddr));
00063 iServH = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
00064 if (iServH < 0)
00065 {
00066 iErrno = errno;
00067 return -1;
00068 }
00069 if (cpHostAddr != NULL)
00070 {
00071 sClientAddr.sin_family = AF_INET;
00072 sClientAddr.sin_port = htons(iHostPort);
00073 if (inet_aton(cpHostAddr, &sClientAddr.sin_addr) == 0)
00074 {
00075 return -1;
00076 }
00077 }
00078 else
00079 {
00080 sClientAddr.sin_family = AF_INET;
00081 sClientAddr.sin_port = htons(iHostPort);
00082 #ifndef __QNX__
00083 spHostEntry = gethostbyname2(cpHostName, AF_INET);
00084 #else
00085 spHostEntry = gethostbyname(cpHostName);
00086 #endif
00087 if (spHostEntry != NULL)
00088 {
00089 spInAddr = (struct in_addr *) spHostEntry->h_addr_list[0];
00090 if (spInAddr != NULL)
00091 {
00092 sClientAddr.sin_addr.s_addr = spInAddr->s_addr;
00093 }
00094 else
00095 {
00096 return -1;
00097 }
00098 }
00099 else
00100 {
00101 return -1;
00102 }
00103 }
00104 if (connect(iServH, (struct sockaddr *) &sClientAddr,
00105 sizeof(sClientAddr)) < 0)
00106 {
00107 iErrno = errno;
00108 return -1;
00109 }
00110 return iServH;
00111 }
00112
00113
00114 int clSockClie::Connect(const char *cpSockName)
00115 {
00116 #ifndef __QNX__
00117 int iServH;
00118 struct sockaddr_un sClientAddr;
00119
00120 memset(&sClientAddr, 0x00, sizeof(sClientAddr));
00121 iServH = socket(PF_UNIX, SOCK_STREAM, 0);
00122 if (iServH < 0)
00123 {
00124 iErrno = errno;
00125 return -1;
00126 }
00127 sClientAddr.sun_family = AF_UNIX;
00128 if ((strlen(GLOBAL_SOCKET_PATH) + strlen(cpSockName)) > UNIX_PATH_MAX)
00129 {
00130 iErrno = 0;
00131 return -1;
00132 }
00133 sprintf(sClientAddr.sun_path, "%s/%s", GLOBAL_SOCKET_PATH, cpSockName);
00134 if (connect(iServH, (struct sockaddr *) &sClientAddr,
00135 sizeof(sClientAddr)) < 0)
00136 {
00137 iErrno = errno;
00138 return -1;
00139 }
00140 return iServH;
00141 #else
00142 return Connect("localhost", NULL, atoi(cpSockName));
00143 #endif
00144 }
00145