BlueZ C/C++ please help


I`m trying to compile a simple rfcomm server from a example BlueZ source code:


int main(int argc, char **argv)
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
int s, client, bytes_read;
socklen_t opt = sizeof(rem_addr);

// allocate socket
printf("tworze socket
perror("socket ");
// bind socket to port 1 of the first available
// local bluetooth adapter

loc_addr.rc_family = AF_BLUETOOTH;
//loc_addr.rc_bdaddr = *BDADDR_ANY;
bacpy(&loc_addr.rc_bdaddr, BDADDR_ANY);
loc_addr.rc_channel = (uint8_t)1;

bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));

perror("bind ");
// put socket into listening mode
listen(s, 1);
perror("listen ");
// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
perror("client ");
ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "accepted connection from %s
", buf);
memset(buf, 0, sizeof(buf));

// read data from the client
bytes_read = read(client, buf, sizeof(buf));
perror("read ");
if( bytes_read > 0 ) {
printf("received [%s]
", buf);

// close connection
return 0;

Everything compile fine, but I`m getting bind errors such as: 'Invalid argument' or sometimes also 'Adress already in use'.

Why I got invalid argument? What`s wrong with that? This is example from

Thx for help, galsan

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!