Re: Programming in C with Bluetooth Sockets

来源:互联网 发布:薛之谦 做作 知乎 编辑:程序博客网 时间:2024/06/06 01:44


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 creatingsocket: ");                                               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, Itried %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 byteswere 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 aftereach 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 easierDownload IBM WebSphere Application Server v.1.0.1 based on Apache Geronimohttp://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 creatingsocket: ");                                               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, Itried %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 byteswere 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 aftereach 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 easierDownload IBM WebSphere Application Server v.1.0.1 based on Apache Geronimohttp://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
0 0
原创粉丝点击