2006-04-19 02:25:54

by Eric Munson

[permalink] [raw]
Subject: [Bluez-users] Programming in C with Bluetooth Sockets

I am attempting to move data across a bluetooth connection using
RFCOMM sockets in C.

I am able to establish the connection and I can successfully send and
receive the first four bytes of the message. The sending program
reports a complete success and the receiving program only reads the
first four characters.

I have tried several things to make this work, first was to send the
message size initially and then break the message up into pieces no
bigger than 4 bytes and send them individually. The sending program
again reports a complete success and the receiving program gets the
correct message size and only the first four bytes of the message.

The next step I tried was to have each side close the socket after
each transmission and open a new socket for each data chunk. This led
to the same results as before.

I am at a loss about what I am doing wrong after searching the web for
information on bluetooth socket programming. Has Anyone seen this
sort of problem before and if so what did you do to fix it? Thanks
for your help.


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users


2006-04-20 06:46:29

by Eric Munson

[permalink] [raw]
Subject: Re: [Bluez-users] Programming in C with Bluetooth Sockets

That did solve the problem, thanks again for catching such a silly mistake.

Eric

On 4/19/06, Eric Munson <[email protected]> wrote:
> Thanks very much for catching this, I believe that it is the problem.
> I will change my code and give it a try.
>
> Eric
>
> On 4/19/06, Albert Huang <[email protected]> wrote:
> >
> >
> > > bytes_read =3D read(client, buf + count, sizeof(buf) -
> > count);
> >
> >
> > sizeof(buf) will always return 4 because buf was allocated as a char* =
and
> > not a char[]
> > So it's never trying to read more than 4 bytes at a time
> >
> > -albert
> >
> >
>


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

2006-04-19 20:41:34

by Eric Munson

[permalink] [raw]
Subject: Re: [Bluez-users] Programming in C with Bluetooth Sockets

Thanks very much for catching this, I believe that it is the problem.=20
I will change my code and give it a try.

Eric

On 4/19/06, Albert Huang <[email protected]> wrote:
>
>
> > bytes_read =3D read(client, buf + count, sizeof(buf) -
> count);
>
>
> sizeof(buf) will always return 4 because buf was allocated as a char* an=
d
> not a char[]
> So it's never trying to read more than 4 bytes at a time
>
> -albert
>
>


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

2006-04-19 18:32:38

by Albert Huang

[permalink] [raw]
Subject: Re: [Bluez-users] Programming in C with Bluetooth Sockets

> bytes_read = read(client, buf + count, sizeof(buf) -
> count);


sizeof(buf) will always return 4 because buf was allocated as a char* and
not a char[]
So it's never trying to read more than 4 bytes at a time

-albert


Attachments:
(No filename) (236.00 B)
(No filename) (495.00 B)
Download all attachments

2006-04-19 18:26:07

by Eric Munson

[permalink] [raw]
Subject: Re: [Bluez-users] Programming in C with Bluetooth Sockets

This is the function run by the bluetooth device that will recieve the data=
.

char* BT_Target()
{
int s, client, bytes_read, count =3D 0;
char * buf;
struct sockaddr_rc loc_addr =3D { 0 }, rem_addr =3D { 0 };
int opt =3D sizeof(rem_addr);
unsigned int max_count;

//attempt to allocate reception buffer
buf =3D (char *)calloc(MAX_MSG, 1);

if(buf =3D=3D NULL)
{
perror("No memory!");
return("No memory!");
}

//attempt to allocate socket
s =3D socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

if(s < 0)
{
perror("socket failed to open:");
return("socket failed to open");
}

loc_addr.rc_family =3D AF_BLUETOOTH;
loc_addr.rc_bdaddr =3D *BDADDR_ANY;
loc_addr.rc_channel =3D (uint8_t) RFCOMM_CHANNEL;

//attempt to bind the socket for a maximum number of attempts
for( count =3D 0; count < MAX_ATTEMPTS && bind(s, (struct sockaddr
*)&loc_addr, sizeof(loc_addr)) < 0; count++);

//if we tried to open the socket but failed
if( count =3D=3D MAX_ATTEMPTS )
{
perror("unable to bind socket: ");
close(s);
free(buf);
return "unable to bind socket";
}

//attempt to listen
if(listen(s, 1) < 0)
{
perror("listen failed: ");
close(s);
free(buf);
return "listen failed.";
}

//attempt to accept the socket
rem_addr.rc_family =3D AF_BLUETOOTH;
rem_addr.rc_bdaddr =3D *BDADDR_ANY;
rem_addr.rc_channel =3D (uint8_t) RFCOMM_CHANNEL;

client =3D accept(s, (struct sockaddr *)&rem_addr, &opt);

//if we have created a connection
//we need to recieve the transmission length into max_count
if(client >=3D 0)
{
count =3D 0;
fprintf(stderr,"Got a connection\n");
read(client, &max_count, sizeof(unsigned int));
printf( "Message size: %d\n", max_count );
close( client );

}
//the accept has failed
else
{
perror("accept failed: ");
close(s);
free(buf);
return("accept failed");
}

//while we have not recieved the entire message, get the next part
while( count < max_count &&
( client =3D accept(s, (struct sockaddr*)&rem_addr, &opt) )=
>=3D 0 )
{
bytes_read =3D read(client, buf + count, sizeof(buf) - count=
);

fprintf(stderr, "got %d bytes this time\n", bytes_read);
count +=3D bytes_read;
fprintf(stderr, "recieved %d [%s] so far\n", count, buf);
close( client );

if(bytes_read < 0)
{
perror("socket issue.");
fprintf(stderr, "We have socket problems.\n");
close(s);
free(buf);
close(client);
return("We have socket Problems.");
}
}

close(s);
close(client);

return "Success";
}

