Archived Forum Post

Index of archived forum posts

Question:

Linux Connect to remote host

Aug 14 '13 at 17:33

I'm having a strange problem on a Linux platform (bit like the one reported here)

I've been using Chilkat for quite some time now and this is the first time I've come across anything I can't solve myself. I'm using code that's pretty much straight out of the examples (to preclude any typing errors on my part) but I can't seem to get this to work on a Linux platform although (uncharacteristically) it works perfectly on Windows.

I'm writing a test harness for an Android App. I need to connect to the app from a remote device over WiFi and exchange some data. However, I'm finding I get a positive response from "connect" (CkSocket) even when the remote device is turned off!! I've got the latest libraries (as far as I know) and I've got all the error logging on:

Here's the trace:

Socket MessageChilkatLog:
  Connect_Socket:
    DllDate: Dec 17 2012
    UnlockPrefix: MREPWASocket
    Username: paul-kde-dev2:paul
    Architecture: Little Endian; 64-bit
    Language: Linux C/C++
    VerboseLogging: 1
    objectId: 1
    hostname: 192.168.168.106
    port: 12347
    ssl: 0
    maxWaitMs: 20000
    ConnectTimeoutMs_1: 20000
    calling ConnectSocket2
    IPV6 enabled connect with NO heartbeat.
    This is an IPV4 numeric address...
    AddrInfoList:
      AddrInfo:
        ai_flags: 1028
        ai_family: 2
        ai_socktype: 1
        ai_protocol: 6
        ai_addrlen: 16
        ai_canonname: (NULL)
      --AddrInfo
    --AddrInfoList
    Connect using IPV4.
    ipAddress1: 192.168.168.106
    Info: Socket operation in progress..
    myIP_3: 192.168.168.201
    myPort_3: 32974
    connect successful (2)
    socketOptions:
      SO_SNDBUF: 16384
      SO_RCVBUF: 87380
      TCP_NODELAY: 0
    --socketOptions
    Success.
  --Connect_Socket
--ChilkatLog

The IP addresses are correct (and I can't ping 192.168.168.106 when the tablet is turned off so I don't think it's a duplicate address.

I've checked the documentation connect(2) and it looks to me that it should return ECONNREFUSED or ENETUNREACH.

However, as soon as I do a receive I get:

Recieving data
Receive length failed
Socket MessageChilkatLog:
  ReceiveBytesN:
    DllDate: Dec 17 2012
    UnlockPrefix: MREPWASocket
    Username: paul-kde-dev2:paul
    Architecture: Little Endian; 64-bit
    Language: Linux C/C++
    VerboseLogging: 1
    fd: 0x3
    objectId: 1
    NumBytesToReceive: 1
    BufferedInSize: 0
    errno: 113
    osErrorMessage: No route to host
    numBytesRequested: 1
    Failed to receive data on the TCP socket
    Failed status...
    TimedOut: 0
    AbortedByApp: 0
    FatalError: 1
    ConnectionClosed: 0
    Failed.
  --ReceiveBytesN
--ChilkatLog

It looks to me like this connection is behaving like a UDP rather than a TCP socket but I can't find anything in the C++ component method/parameter list that changes this behaviour. I haven't done a Wireshark trace on it yet though to confirm the latter; I'm running it in a VM and w/shark seems to have a problem with the adapter.

For the record though, here's the code:

// address = 192.168.168.106, port = 12347  
void foo (char * address, int port)
{
     CkSocket   socket ;
     bool       success ;
     int        FrameArrayCount = 0 ;

    /* Load the Chilkat component */
    /******************************/
    success = socket.UnlockComponent(".........MY UNLOCK CODE..............");
    socket.put_VerboseLogging(true);
    socket.put_KeepSessionLog(true);

    if (success == true)
    {
        cout << "Comms Library Loaded" << endl ;

        long maxWaitMillisec;
        maxWaitMillisec = 20000;
        CkString       Logstr ;

        while (true)
        {
            Logstr.clear() ;

            cout  << "Calling " << address << " " << port << endl ;
            success = socket.Connect(address, port, false, maxWaitMillisec);

            if (success)
            {
                cout  << "******************************************************" << endl ;
                cout  << "got a connection" << endl ;
                //  Set maximum timeouts for reading an writing (in millisec)
                socket.put_MaxReadIdleMs(60000);
                socket.put_MaxSendIdleMs(60000);
                socket.put_ReceivePacketSize((long) 1);

                cout << "Socket Message" << socket.lastErrorText()  << endl ;
                //---- processloop (socket) ;  // Where I would do some processing

                socket.get_SessionLog(Logstr);
                socket.Close(5000);

                // cout << "++++++++++++++++++++++++++Session Log++++++++++++++++++++++++++" << endl ;
                cout << Logstr.getString() << endl << endl << endl ;
            }
            else
            {
                cout << "++++++++++++++++++++++++++ Connection Failed ++++++++++++++++++++++++++" << endl ;
                socket.get_SessionLog(Logstr);
                socket.Close(5000);

                cout << Logstr.getString() << endl << endl << endl ;
                sleep(1000) ;
            }
        }
    }
    else
    {
       cout << "Failed to Load Communications Libraries" << endl ;
    }
}

Anyone got any ideas?


Answer

It certainly is a mystery. Information about the socket family, type, and protocol are included in the LastErrorText, so it's certainly not trying to use UDP:

    AddrInfoList:
      AddrInfo:
        ai_flags: 1028
        ai_family: 2    (This is AF_INET)
        ai_socktype: 1  (This is SOCK_STREAM)
        ai_protocol: 6  (This is IPPROTO_TCP)
        ai_addrlen: 16
        ai_canonname: (NULL)
      --AddrInfo
    --AddrInfoList
    
Unfortunately, I don't have any good answer for it..


Answer

After a quick Google search, I found this: http://stackoverflow.com/questions/7905505/android-socket-java-net-socketexception-no-route-to-host