native socket creation failing

1,574 views
Skip to first unread message

vick.s

unread,
Sep 16, 2010, 5:46:30 AM9/16/10
to android-ndk
Hi seniors,
I'm trying to create native socket using linux api's. It is working
fine in linux desktop having ubuntu, but not in android. I tried both
TCP and UDP socket creation, none are working.
the code snippet is:
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1)
{
printerror("Socket creation failed\n");
return;
}

It is always printing the error log.
I wanted to know, what is the procedure to use native socket api's in
android?

thanks

Ngo Van Luyen

unread,
Sep 16, 2010, 7:06:59 AM9/16/10
to andro...@googlegroups.com
Did you try to put internet permission?



2010/9/16 vick.s <souvic...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Hello Android!
http://androidcore.com/

vick.s

unread,
Sep 17, 2010, 6:33:05 AM9/17/10
to android-ndk
Hi Van,
Thanks for pointing out the exact mistake i was making.
I put the internet permission in manifest file and the socket creation
worked fine.
But now it is failing at the next stage, bind.
I am working on android emulators. So i have used my desktop's IP
address for socket binding. The code snippet is:
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = /*INADDR_ANY*/
inet_addr("desktop IP address");
memset(&(my_addr.sin_zero), 0, 8);
bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr));

but bind is failing.
If i use INADDR_ANY, then bind is a success, but i dont know which
address to connect from client.
So my questions are:
1. Can i use my system's IP address as native server socket IP
address?
2. If i have to use INADDR_ANY, then how can the client know which IP
address to connect?

Thanks

On Sep 16, 4:06 pm, Ngo Van Luyen <nvluye...@gmail.com> wrote:
> Did you try to put internet permission?
>
> 2010/9/16 vick.s <souvick.i...@gmail.com>
>
>
>
> > Hi seniors,
> >  I'm trying to create native socket using linux api's. It is working
> > fine in linux desktop having ubuntu, but not in android. I tried both
> > TCP and UDP socket creation, none are working.
> > the code snippet is:
> >                 sockfd = socket(AF_INET, SOCK_STREAM, 0);
> >                 if(sockfd == -1)
> >                   {
> >                        printerror("Socket creation failed\n");
> >                        return;
> >                   }
>
> > It is always printing the error log.
> > I wanted to know, what is the procedure to use native socket api's in
> > android?
>
> > thanks
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .

David Turner

unread,
Sep 17, 2010, 6:53:21 AM9/17/10
to andro...@googlegroups.com
On Fri, Sep 17, 2010 at 12:33 PM, vick.s <souvic...@gmail.com> wrote:
Hi Van,
 Thanks for pointing out the exact mistake i was making.
I put the internet permission in manifest file and the socket creation
worked fine.
But now it is failing at the next stage, bind.
I am working on android emulators. So i have used my desktop's IP
address for socket binding. The code snippet is:
                 my_addr.sin_family = AF_INET;
                 my_addr.sin_port = htons(MYPORT);
                 my_addr.sin_addr.s_addr = /*INADDR_ANY*/
inet_addr("desktop IP address");
                 memset(&(my_addr.sin_zero), 0, 8);
                 bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr));

this should be sizeof(my_addr) (which probably is a struct sockaddr_in).
sizeof(struct sockaddr) < sizeof(struct sockaddr_in) and this probably explains the problem.

Also, why do you memset(sin_zero,0,8) ?
you should only memset(&my_addr, 0, sizeof my_addr) at the start of your routine
 
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

vick.s

unread,
Sep 17, 2010, 7:59:25 AM9/17/10
to android-ndk
Hi David,
Thanks for the reply.
I tried with
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr_in))
but not yet successful.
If I try with INADDR_ANY, then it is binding. In this case, is there
any way to know which address to connect from client?

Thanks

On Sep 17, 3:53 pm, David Turner <di...@android.com> wrote:
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>

Angus Lees

unread,
Sep 17, 2010, 8:03:22 AM9/17/10
to andro...@googlegroups.com
Also, the address Android gets inside the emulator is not the same address that your desktop has.

From memory, the emulator usually uses 10.0.2.2 (at least that is the qemu default).  More interestingly for you is that you need to set up port forwards from the real desktop networking world to the emulated machine in order to connect *to* the emulated device (connecting *from* Just Works).  "adb forward tcp:1234 tcp:456" will let you connect to port 1234 on your desktop and have that arrive at port 456 on emulated Android (you can use the same port number on the outside and inside and this is usually easier to remember).

 - Gus

David Turner

unread,
Sep 17, 2010, 9:23:21 AM9/17/10
to andro...@googlegroups.com
On Fri, Sep 17, 2010 at 1:59 PM, vick.s <souvic...@gmail.com> wrote:
Hi David,
 Thanks for the reply.
I tried with
     bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr_in))
but not yet successful.
If I try with INADDR_ANY, then it is binding. In this case, is there
any way to know which address to connect from client?

Because you can't bind to the address of your desktop PC (only connect to it).
Did you read the emulator networking documentation ?


 
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