And this is the function that will send the data.
char* BT_Source(char* fname, int iter, char* tMAC)
{
struct sockaddr_rc addr =3D { 0 };
int s, status, count =3D 0, fd;
char* dest =3D tMAC;
struct stat *stats;
unsigned int len =3D 0;
char* buf;

//attempt to allocate a socket
s =3D socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if(s < 0)
{
perror("error creating socket: ");
return "socket creation failed.";
}

// set the connection parameters (who to connect to)
addr.rc_family =3D AF_BLUETOOTH;
addr.rc_channel =3D (uint8_t) RFCOMM_CHANNEL;

fprintf(stderr, "The dest addr is now %s\n", dest);
str2ba(dest, &addr.rc_bdaddr);

//attempt to connect the socket for our maximum number of times
status =3D connect(s, (struct sockaddr *)&addr, sizeof(addr));
for(count =3D 0; status !=3D 0 && count < MAX_ATTEMPTS;)
{
perror("retrying connection: ");

//if the socket is busy or the host is down, close and reope=
n
if(!(errno =3D=3D EBUSY || errno =3D=3D EHOSTDOWN))
{
close(s);
s =3D socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCO=
MM);
if(s < 0)
{
perror("error creating socket: ");
return "socket creation failed.";
}
count++;
}

status =3D connect(s, (struct sockaddr *)&addr, sizeof(addr)=
);
}

