You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by Rahul Jain <ra...@apache.org> on 2000/03/15 03:38:11 UTC

'connection refused' only on RH Linux/FreeBSD. Works under AIX/Solaris/HPUX...

Hi,

I am stuck with this problem and would appreciate if anyone had
any suggestions / ideas / theory explaining the behviour that
I describe below.

The problem has nothing to do with XML, but I wish to add
capability to fetch HTTP URL's to Xerces-C. Hence, I am posting
on this mailing list, with the hope that someone with more
experience under Linux will be able to help me.

I wrote a simple C++ program which creates a socket, connects
to the HTTP server specified (at the given/default port), and
fetches the content of the resource specified in the URL (source
code appended below).

THE PROBLEM
-----------

This program works under Solaris 2.6 (CC), AIX (xlC),
HPUX (CC and aCC) as expected. However, when I try running
it under RH 6.0/6.1 or FreeBSD (locus.apache.org) it fails
to 'connect()' and gives a 'connection refused' error message.

I have looked at the source code for number of open source
products and I don't seem to be doing things any differently.
My attempt is very straightforward.

If anyone could please, tell me what I need to do to get this
code to work under RH 6.0/6.1, I'll be grateful.

Thanks,

rahul


Attached below:        The code.
                       Save in file getURL.cpp

To compile:            g++ -o getURL getURL.cpp
To run:                ./getURL www.apache.org
result:                should see the content of the URL scroll by
                       in the shell.

8<----------------------------------------------------------------------

#include <iostream.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>


#define BUFLENT          512


int getServerName(char* httpURL, char*& serverName, int& portNumber);


int getServerName(char* httpURL, char*& serverName, int& portNumber)
{
    int     retval = 1;
    int     lent = 0;
    int     lent1 = 0;
    char*   colonPos = 0;
    char*   slashPosn = 0;
    char*   pnumStr = 0;

    if ((httpURL == NULL) || (*httpURL == 0x00))
    {
        return retval;
    }
    colonPos = strchr(httpURL, ':');
    if (colonPos == 0)
    {
        slashPosn = strchr(httpURL, '/');
        if (slashPosn == 0)
        {
            lent = strlen(httpURL);
            serverName = new char[lent + 1];
            strcpy(serverName, httpURL);
        }
        else
        {
            lent1 = slashPosn - httpURL;
            serverName = new char[lent1 + 1];
            strncpy(serverName, httpURL, lent1);
            serverName[lent1] = 0;
        }
        portNumber = 80;
    }
    else
    {
        int lent = colonPos - httpURL;
        serverName = new char[lent + 2];
        strncpy(serverName, httpURL, lent);
        serverName[lent] = 0;
        slashPosn = strchr(colonPos, '/');
        if (slashPosn == 0)
            portNumber = atoi(colonPos + 1);
        else
        {
            lent1 = slashPosn - colonPos - 1;
            char*  pnumStr = new char[lent1 + 1];
            strncpy(pnumStr, colonPos + 1, lent1);
            pnumStr[lent1] = 0x00;
            portNumber = atoi(pnumStr);
            delete pnumStr;
        }
    }

    retval = 0;
    return retval;
}


int main(int argc, char* argv[])
{
    char       obuf[BUFLENT + 1];
    char       ibuf[BUFLENT + 1];
    int        lent = 0;
    struct sockaddr_in  sa;
    struct hostent*     hp;


    if (argc < 2)
    {
        cout << "Usage: " << argv[0] << " http_URL" << endl;
        exit(-1);
    }
    char*  url = argv[1];
    char*  server = NULL;
    int    pno = 0;
    int    retval = getServerName(url, server, pno);
    if (retval != 0)
    {
        cout << "Error extracting hostname from URL: " << url << endl;
        exit(-1);
    }

    cout << "Server name: " << server << endl;
    cout << "Port Number: " << pno << endl;

    if ((hp = gethostbyname(server)) == NULL)
    {
        cout << "Could not determine the hostaddress." << endl;
        exit(-1);
    }

    bcopy((char *) hp->h_addr, (char *) &sa.sin_addr, hp->h_length);
    sa.sin_family = hp->h_addrtype;
    sa.sin_port = pno;

    int  s = 0;
    s = socket(hp->h_addrtype, SOCK_STREAM, 0);
    if (s < 0)
    {
        cout << "Could not create a socket." << endl;
        exit(-1);
    }

    if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
    {
        cout <<
            "Could not connect the socket to the destination address/port."
            << endl;
        perror("");
        exit(-1);
    }
    
    // Now you can simply read and write from/to the socket.

    sprintf(obuf, "GET http://%s\n\n", url);
    if (write(s, (void *) obuf, strlen(obuf)) != strlen(obuf))
    {
        cout << "Could not write the request to the socket." << endl;
        exit(-1);
    }

    while ((lent = read(s, ibuf, BUFLENT)) > 0)
    {
        write(1, ibuf, lent);
    }

    close(s);
    delete server;
}

8<-----------------------------------------------------------------------------