First of all:
  -- Why are you running a server on a device?  In general this is a
"bad idea" because you're probably going to make your users hate you
when their batteries die extremely quickly.
  -- You understand that the Android system can, at any time, kill
your app right?
  -- You probably need at least a wake lock to make something like this work
  -- If it's tinyish, why not just rewrite it using Java?  This is (I
assure you) going to be much easier than doing the proper
communication between JNI / Service / rest of the app.  (This, of
course, depends on your definition of "tinyish.")
  -- What is the value of 'errno' after your call fails?

kris

On Sat, Jan 21, 2012 at 11:57 AM, Chad A. <[email protected]> wrote:
> I'm porting a tinyish server to Android using the NDK.  The ultimate
> goal is for my app to interact with the server through JNI and Android
> Service classes.
>
> I've run across a stumbling block, however.  The server fails to start
> as soon as I call accept() on the socket.  I've pasted the code
> below.  All I want to to is open a socket that listens to localhost
> requests of port 8888.
>
> If anyone can give me advice, I would greatly appreciate it. Thanks,
>
> -------
>
>
> static void
> socket_listen_to(const char *host_name, unsigned int port) {
>  struct sockaddr_in address;
>  int listening_socket;
>  int connection_socket;
>  if (host_name) {
>    struct hostent *host_data;
>    host_data = gethostbyname(host_name);
>    if (!host_data || host_data->h_addrtype != AF_INET || host_data-
>>h_length != sizeof address.sin_addr) {
>      exit(EXIT_FAILURE);
>    }
>    host_name = host_data->h_name;
>    memcpy(&address.sin_addr, host_data->h_addr_list[0],
>           sizeof address.sin_addr);
>  }
>
>  listening_socket = socket(PF_INET, SOCK_STREAM, 0);
>  if (listening_socket == -1) {
>    exit(EXIT_FAILURE);
>  }
>
>  address.sin_family = AF_INET;
>  address.sin_port = htons((unsigned short) port);
>
>  if (bind(listening_socket, (struct sockaddr *) &address, sizeof
> address) == -1
>      || listen(listening_socket, 5) == -1 ) {
>    closesocket(listening_socket);
>    exit(EXIT_FAILURE);
>  }
>  connection_socket = accept(listening_socket, NULL, NULL);
>  if (connection_socket == -1 ) {
>    closesocket(listening_socket);
>    exit(EXIT_FAILURE);
>  }
> }
>
> unsigned int port = 8888;
> const char host_name[] = "localhost";
> socket_listen_at(host_name, port, &gtp_input_FILE, &gtp_output_FILE);
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to