about ndk fcntl O_NONBLOCK

839 views
Skip to first unread message

lu_zi

unread,
Feb 4, 2012, 10:47:49 AM2/4/12
to android-ndk
hi all,
question:I create a NDK program.It's about async socket.
coding:
int sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr_org.sin_family = AF_INET;
addr_org.sin_addr.s_addr = inet_addr("192.168.1.101");
addr_org.sin_port = htons(28888);
connect(sd,(sockaddr*)&addr_org,sizeof(addr_org));

int flags = fcntl(sd, F_GETFL);
fcntl(sd,F_SETFL,O_NONBLOCK|flags);

char e;
ret=recv(sd,&e,1,0);

running there,it BLOCKED.oh my God,who can help me?

Futumsh

unread,
Feb 6, 2012, 10:06:04 AM2/6/12
to android-ndk
Did it block on the read, or on the connect?

I haven't had trouble with nonblocking sockets in our android apps,
though I use the ioctl rather than the fcntl to set nonblocking, so
you may want to check that if it is blocking on the read.

To set nonblocking with the ioctl:

int val = 1;
ioctl(sd, FIONBIO, &val);

zi lu

unread,
Feb 8, 2012, 4:08:14 AM2/8/12
to andro...@googlegroups.com
I use ioctl also.but I found It didn't work with epoll.do you use epoll? and is your program in common premission?

thanks!

2012/2/6 Futumsh <rdela...@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.


David Turner

unread,
Feb 8, 2012, 5:00:59 AM2/8/12
to andro...@googlegroups.com
I don't see any reason for this not to work properly (of course, connect() will block in the code sample you posted).
The platform uses O_NONBLOCK extensively, with select(), epoll() and plenty of other system calls.

If you have a simple way to reproduce the problem, please file a bug at b.android.com and upload it there.

Thanks

zi lu

unread,
Feb 9, 2012, 1:58:15 AM2/9/12
to andro...@googlegroups.com
ok,thanks.I have used other method.maybe my environment is a bit special.

2012/2/8 David Turner <di...@android.com>
Reply all
Reply to author
Forward
0 new messages