Re: Programming in C with Bluetooth Sockets



Re: 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 = 0;
       char * buf;
       struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
       int opt = sizeof(rem_addr);
       unsigned int max_count;

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

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

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

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

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

       //attempt to bind the socket for a maximum number of attempts
       for( count = 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 == 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 = AF_BLUETOOTH;
       rem_addr.rc_bdaddr = *BDADDR_ANY;
       rem_addr.rc_channel = (uint8_t) RFCOMM_CHANNEL;

       client = 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 >= 0)
       {
               count = 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 = accept(s, (struct sockaddr*)&rem_addr, &opt) ) >= 0 )
       {
               bytes_read = read(client, buf + count, sizeof(buf) - count);

               fprintf(stderr, "got %d bytes this time\n", bytes_read);
               count += 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 = { 0 };
       int s, status, count = 0, fd;
       char* dest = tMAC;
       struct stat *stats;
       unsigned int len = 0;
       char* buf;

       //attempt to allocate a socket
       s = 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 = AF_BLUETOOTH;
       addr.rc_channel = (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 = connect(s, (struct sockaddr *)&addr, sizeof(addr));
       for(count = 0; status != 0 && count < MAX_ATTEMPTS;)
       {
               perror("retrying connection: ");

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

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

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

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

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

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

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

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

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

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

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

                               status = 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 through";
                       }

                       //write the next message segment
                       fprintf(stderr,"data read '%s'\n", buf);
                       count = 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", count);
               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&kid0709&bid&3057&dat1642

Re: 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 = 0;
       char * buf;
       struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
       int opt = sizeof(rem_addr);
       unsigned int max_count;

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

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

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

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

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

       //attempt to bind the socket for a maximum number of attempts
       for( count = 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 == 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 = AF_BLUETOOTH;
       rem_addr.rc_bdaddr = *BDADDR_ANY;
       rem_addr.rc_channel = (uint8_t) RFCOMM_CHANNEL;

       client = 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 >= 0)
       {
               count = 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 = accept(s, (struct sockaddr*)&rem_addr, &opt) ) >= 0 )
       {
               bytes_read = read(client, buf + count, sizeof(buf) - count);

               fprintf(stderr, "got %d bytes this time\n", bytes_read);
               count += 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 = { 0 };
       int s, status, count = 0, fd;
       char* dest = tMAC;
       struct stat *stats;
       unsigned int len = 0;
       char* buf;

       //attempt to allocate a socket
       s = 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 = AF_BLUETOOTH;
       addr.rc_channel = (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 = connect(s, (struct sockaddr *)&addr, sizeof(addr));
       for(count = 0; status != 0 && count < MAX_ATTEMPTS;)
       {
               perror("retrying connection: ");

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

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

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

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

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

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

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

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

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

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

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

                               status = 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 through";
                       }

                       //write the next message segment
                       fprintf(stderr,"data read '%s'\n", buf);
                       count = 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", count);
               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&kid0709&bid&3057&dat1642
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值