1

I don't know if this is in the way I'm handling Android, or a problem with my native code, or both.

I am setting up a udp socket in C++ (wrappers generated by swig):

udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (udpSocket < 0)
        {
            pthread_mutex_unlock(&csOpenCloseUdp);
            throw IOException("Failed to open socket");
        }


        char bAllowMultiple = true;
        setsockopt(udpSocket, SOL_SOCKET, SO_REUSEADDR, &bAllowMultiple, sizeof(bAllowMultiple));
        setsockopt(udpSocket, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&hopLimit, sizeof(hopLimit));
        setsockopt(udpSocket, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localAddr, sizeof(localAddr));

        // Set to non-blocking mode
        unsigned long bMode = 1;
        ioctl( udpSocket, FIONBIO, &bMode );

        // Create the local endpoint
        sockaddr_in localEndPoint;
        localEndPoint.sin_family = AF_INET;
        localEndPoint.sin_addr.s_addr = localAddr.s_addr;
        localEndPoint.sin_port = groupEndPoint.sin_port;

        // Bind the socket to the port
        int r = bind(udpSocket, (sockaddr*)&localEndPoint, sizeof(localEndPoint));
        if (r == SOCKET_ERROR)
        {
            //LeaveCriticalSection(&csOpenCloseUdp);
            pthread_mutex_unlock(&csOpenCloseUdp);
            close();
            throw IOException("Failed to bind port");
        }


        // Join the multicast group
        struct ip_mreq imr;
        imr.imr_multiaddr = groupEndPoint.sin_addr;
        imr.imr_interface.s_addr = localAddr.s_addr;
        setsockopt(udpSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&imr, sizeof(imr));

The socket is not throwing any exceptions, and after this it has some value not INVALID_SOCKET.

When I try to send a packet though,

int r = sendto(udpSocket, (char*)dataToSend, (size_t)length, 0, (sockaddr*)&groupEndPoint, (socklen_t)sizeof(groupEndPoint));

I get errno 101: Network is unreachable.

I'm quite new to socket programming, and I know sockets in Android is a bad way to start, but the fact is I have to get this done and have very little time. Does anyone here know of a likely reason to get Network Unreachable? Has anyone tried playing with UDP on Android and can shed some light?

eskimo9
  • 753
  • 10
  • 24
  • The reason for this may be not in the code but in the network setup. If you are in your own private wi-fi network then you should check the router settings. – Viktor Latypov May 02 '12 at 07:21
  • im using physical LAN on an arm dev board device. I will have a dedicated link straight from the device to the device it's talking to. If anyone has tried this kind of thing before, help would be appreciated – eskimo9 May 02 '12 at 23:43
  • Can you ping the other system from the adb command line? – Chris Stratton May 03 '12 at 00:13
  • hm. It seems not. Ping to my laptop's IP returns network unreachable from adb shell. Thoughts? – eskimo9 May 03 '12 at 00:41
  • ip -s addr shows eth0: BROADCAST, MULTICAST mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 08:90:00:a0:02:10 brd ff:ff:ff:ff:ff:ff – eskimo9 May 03 '12 at 00:46

2 Answers2

1

Is there a requirement to use C++ sockets? If possible, in the interests of time, and pretty much in the interests of everything, I'd recommend the Java API instead. Here is an example of how to use it: http://android-er.blogspot.com/2011/01/simple-communication-using.html . I like C, but I would recommend against using it here.

Yusuf X
  • 14,513
  • 5
  • 35
  • 47
  • appreciate the advice, but the C++ is there, I was given a device driver library and told to convert it. I don't have the time or know-how to rewrite the library in java, what i have is c++ that is now ANSI compatible, my problem appears to be how android accesses the linux base network drivers, but I'm not sure. – eskimo9 May 02 '12 at 23:46
  • If it helps, it probably isn't how Android accesses the linux base network drivers. I've found Android is pretty consistent with other Linux distros. Does your code, or a command line version of it anyway, work on another distro? – Yusuf X May 03 '12 at 00:03
0

SOLVED:

I just had to monkey with the ethernet settings on the device to get it to talk to my laptop. for some reason it didn't like using the dedicated link, so I'm going through the local network router and it's working. now getting different issues, but this one's done

eskimo9
  • 753
  • 10
  • 24