Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Unable to get Native Socket to accept requests
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chad A.  
View profile  
 More options Jan 21 2012, 11:57 am
From: "Chad A." <calb...@neomantic.com>
Date: Sat, 21 Jan 2012 08:57:57 -0800 (PST)
Local: Sat, Jan 21 2012 11:57 am
Subject: Unable to get Native Socket to accept requests
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kristopher Micinski  
View profile  
 More options Jan 23 2012, 5:29 am
From: Kristopher Micinski <krismicin...@gmail.com>
Date: Mon, 23 Jan 2012 05:29:46 -0500
Local: Mon, Jan 23 2012 5:29 am
Subject: Re: [android-developers] Unable to get Native Socket to accept requests
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »