问题描述
程序如下,程序在Ubuntu上编译后能运行,但是用arm-linux-server编译后移植到ok6410上,运行后报buserror错误不对数据库操作的话,程序又是可以运行的,请教一下大神如何修改?具体点,本人菜鸟。应该是地址没有对齐的读写问题。#include<netinet/in.h>#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<sqlite3.h>#defineSERVER_PORT8000//服务端口#defineBUFFER_SIZE1024//缓冲区大小#defineFILE_NAME_MAX_SIZE512//文件名大小intselect_callback(void*data,intcol_count,char**col_values,char**col_Name);intsqlite3_z(intargc,char*argv[]);intmain(){//数据库函数char*argv[]={"","",""};sqlite3_z(3,argv);//声明并初始化一个客户端的socket地址结构structsockaddr_inclient_addr;bzero(&client_addr,sizeof(client_addr));client_addr.sin_family=AF_INET;//TCP/IP协议族client_addr.sin_addr.s_addr=htons(INADDR_ANY);//iner_addr("0.0.0.0")client_addr.sin_port=htons(0);//socket对应的端口//创建socket,若成功,返回socket描述符//AF_INET表示采用TCP/IP协议族//SOCK_STREAM表示采用TCP协议//0是通常的默认情况intclient_socket_fd=socket(AF_INET,SOCK_STREAM,0);if(client_socket_fd<0){perror("CreateSocketFailed:");exit(1);}//绑定客户端的socket和客户端的socket地址结构非必需if(-1==(bind(client_socket_fd,(structsockaddr*)&client_addr,sizeof(client_addr)))){perror("ClientBindFailed:");exit(1);}//声明一个服务器端的socket地址结构,并用服务器那边的IP地址及端口对其进行初始化,用于后面的连接structsockaddr_inserver_addr;bzero(&server_addr,sizeof(server_addr));server_addr.sin_family=AF_INET;if(inet_pton(AF_INET,"127.0.0.1",&server_addr.sin_addr)==0){perror("ServerIPAddressError:");exit(1);}server_addr.sin_port=htons(SERVER_PORT);socklen_tserver_addr_length=sizeof(server_addr);//向服务器发起连接,连接成功后client_socket_fd代表了客户端和服务器的一个socket连接if(connect(client_socket_fd,(structsockaddr*)&server_addr,server_addr_length)<0)//发起连接{perror("CanNotConnectToServerIP:");exit(0);}//输入要发送的文件名charfile_name[FILE_NAME_MAX_SIZE+1]={"test.csv"};printf("sendFileName:test.csvt");charbuffer[BUFFER_SIZE];bzero(buffer,BUFFER_SIZE);strncpy(buffer,file_name,strlen(file_name)>BUFFER_SIZE?BUFFER_SIZE:strlen(file_name));//向服务器发送buffer中的数据if(send(client_socket_fd,buffer,BUFFER_SIZE,0)<0){perror("SendFileNameFailed:");exit(1);}//打开文件,读取数据FILE*fp=fopen(file_name,"r");if(NULL==fp){printf("File:%sNotFoundn",file_name);}else{bzero(buffer,BUFFER_SIZE);intlength=0;//每读取一段数据,便将其发送给服务器,循环直到文件读完为止while((length=fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0){if(send(client_socket_fd,buffer,length,0)<0){printf("SendFile:%sFailed./n",file_name);break;}bzero(buffer,BUFFER_SIZE);}//关闭文件fclose(fp);remove("test.csv");printf("File:%sTransferSuccessful!n",file_name);}close(client_socket_fd);return0;}intsqlite3_z(intargc,char*argv[]){char*pErrMsg=0;//错误存放地址intresult=0;//连接数据库sqlite3*db=0;intret=sqlite3_open("./test.db",&db);//打开数据库if(ret!=SQLITE_OK){fprintf(stderr,"无法打开数据库:%s",sqlite3_errmsg(db));return(1);}printf("数据库连接成功!n");if(freopen("./test.csv","w",stdout)==NULL)//重定向写文件fprintf(stderr,"errorredirectingstdoutn");sqlite3_exec(db,"select*fromtest_1;",select_callback,0,&pErrMsg);fclose(stdout);freopen("/dev/tty","w",stdout);sqlite3_close(db);db=0;printf("数据库关闭成功!n");}//数据库回调函数intselect_callback(void*data,intcol_count,char**col_values,char**col_Name){//每条记录回调一次该函数,有多少条就回调多少次,发送11条数据换行inti,j=0;for(i=0;i<col_count;i++,j++){if(j<10)printf("%s,",col_values[i]==0?"NULL":col_values[i]);else{j=0;printf("%sn",col_values[i]==0?"NULL":col_values[i]);}}return0;}