Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

SockClie.cc

Go to the documentation of this file.
00001 /*
00002 
00003     Socket client class
00004     Copyright (C) 1999-2001 Jussi Laako
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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             

Generated on Sun Oct 26 19:11:22 2003 for HASAS by doxygen 1.3.3