* [Bluez-users] Programming in C with Bluetooth Sockets @ 2006-04-19 2:25 Eric Munson 2006-04-19 4:18 ` Sowmya Gattupalli 0 siblings, 1 reply; 9+ messages in thread From: Eric Munson @ 2006-04-19 2:25 UTC (permalink / raw) To: bluez-users 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 Bluez-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bluez-users ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Bluez-users] Programming in C with Bluetooth Sockets 2006-04-19 2:25 [Bluez-users] Programming in C with Bluetooth Sockets Eric Munson @ 2006-04-19 4:18 ` Sowmya Gattupalli 2006-04-19 15:04 ` Eric Munson 0 siblings, 1 reply; 9+ messages in thread From: Sowmya Gattupalli @ 2006-04-19 4:18 UTC (permalink / raw) To: bluez-users [-- Attachment #1: Type: text/plain, Size: 1874 bytes --] 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 <ebmunson@gmail.com> 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&kid\x110944&bid$1720&dat\x121642 > _______________________________________________ > Bluez-users mailing list > Bluez-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/bluez-users > [-- Attachment #2: Type: text/html, Size: 2510 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Bluez-users] Programming in C with Bluetooth Sockets 2006-04-19 4:18 ` Sowmya Gattupalli @ 2006-04-19 15:04 ` Eric Munson 2006-04-19 16:50 ` Sowmya Gattupalli 2006-04-19 16:56 ` Albert Huang 0 siblings, 2 replies; 9+ messages in thread From: Eric Munson @ 2006-04-19 15:04 UTC (permalink / raw) To: bluez-users 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 <sowmya.gattupalli@gmail.com> 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 <ebmunson@gmail.com> 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 > Bluez-users@lists.sourceforge.net > 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 Bluez-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bluez-users ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Bluez-users] Programming in C with Bluetooth Sockets 2006-04-19 15:04 ` Eric Munson @ 2006-04-19 16:50 ` Sowmya Gattupalli 2006-04-19 16:56 ` Albert Huang 1 sibling, 0 replies; 9+ messages in thread From: Sowmya Gattupalli @ 2006-04-19 16:50 UTC (permalink / raw) To: bluez-users [-- Attachment #1: Type: text/plain, Size: 3306 bytes --] 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 <ebmunson@gmail.com> 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 <sowmya.gattupalli@gmail.com> 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 <ebmunson@gmail.com> 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&kid\x110944&bid$1720&dat\x121642 > > _______________________________________________ > > Bluez-users mailing list > > Bluez-users@lists.sourceforge.net > > 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&kid\x120709&bid&3057&dat\x121642 > _______________________________________________ > Bluez-users mailing list > Bluez-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/bluez-users > [-- Attachment #2: Type: text/html, Size: 4540 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Bluez-users] Programming in C with Bluetooth Sockets 2006-04-19 15:04 ` Eric Munson 2006-04-19 16:50 ` Sowmya Gattupalli @ 2006-04-19 16:56 ` Albert Huang 2006-04-19 18:26 ` Eric Munson 1 sibling, 1 reply; 9+ messages in thread From: Albert Huang @ 2006-04-19 16:56 UTC (permalink / raw) To: bluez-users [-- Attachment #1: Type: text/plain, Size: 3176 bytes --] 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 <ebmunson@gmail.com> 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 <sowmya.gattupalli@gmail.com> 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 <ebmunson@gmail.com> 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&kid\x110944&bid$1720&dat\x121642 > > _______________________________________________ > > Bluez-users mailing list > > Bluez-users@lists.sourceforge.net > > 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&kid\x120709&bid&3057&dat\x121642 > _______________________________________________ > Bluez-users mailing list > Bluez-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/bluez-users > [-- Attachment #2: Type: text/html, Size: 4400 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Bluez-users] Programming in C with Bluetooth Sockets 2006-04-19 16:56 ` Albert Huang @ 2006-04-19 18:26 ` Eric Munson 2006-04-19 18:32 ` Albert Huang 0 siblings, 1 reply; 9+ messages in thread From: Eric Munson @ 2006-04-19 18:26 UTC (permalink / raw) To: bluez-users 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 Bluez-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bluez-users ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Bluez-users] Programming in C with Bluetooth Sockets 2006-04-19 18:26 ` Eric Munson @ 2006-04-19 18:32 ` Albert Huang 2006-04-19 20:41 ` Eric Munson 0 siblings, 1 reply; 9+ messages in thread From: Albert Huang @ 2006-04-19 18:32 UTC (permalink / raw) To: bluez-users [-- Attachment #1: Type: text/plain, Size: 245 bytes --] > 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 [-- Attachment #2: Type: text/html, Size: 497 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Bluez-users] Programming in C with Bluetooth Sockets 2006-04-19 18:32 ` Albert Huang @ 2006-04-19 20:41 ` Eric Munson 2006-04-20 6:46 ` Eric Munson 0 siblings, 1 reply; 9+ messages in thread From: Eric Munson @ 2006-04-19 20:41 UTC (permalink / raw) To: bluez-users 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 <albert@csail.mit.edu> 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 Bluez-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bluez-users ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Bluez-users] Programming in C with Bluetooth Sockets 2006-04-19 20:41 ` Eric Munson @ 2006-04-20 6:46 ` Eric Munson 0 siblings, 0 replies; 9+ messages in thread From: Eric Munson @ 2006-04-20 6:46 UTC (permalink / raw) To: bluez-users That did solve the problem, thanks again for catching such a silly mistake. Eric On 4/19/06, Eric Munson <ebmunson@gmail.com> 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 <albert@csail.mit.edu> 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 Bluez-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bluez-users ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2006-04-20 6:46 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2006-04-19 2:25 [Bluez-users] Programming in C with Bluetooth Sockets Eric Munson 2006-04-19 4:18 ` Sowmya Gattupalli 2006-04-19 15:04 ` Eric Munson 2006-04-19 16:50 ` Sowmya Gattupalli 2006-04-19 16:56 ` Albert Huang 2006-04-19 18:26 ` Eric Munson 2006-04-19 18:32 ` Albert Huang 2006-04-19 20:41 ` Eric Munson 2006-04-20 6:46 ` Eric Munson
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox