* EBADFD on RFComm connect
@ 2003-07-22 13:29 Stephen Crane
2003-07-23 0:33 ` [Bluez-devel] " Max Krasnyansky
0 siblings, 1 reply; 4+ messages in thread
From: Stephen Crane @ 2003-07-22 13:29 UTC (permalink / raw)
To: bluez-devel; +Cc: maxk, marcel
[-- Attachment #1: Type: text/plain, Size: 575 bytes --]
Hi Max, Marcel,
Under what circumstances can connect() return EBADFD?
I have a couple of programs (attached) one of which opens a listening
socket and accepts 32 connections. The other loops attempting 32
connections to the same channel. On the second connection connect()
always fails with EBADFD. (If I leave in the 0.5 second delay it all
works fine.)
I've also attached hcidumps from each machine --- each is running kernel
2.4.21-mh2.
Thanks,
Steve
--
Stephen Crane, Rococo Software Ltd. http://www.rococosoft.com
steve.crane@rococosoft.com +353-1-6601315 (ext 209)
[-- Attachment #2: connector.c --]
[-- Type: text/x-c, Size: 678 bytes --]
#include <sys/socket.h>
#include <netinet/in.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
struct sockaddr_rc addr;
int addr_len = sizeof(addr);
int i;
addr.rc_channel = 2;
addr.rc_family = AF_BLUETOOTH;
baswap(&addr.rc_bdaddr, strtoba(argv[1]));
for (i = 0; i < 32; i++) {
int s = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
char c;
while (0 > connect(s, (struct sockaddr *)&addr, sizeof(addr)))
if (errno != EBUSY) {
perror("connect");
return -1;
}
printf("connected %d\n", i);
read(s, &c, 1);
close(s);
// usleep(500000);
}
return 0;
}
[-- Attachment #3: listener.c --]
[-- Type: text/x-c, Size: 914 bytes --]
#include <unistd.h>
#include <malloc.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
int main(int argc, char *argv[])
{
int l = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
struct sockaddr_rc addr, myaddr;
int i, addr_len = sizeof(addr);
if (0 > l) {
perror("socket");
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = 2;
bacpy(&addr.rc_bdaddr, BDADDR_ANY);
if (0 > bind(l, (struct sockaddr *)&addr, sizeof(addr))) {
perror("bind");
return -1;
}
if (0 > listen(l, 10)) {
perror("listen");
return -1;
}
for (i=0; i < 32; i++) {
int s = accept(l, (struct sockaddr *)&addr, &addr_len);
char c;
if (0 > s) {
perror("accept");
return -1;
}
printf("accepted %d\n", i);
write(s, &c, 1);
close(s);
}
return 0;
}
[-- Attachment #4: connect --]
[-- Type: application/octet-stream, Size: 1254 bytes --]
[-- Attachment #5: listen --]
[-- Type: application/octet-stream, Size: 1274 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [Bluez-devel] EBADFD on RFComm connect
2003-07-22 13:29 EBADFD on RFComm connect Stephen Crane
@ 2003-07-23 0:33 ` Max Krasnyansky
2003-07-24 14:09 ` Stephen Crane
0 siblings, 1 reply; 4+ messages in thread
From: Max Krasnyansky @ 2003-07-23 0:33 UTC (permalink / raw)
To: Stephen Crane, bluez-devel; +Cc: marcel
At 06:29 AM 7/22/2003, Stephen Crane wrote:
>Hi Max, Marcel,
>Under what circumstances can connect() return EBADFD?
>
>I have a couple of programs (attached) one of which opens a listening
>socket and accepts 32 connections. The other loops attempting 32
>connections to the same channel. On the second connection connect()
>always fails with EBADFD. (If I leave in the 0.5 second delay it all
>works fine.)
You can not do this
while (0 > connect(s, (struct sockaddr *)&addr, sizeof(addr)))
if (errno != EBUSY) {
perror("connect");
return -1;
}
If connect returns EBUSY error you have to close() the socket. And it does
indeed return EBUSY in your case (I ran the progs) because close() is asynchronous
and that channel still exist (two connections on the same channel are not allowed
in RFCOMM). What you can do to avoid that error is to enable SO_LINGER option on that
socket, in which case close() will be synchronous.
Max
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [Bluez-devel] EBADFD on RFComm connect
2003-07-23 0:33 ` [Bluez-devel] " Max Krasnyansky
@ 2003-07-24 14:09 ` Stephen Crane
2003-07-24 17:38 ` Max Krasnyansky
0 siblings, 1 reply; 4+ messages in thread
From: Stephen Crane @ 2003-07-24 14:09 UTC (permalink / raw)
To: Max Krasnyansky; +Cc: bluez-devel, marcel
On Wed, 2003-07-23 at 01:33, Max Krasnyansky wrote:
> You can not do this
> while (0 > connect(s, (struct sockaddr *)&addr, sizeof(addr)))
> if (errno != EBUSY) {
> perror("connect");
> return -1;
> }
>
> If connect returns EBUSY error you have to close() the socket. And it does
> indeed return EBUSY in your case (I ran the progs) because close() is asynchronous
> and that channel still exist (two connections on the same channel are not allowed
> in RFCOMM). What you can do to avoid that error is to enable SO_LINGER option on that
> socket, in which case close() will be synchronous.
Thanks Max. That's fixed it. Another one for the programming manual?
Steve
--
Stephen Crane, Rococo Software Ltd. http://www.rococosoft.com
steve.crane@rococosoft.com +353-1-6601315 (ext 209)
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [Bluez-devel] EBADFD on RFComm connect
2003-07-24 14:09 ` Stephen Crane
@ 2003-07-24 17:38 ` Max Krasnyansky
0 siblings, 0 replies; 4+ messages in thread
From: Max Krasnyansky @ 2003-07-24 17:38 UTC (permalink / raw)
To: Stephen Crane; +Cc: bluez-devel, marcel
At 07:09 AM 7/24/2003, Stephen Crane wrote:
>On Wed, 2003-07-23 at 01:33, Max Krasnyansky wrote:
>
>> You can not do this
>> while (0 > connect(s, (struct sockaddr *)&addr, sizeof(addr)))
>> if (errno != EBUSY) {
>> perror("connect");
>> return -1;
>> }
>>
>> If connect returns EBUSY error you have to close() the socket. And it does
>> indeed return EBUSY in your case (I ran the progs) because close() is asynchronous
>> and that channel still exist (two connections on the same channel are not allowed
>> in RFCOMM). What you can do to avoid that error is to enable SO_LINGER option on that
>> socket, in which case close() will be synchronous.
>
>Thanks Max. That's fixed it. Another one for the programming manual?
Yep.
Max
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-07-24 17:38 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-07-22 13:29 EBADFD on RFComm connect Stephen Crane
2003-07-23 0:33 ` [Bluez-devel] " Max Krasnyansky
2003-07-24 14:09 ` Stephen Crane
2003-07-24 17:38 ` Max Krasnyansky
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox