EBADFD on RFComm connect
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
Signature · 2 lines
Stephen Crane, Rococo Software Ltd. http://www.rococosoft.com [email protected] +353-1-6601315 (ext 209)
#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;
}#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;
}Quoted 8 lines · Stephen Crane
Hi Max, Marcel,
Under what circumstances canconnect()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
Quoted 7 lines · Max Krasnyansky
You can not do this
while (0 > connect(s, (struct sockaddr *)&addr, sizeof(addr))) if (errno != EBUSY) { perror("connect"); return -1; }
Quoted 5 lines
If connect returns EBUSY error you have to
close()the socket. And it does indeed return EBUSY in your case (I ran the progs) becauseclose()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 enableSO_LINGERoption on that socket, in which caseclose()will be synchronous.
Thanks Max. That's fixed it. Another one for the programming manual?
Steve
Signature · 2 lines
Stephen Crane, Rococo Software Ltd. http://www.rococosoft.com [email protected] +353-1-6601315 (ext 209)
Quoted 9 lines · Stephen Crane
Quoted 7 lines · Max Krasnyansky
You can not do this
while (0 > connect(s, (struct sockaddr *)&addr, sizeof(addr))) if (errno != EBUSY) { perror("connect"); return -1; }
Quoted 7 lines
Quoted 5 lines
If connect returns EBUSY error you have to
close()the socket. And it does indeed return EBUSY in your case (I ran the progs) becauseclose()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 enableSO_LINGERoption on that socket, in which caseclose()will be synchronous.Thanks Max. That's fixed it. Another one for the programming manual?
Yep.
Max