vick.s

unread,
Sep 22, 2010, 2:26:22 AM9/22/10
to android-ndk
Hi,
Sorry for late reply.
Thanks for the documentation link. It helped in understanding the
procedure better.
However, still there is no success in connecting from client to
server. I believe I have followed all the steps mentioned in the
documentation, but it is failing to connect.
I will briefly tell the procedure i am following.
1. run the server in 1st emulator. set the server to listen at
10.0.2.15:6000 --> listen is successful
2. run telnet and do: redir add tcp:5000:6000 --> redirect device port
5000 to emulator port 6000
3. run client in 2nd emulator in the same device and try to connect to
10.0.2.2:5000 --> connect gives timeout error

there is no error shown in accept() in the server side.

This IP addresses are suggested in the documentation, i am not sure
about the details behind it.
I think, there is some mismatch in the IP address and port server is
listening and client is trying to connect.

can anybody suggest some corrections?

Thanks

On Sep 17, 6:23 pm, David Turner <di...@android.com> wrote:
> On Fri, Sep 17, 2010 at 1:59 PM, vick.s <souvick.i...@gmail.com> wrote:
> > Hi David,
> >  Thanks for the reply.
> > I tried with
> >       bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct
> > sockaddr_in))
> > but not yet successful.
> > If I try with INADDR_ANY, then it is binding. In this case, is there
> > any way to know which address to connect from client?
>
> > Because you can't bind to the address of your desktop PC (only connect to
>
> it).
> Did you read the emulator networking documentation ?
>
> http://developer.android.com/guide/developing/tools/emulator.html#emu...
> > > > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
> > <android-ndk%252Buns...@googlegroups.com<android-ndk%25252Bun...@googlegroups.com>

David Turner

unread,
Sep 22, 2010, 5:47:40 AM9/22/10
to andro...@googlegroups.com
1/ Are you sure you setup the redirection on the 1st emulator's console (and not the second)

2 /did you try connecting from your development machine to check that the redirection works.
I.e. what does "telnet 127.0.0.1 5000" does when you try it in a terminal while running your two emulators ?

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

Angus Lees

unread,
Sep 22, 2010, 8:00:43 AM9/22/10
to andro...@googlegroups.com
On Wed, Sep 22, 2010 at 16:26, vick.s <souvic...@gmail.com> wrote:
Hi,
 Sorry for late reply.
Thanks for the documentation link. It helped in understanding the
procedure better.
However, still there is no success in connecting from client to
server. I believe I have followed all the steps mentioned in the
documentation, but it is failing to connect.
I will briefly tell the procedure i am following.
1. run the server in 1st emulator. set the server to listen at
10.0.2.15:6000    --> listen is successful
2. run telnet and do: redir add tcp:5000:6000 --> redirect device port
5000 to emulator port 6000
3. run client in 2nd emulator in the same device and try to connect to
10.0.2.2:5000  --> connect gives timeout error

there is no error shown in accept() in the server side.

This IP addresses are suggested in the documentation, i am not sure
about the details behind it.
I think, there is some mismatch in the IP address and port server is
listening and client is trying to connect.

can anybody suggest some corrections?

As David suggests, test first with "telnet localhost 5000" from your desktop.  This should end up at the server on the first emulator.

In your second emulator, you should connect to your *desktop ip*, port 5000 - not 10.0.2.2.  The two emulator images don't have any secret network between them (unless you play with qemu options), so the second emulator has to go out to your desktop, and then hit the 5000->emulator:6000 port forwarder in order to get back in to Android on the first emulator.
 
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

David Turner

unread,
Sep 22, 2010, 9:07:07 AM9/22/10
to andro...@googlegroups.com
On Wed, Sep 22, 2010 at 2:00 PM, Angus Lees <g...@inodes.org> wrote:
On Wed, Sep 22, 2010 at 16:26, vick.s <souvic...@gmail.com> wrote:
Hi,
 Sorry for late reply.
Thanks for the documentation link. It helped in understanding the
procedure better.
However, still there is no success in connecting from client to
server. I believe I have followed all the steps mentioned in the
documentation, but it is failing to connect.
I will briefly tell the procedure i am following.
1. run the server in 1st emulator. set the server to listen at
10.0.2.15:6000    --> listen is successful
2. run telnet and do: redir add tcp:5000:6000 --> redirect device port
5000 to emulator port 6000
3. run client in 2nd emulator in the same device and try to connect to
10.0.2.2:5000  --> connect gives timeout error

there is no error shown in accept() in the server side.

This IP addresses are suggested in the documentation, i am not sure
about the details behind it.
I think, there is some mismatch in the IP address and port server is
listening and client is trying to connect.

can anybody suggest some corrections?

As David suggests, test first with "telnet localhost 5000" from your desktop.  This should end up at the server on the first emulator.

In your second emulator, you should connect to your *desktop ip*, port 5000 - not 10.0.2.2.  The two emulator images don't have any secret network between them (unless you play with qemu options), so the second emulator has to go out to your desktop, and then hit the 5000->emulator:6000 port forwarder in order to get back in to Android on the first emulator.
 