//if the socket was opened properly, send a message
if(status =3D=3D 0)
{
count =3D 0;

//get the file size and send it to the client
stat(fname, stats);
len =3D stats->st_size;
fprintf(stderr, "The length in bytes is %d\n", len);
write(s, &len, sizeof(unsigned int));

//open the file
if((fd =3D open(fname, O_RDONLY)) =3D=3D NULL)
{
perror( "Cannot open file for reading" );
close( s );
return "failed";
}

//attempt to allocate reception buffer
buf =3D (char *)calloc( MAX_MSG + 1, 1 );

if( buf =3D=3D NULL )
{
perror( "out of memory" );
close( fd );
close( s );
return "failed";
}

//while we still have message to send
while((in =3D read(fd, buf, MAX_MSG)) > 0)
{
//close previous socket connection to force a flush
close( s );

//allocate a socket
s =3D socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCO=
MM);
if(s < 0)
{
perror("error creating socket: ");
return "socket creation failed.";
}

//connect the socket
status =3D connect(s, (struct sockaddr *)&addr,
sizeof(addr));
for(count =3D 0; status !=3D 0 && count < MAX_ATTEMP=
TS;)
{
perror("retrying connection: ");

if(!(errno =3D=3D EBUSY || errno =3D=3D EHOS=
TDOWN))
{
close(s);
s =3D socket(AF_BLUETOOTH,
SOCK_STREAM, BTPROTO_RFCOMM);
if(s < 0)
{
perror("error creating
socket: ");
return "socket creation fail=
ed.";
}
count++;
sleep( 1 );
}

status =3D connect(s, (struct sockaddr
*)&addr, sizeof(addr));
}

if( status < 0 )
{
perror("message failed: ");
close(s);
close(fd);
fprintf(stderr, "failed connecting, I
tried %d times.\n", count);
return "uh oh, that message did not go throu=
gh";
}

//write the next message segment
fprintf(stderr,"data read '%s'\n", buf);
count =3D write(s, buf, MAX_MSG);
fprintf(stderr,"contents of in: %d, %d bytes
were written\n", in, count);
}
}
else if(status < 0)
{
perror("message failed: ");
close(s);
close(fd);
fprintf(stderr, "failed connecting, I tried %d times.\n", co=
unt);
return "uh oh, that message did not go through";
}

fprintf(stderr, "message sent\n");
close(s);
close(fd);

return strdup("Success");
}

This is the latest iteration where the connection is stopped after
each 4 byte transmission and restarted before the next.


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

2006-04-19 16:56:56

by Albert Huang

[permalink] [raw]
Subject: Re: [Bluez-users] Programming in C with Bluetooth Sockets

use hcidump -X -V to examine the packets that actually get transmitted and
received. That might give you some insight on where things are going wrong.

-albert

On 4/19/06, Eric Munson <[email protected]> wrote:
>
> Thanks for the link, but I have already been through that
> documentation and my initial attempt was a direct copy of of his
> RFCOMM socket code found here:
> http://people.csail.mit.edu/albert/bluez-intro/x499.html This code
> gave the results that I am describing (only getting the first 4 bytes
> of a string).
>
> Thanks,
> Eric
>
> On 4/18/06, Sowmya Gattupalli <[email protected]> wrote:
> > I think u are not doing the socket programmin properly
> > check out this link
> > http://people.csail.mit.edu/albert/bluez-intro/index.html
> > by Albert
> >
> >
> > On 4/19/06, Eric Munson <[email protected]> wrote:
> > >
> > I am attempting to move data across a bluetooth connection using
> > RFCOMM sockets in C.
> >
> > I am able to establish the connection and I can successfully send and
> > receive the first four bytes of the message. The sending program
> > reports a complete success and the receiving program only reads the
> > first four characters.
> >
> > I have tried several things to make this work, first was to send the
> > message size initially and then break the message up into pieces no
> > bigger than 4 bytes and send them individually. The sending program
> > again reports a complete success and the receiving program gets the
> > correct message size and only the first four bytes of the message.
> >
> > The next step I tried was to have each side close the socket after
> > each transmission and open a new socket for each data chunk. This led
> > to the same results as before.
> >
> > I am at a loss about what I am doing wrong after searching the web for
> > information on bluetooth socket programming. Has Anyone seen this
> > sort of problem before and if so what did you do to fix it? Thanks
> > for your help.
> >
> >
> > -------------------------------------------------------
> > This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> > that extends applications into web and mobile media. Attend the live
> webcast
> > and join the prime developer group breaking into this new coding
> territory!
> > http://sel.as-us.falkag.net/sel?cmdlnk&kid0944&bid$1720&dat1642
> > _______________________________________________
> > Bluez-users mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/bluez-users
> >
> >
>
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job
> easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmdlnk&kid0709&bid&3057&dat1642
> _______________________________________________
> Bluez-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/bluez-users
>


Attachments:
(No filename) (3.03 kB)
(No filename) (4.28 kB)
Download all attachments

2006-04-19 16:50:03

by Sowmya Gattupalli

[permalink] [raw]
Subject: Re: [Bluez-users] Programming in C with Bluetooth Sockets

well Eric
Iam surprised at ur problem.... I sent a chunk of msg and I never faced
this problem.....
just give the code u have written may be I can help u or try to find out
exactly what lines are causing the problem and paste it here..... I shall
implement in my Linux Box and chk out!!

On 4/19/06, Eric Munson <[email protected]> wrote:
>
> Thanks for the link, but I have already been through that
> documentation and my initial attempt was a direct copy of of his
> RFCOMM socket code found here:
> http://people.csail.mit.edu/albert/bluez-intro/x499.html This code
> gave the results that I am describing (only getting the first 4 bytes
> of a string).
>
> Thanks,
> Eric
>
> On 4/18/06, Sowmya Gattupalli <[email protected]> wrote:
> > I think u are not doing the socket programmin properly
> > check out this link
> > http://people.csail.mit.edu/albert/bluez-intro/index.html
> > by Albert
> >
> >
> > On 4/19/06, Eric Munson <[email protected]> wrote:
> > >
> > I am attempting to move data across a bluetooth connection using
> > RFCOMM sockets in C.
> >
> > I am able to establish the connection and I can successfully send and
> > receive the first four bytes of the message. The sending program
> > reports a complete success and the receiving program only reads the
> > first four characters.
> >
> > I have tried several things to make this work, first was to send the
> > message size initially and then break the message up into pieces no
> > bigger than 4 bytes and send them individually. The sending program
> > again reports a complete success and the receiving program gets the
> > correct message size and only the first four bytes of the message.
> >
> > The next step I tried was to have each side close the socket after
> > each transmission and open a new socket for each data chunk. This led
> > to the same results as before.
> >
> > I am at a loss about what I am doing wrong after searching the web for
> > information on bluetooth socket programming. Has Anyone seen this
> > sort of problem before and if so what did you do to fix it? Thanks
> > for your help.
> >
> >
> > -------------------------------------------------------
> > This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> > that extends applications into web and mobile media. Attend the live
> webcast
> > and join the prime developer group breaking into this new coding
> territory!
> > http://sel.as-us.falkag.net/sel?cmdlnk&kid0944&bid$1720&dat1642
> > _______________________________________________
> > Bluez-users mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/bluez-users
> >
> >
>
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job
> easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmdlnk&kid0709&bid&3057&dat1642
> _______________________________________________
> Bluez-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/bluez-users
>


Attachments:
(No filename) (3.15 kB)
(No filename) (4.41 kB)
Download all attachments

2006-04-19 15:04:31

by Eric Munson

[permalink] [raw]
Subject: Re: [Bluez-users] Programming in C with Bluetooth Sockets

Thanks for the link, but I have already been through that
documentation and my initial attempt was a direct copy of of his
RFCOMM socket code found here:
http://people.csail.mit.edu/albert/bluez-intro/x499.html This code
gave the results that I am describing (only getting the first 4 bytes
of a string).

Thanks,
Eric

On 4/18/06, Sowmya Gattupalli <[email protected]> wrote:
> I think u are not doing the socket programmin properly
> check out this link
> http://people.csail.mit.edu/albert/bluez-intro/index.html
> by Albert
>
>
> On 4/19/06, Eric Munson <[email protected]> wrote:
> >
> I am attempting to move data across a bluetooth connection using
> RFCOMM sockets in C.
>
> I am able to establish the connection and I can successfully send and
> receive the first four bytes of the message. The sending program
> reports a complete success and the receiving program only reads the
> first four characters.
>
> I have tried several things to make this work, first was to send the
> message size initially and then break the message up into pieces no
> bigger than 4 bytes and send them individually. The sending program
> again reports a complete success and the receiving program gets the
> correct message size and only the first four bytes of the message.
>
> The next step I tried was to have each side close the socket after
> each transmission and open a new socket for each data chunk. This led
> to the same results as before.
>
> I am at a loss about what I am doing wrong after searching the web for
> information on bluetooth socket programming. Has Anyone seen this
> sort of problem before and if so what did you do to fix it? Thanks
> for your help.
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting langua=
ge
> that extends applications into web and mobile media. Attend the live webc=
ast
> and join the prime developer group breaking into this new coding territor=
y!
> http://sel.as-us.falkag.net/sel?cmdlnk&kid=110944&bid$1720&dat=121642
> _______________________________________________
> Bluez-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/bluez-users
>
>


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

2006-04-19 04:18:17

by Sowmya Gattupalli

[permalink] [raw]
Subject: Re: [Bluez-users] Programming in C with Bluetooth Sockets

I think u are not doing the socket programmin properly
check out this link
http://people.csail.mit.edu/albert/bluez-intro/index.html by Albert

On 4/19/06, Eric Munson <[email protected]> wrote:
>
> I am attempting to move data across a bluetooth connection using
> RFCOMM sockets in C.
>
> I am able to establish the connection and I can successfully send and
> receive the first four bytes of the message. The sending program
> reports a complete success and the receiving program only reads the
> first four characters.
>
> I have tried several things to make this work, first was to send the
> message size initially and then break the message up into pieces no
> bigger than 4 bytes and send them individually. The sending program
> again reports a complete success and the receiving program gets the
> correct message size and only the first four bytes of the message.
>
> The next step I tried was to have each side close the socket after
> each transmission and open a new socket for each data chunk. This led
> to the same results as before.
>
> I am at a loss about what I am doing wrong after searching the web for
> information on bluetooth socket programming. Has Anyone seen this
> sort of problem before and if so what did you do to fix it? Thanks
> for your help.
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> that extends applications into web and mobile media. Attend the live
> webcast
> and join the prime developer group breaking into this new coding
> territory!
> http://sel.as-us.falkag.net/sel?cmdlnk&kid0944&bid$1720&dat1642
> _______________________________________________
> Bluez-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/bluez-users
>


Attachments:
(No filename) (1.79 kB)
(No filename) (2.44 kB)
Download all attachments