C语言socket 蓝牙文件传输

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sinat_31741803/article/details/50478095

I-client

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define DEBUG

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>




#include "bluetooth/bluetooth.h"
#include "bluetooth/hci.h"
#include "bluetooth/hci_lib.h"
#include "bluetooth/l2cap.h"
#include "bluetooth/sdp.h"
#include "bluetooth/sdp_lib.h"
//#include "bluetooth/sco.h"

//#include "bluetooth/tcs.h"




/********* for debug***********************************/
#ifdef DEBUG
 void debug(char * str)
 {
    printf(str);
 }
#else
#define debug(str)
#endif

#define MAX_LEN 32
#define FILE_NAME_MAX_SIZE         512 
#define BUFFER_SIZE                1024  
/*GLOBAL VARIABLE***********************************/
struct sockaddr_l2 addr = {0};        //for l2cap socket
int s;
int status;
char *dest;
int bytes_read;
unsigned char buf[1024];



void baswap(bdaddr_t *dst, const bdaddr_t *src)
{
        register unsigned char *d = (unsigned char *) dst;
        register const unsigned char *s = (const unsigned char *) src;
        register int i;
        for (i = 0; i < 6; i++)
                d[i] = s[5-i];
}

int str2ba(const char *str, bdaddr_t *ba)
{
        uint8_t b[6];
        const char *ptr = str;
        int i;
        for (i = 0; i < 6; i++)
        {
                b[i] = (uint8_t) strtol(ptr, NULL, 16);
                if (i != 5 && !(ptr = strchr(ptr, ':')))
                        ptr = ":00:00:00:00:00";
                ptr++;
        }
        baswap(ba, (bdaddr_t *) b);
        return 0;
}



void tcs_dt_init(int argc,char*argv[])
{
   char c;
   int nwrite;
   if(argc == 2)
   {
    dest = argv[1];
   }
   else
   {
    printf("usage::clinet addr\n");
    exit(1);
   }
      //unsigned char* addr;
      debug("\n||==========  get tcs_dt initialized =============||\n");

     debug("(1) dt opening l2cap socket...\n");
     //allocate socket
     printf("s = %d\n",s);
     s = socket(PF_BLUETOOTH,SOCK_SEQPACKET,BTPROTO_L2CAP);  
     printf("s = %d\n",s);
   if(s < 0)
   {
     printf("create socket in dt failed...\n");
     exit(1);
   }
   else 
   {
    debug(" dt open socket success!\n");
    }

   //bind socket to port 0x1001 of the first available bluetooth adaptor
   addr.l2_family = AF_BLUETOOTH;
   addr.l2_psm = htobs(0x1001);
   str2ba(dest,&addr.l2_bdaddr);

   debug("(2) connecting to gateway...\n");
   printf("status = %d \n",status);
   status = connect(s,(struct sockaddr*)&addr, sizeof(addr));
    printf("status = %d \n",status);
   if(status < 0)
     {
       printf("connect to gateway failed...\n");
       exit(1);
     }
   else
     {
       debug("connect success!\n");
     }

   c= 0x41;

   //debug("||==========initialize tcs_dt end ================||\n\n");
  sleep(1);

 /* while(1)
  {
    sleep(1);
    nwrite = write(s,"1234",4); 
    printf("%d bytes written\n",nwrite);

  }*/

   memset(buf,0,sizeof(buf));
   char file_name[FILE_NAME_MAX_SIZE+1]="input.txt";
   nwrite = write(s,file_name,8);

   memset(buf,0,sizeof(buf));
   FILE *fp = fopen(file_name,"r");
   int file_block_length = 0;
   while( (file_block_length = fread(buf,sizeof(buf),1,fp)) > 0)
    {
       printf("file_block_length = %d \n",file_block_length);
        if(send(s,buf,file_block_length,0) < 0)
        {
            printf("Send file : \t  Failed! \n");
                        break;  
        }
               memset(buf,0,sizeof(buf));
    }
            fclose(fp);
        printf("File :\t %s Transfer Finished!\n");
}



void dail()
{
    char buff[20];

    char c;
    int i;

    while(1)
    {
        memset(buff,0,20);
        write(s,"123",3);
    }

}




int main(int argc, char*argv[])
{ 
   int fd;

   debug("begining..now.\n");

   fd=open_port(fd,3);
   set_opt(fd,115200, 8, 'N', 1);

   tcs_dt_init(argc,argv); 
   dail();
   while(1);
   return 0;
}


II-server:

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define DEBUG

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/un.h>
#include <sys/socket.h>

#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <pthread.h> /* Mluti-programming definitions*/

#include "bluetooth/bluetooth.h"
#include "bluetooth/hci.h"
#include "bluetooth/hci_lib.h"
#include "bluetooth/l2cap.h"
#include "bluetooth/sdp.h"
#include "bluetooth/sdp_lib.h"
#include "bluetooth/sco.h"
#include "bluetooth/tcs.h"

#define MAX_LEN 32


/********* for debug***********************************/
#ifdef DEBUG
 void debug(char * str)
 {
    printf(str);
 }
#else
#define debug(str)
#endif

#define BUFFER_SIZE 512

void baswap(bdaddr_t *dst, const bdaddr_t *src)
{
    register unsigned char *d = (unsigned char *) dst;
    register const unsigned char *s = (const unsigned char *) src;
    register int i;
    for (i = 0; i < 6; i++)
        d[i] = s[5-i];
}


int ba2str(const bdaddr_t *ba, char *str)
{
    uint8_t b[6];

    baswap((bdaddr_t *) b, ba);
    return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
        b[0], b[1], b[2], b[3], b[4], b[5]);
}


/*****************GLOBAL VARIABLE***********************************/
//unsigned char option;
struct sockaddr_l2 loc_addr={0}, rem_addr = {0};    // for l2cap
int s;
int result;
int client;
int bytes_read;
char buf[30];
int opt = sizeof(rem_addr);
char addr[18];

int fd;

/*==============================================================================*
 *          EXPORT FUNCITON DEFINATION  (FOR GATEWAY)                                               *
 *==============================================================================*/
 void tcs_init(void)
 {
      //unsigned char* addr;
      debug("\n||==========  get tcs initialized =============||\n");

      debug("(1) tcs init: opening l2cap socket...\n");
     //allocate socket
     s = socket(PF_BLUETOOTH,SOCK_SEQPACKET,BTPROTO_L2CAP);  
   if(s < 0)
   {
     printf("create socket in gateway failed...\n");
     exit(1);
   }
   else 
   {
    debug("    open socket success!\n");
    }

   //bind socket to port 0x1001 of the first available bluetooth adaptor
   loc_addr.l2_family = AF_BLUETOOTH;
   loc_addr.l2_psm = htobs(0x1001);
   loc_addr.l2_bdaddr = *BDADDR_ANY;

   //ba2str(&loc_addr.l2_bdaddr,addr);

   ba2str(&(*BDADDR_ANY),addr);
   printf("local address is %s\n",addr);
   debug("(2) binding socket...\n");
   result = bind(s,(struct sockaddr*)&loc_addr, sizeof(loc_addr));
   if(result < 0)
     {
       printf("bind socket error...\n");
       close(s);
       exit(1);
     }
   else
     {
       debug("    binding socket success!\n");
     }
   printf("(3) listening..\n");
   result = listen(s,1);
   if(result < 0)
     {
       debug("listening error...\n");
       exit(1);
     }
   else
     {
       debug("    requested!\n");
     }
   debug("||==========initialize end ================||\n\n");
  // gw_add_sco(); 
}

int  gw_process()
{ 
   char c;
//   debuggateway proceeding...\n\n");
   printf("1)accepting...\n");
   client = accept(s,(struct sockaddr*)&rem_addr,&opt);
   if(client < 0)
   {
      printf("accept error\n");
      exit(1);
   }
   else
   {
      printf("accept succes!\n\n");
   }
   ba2str(&rem_addr.l2_bdaddr,buf);
   printf("remote address is %s \n",buf);

   memset(buf,0,sizeof(buf));
  /* while(1)
   {

      bytes_read = read(client,buf,sizeof(buf));
      if(bytes_read > 0)
      {
         printf("buffer_received :%s\n",buf);
         memset(buf,0,sizeof(buf) );

      }  
    }
    */



    memset(buf,0,sizeof(buf));  

    int length = 0;
    int len = 0;
    char bufm[20];
    char file_name[20];
     len = recv(client,bufm ,sizeof(bufm),0);
    if(len < 0)
         printf("receive file_name error");
     strncpy(file_name,bufm,len);
     printf("%s",file_name);
     FILE *fp;
     if((fp = fopen(file_name,"w++"))== NULL)
      {
          printf("error");
          return -1;
      } 
     sleep(1);


  while(length = recv(client, buf, sizeof(buf), 0))  
    {  
        if (length < 0)  
        {  
            printf("Recieve Data From Server Failed!\n");  
              break;
        }  

        int write_length = fwrite(buf, sizeof(buf), length, fp);  
        if (write_length < length)  
        {  
            printf("File:\t Write Failed!\n");  
            break;  
        }  
        memset(buf,0,sizeof(buf));  
    }  

    close(client);
    close(s);  
  return 0;  
}


int main()
{  
   debug("begining..now.\n");

   fd=open_port(fd,3);
   set_opt(fd,115200, 8, 'N', 1);

   tcs_init();
   gw_process();

   return 0;
}
展开阅读全文

没有更多推荐了,返回首页