not true, 10.0.2.2 is always mapped to 127.0.0.1 on the development machine. Besides, the network redirection you setup will make the emulator listen for connection attemps on 127.0.0.1 too. This is an intentional security measure.

vick.s

unread,
Sep 23, 2010, 4:41:50 AM9/23/10
to android-ndk
hi guys,
yes i have tried telnet localhost 5000
it is connecting to the server. accept is successful. recv is also
working
but client is not able to connect.
i tried to connect to my desktop IP too from client. But no result :(
also i tried to connect directly to 127.0.0.1 from client. failed :(

This is my client code:
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl("10.0.2.2");
addr.sin_port = htons(5000);
memset(&(addr.sin_zero), 0, 8);

rc = connect(Clisockfd, (struct sockaddr *)&addr, sizeof(struct
sockaddr_in));

if you see anything wrong, please pass your feedbacks.
Also, is there any way to test my client? (as thru telnet i can
connect to server, i assume server code is proper)

thanks

On Sep 22, 6:07 pm, David Turner <di...@android.com> wrote:
> On Wed, Sep 22, 2010 at 2:00 PM, Angus Lees <g...@inodes.org> wrote:
> >> > > > > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
> >> <android-ndk%252Buns...@googlegroups.com<android-ndk%25252Bun...@googlegroups.com>
>
> >> > > <android-ndk%252Buns...@googlegroups.com<android-ndk%25252Bun...@googlegroups.com>
> >> <android-ndk%25252Bun...@googlegroups.com<android-ndk%2525252Bu...@googlegroups.com>

Chris Stratton

unread,
Sep 23, 2010, 6:11:05 AM9/23/10
to android-ndk
I think you should try testing with netcat (nc) instead of your
software, on one or both ends. I believe it ships with stock android
(if not it builds easily) and is also available for desktops.

For example, you can run a listening instance on the desktop and
verify that your client in the emulator can connect to it. Or you can
use an instance in one emulator to connect to your server or a
listening instance in the other.

Angus Lees

unread,
Sep 23, 2010, 7:38:58 AM9/23/10
to andro...@googlegroups.com
On Wed, Sep 22, 2010 at 23:07, David Turner <di...@android.com> wrote:
not true, 10.0.2.2 is always mapped to 127.0.0.1 on the development machine. Besides, the network redirection you setup will make the emulator listen for connection attemps on 127.0.0.1 too. This is an intentional security measure.

Oh duh.  I was thinking of 10.0.2.15 and had completely forgotten about the other magic addresses.  Sorry for adding confusion :/

 - Gus

David Turner

unread,
Sep 24, 2010, 3:10:27 AM9/24/10
to andro...@googlegroups.com
On Thu, Sep 23, 2010 at 10:41 AM, vick.s <souvic...@gmail.com> wrote:
hi guys,
 yes i have tried telnet localhost 5000
it is connecting to the server. accept is successful. recv is also
working
but client is not able to connect.
i tried to connect to my desktop IP too from client. But no result :(
also i tried to connect directly to 127.0.0.1 from client. failed :(

This is my client code:
   addr.sin_family = AF_INET;
   addr.sin_addr.s_addr = htonl("10.0.2.2");

No, this is not how htonl works at all :-) You're trying to use the memory address of a constant string as an IP address. This cannot work.
Use inet_addr() or inet_aton() instead.

 
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

vick.s

unread,
Sep 24, 2010, 6:53:18 AM9/24/10
to android-ndk
Hi David,
thanks a lot for the suggestion. It worked finally :)

Thanks to everybody for their time and help.

thanks

On Sep 24, 12:10 pm, David Turner <di...@android.com> wrote:
> > > >> > > <android-ndk%252Buns...@googlegroups.com<android-ndk%25252Bun...@googlegroups.com>
> > <android-ndk%25252Bun...@googlegroups.com<android-ndk%2525252Bu...@googlegroups.com>
>
> > > >> <android-ndk%25252Bun...@googlegroups.com<android-ndk%2525252Bu...@googlegroups.com>
> > <android-ndk%2525252Bu...@googlegroups.com<android-ndk%252525252B...@googlegroups.com>
>
> > > >> > > > > > > .
> > > >> > > > > > > For more options, visit this group at
> > > >> > > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > >> > > > > > --
> > > >> > > > > > Hello Android!http://androidcore.com/
>
> > > >> > > > > --
> > > >> > > > > You received this message because you are subscribed to the
> > Google
> > > >> > > Groups
> > > >> > > > > "android-ndk" group.
> > > >> > > > > To post to this group, send email to
> > andro...@googlegroups.com
> > > >> .
> > > >> > > > > To unsubscribe from this group, send email to
> > > >> > > > > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
>
> > > >> <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
> > <android-ndk%252Buns...@googlegroups.com<android-ndk%25252Bun...@googlegroups.com>
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages