IOS — 关于Socket传输文件需要自定义延时或者包大小的情况
1. 首先导入头文件
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/stat.h>
2。定义全局变量
int delay;
int sockfd;
char buffer[1024];
struct sockaddr_in sa;
#define sigbuf_lens 512 // 设置包的大小
3. 在获取到是其他设备的IP地址之后 复制一下代码
UrlStr = listarr[0]; // UrlStr为获取到的服务器的地址
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber; // 端口号
portnumber = 80;
host = (__bridge struct hostent *)(UrlStr);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
exit(1);
}
NSString *addrString = UrlStr;
struct in_addr addr;
if (inet_aton([addrString UTF8String], &addr) != 0) {
uint32_t ip = ntohl(addr.s_addr);
NSLog(@"%08x", ip);
} else {
NSLog(@"invalid address");
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr = addr;
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct
sockaddr))==-1)
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}
int len = sizeof(sa);
if(!getsockname(sockfd, &sa, &len))//ªÒ»°±æµÿIPµÿ÷∑
{
printf("Local IP£∫%s\n", inet_ntoa(sa.sin_addr));
}
const char * filePathChar = [_upFilePath UTF8String];//_upFilePath 为你需要上传的文件的路径
uint8_t* upDataFilePath = (uint8_t*)filePathChar;
sys_update(upDataFilePath);// 调取升级方法
4. 发送文件的方法
void sys_update(uint8_t *file_name)
{
printf("Start to update:%s, delay:%dmS, lens:%d\n", file_name, delay, sigbuf_lens);
/*sprintf(buffer, CMD_GET_INFO, inet_ntoa(sa.sin_addr));
if(write(sockfd, buffer, strlen(buffer))) {
fprintf(stderr,"Read Error:%s\n",strerror(errno));
exit(1);
}*/
/*if((nbytes=read(sockfd,buffer,1024))==-1)
{
fprintf(stderr,"Read Error:%s\n",strerror(errno));
exit(1);
}
buffer[nbytes]='\0';
uint8_t *index = strstr(buffer, "iot_version")+1;
if( < 0)*/
FILE *fil;
fil = fopen(file_name, "r");//¥Úø™…˝º∂Œƒº˛
if(fil == NULL)
{
printf("open Image FILE fail !! \n");
exit(0);
}
struct stat statbuf;
stat(file_name, &statbuf);
printf("Get File leng = %lld\n", statbuf.st_size);
sprintf(buffer, CMD_UPDATE_START, inet_ntoa(sa.sin_addr));
if(write(sockfd, buffer, strlen(buffer)) < 0){
fprintf(stderr,"CMD_UPDATE_START Error:%s\n",strerror(errno));
exit(1);
}
usleep(1000000);
printf("Start Update\n");
sprintf(buffer, CMD_UPDATE, inet_ntoa(sa.sin_addr), statbuf.st_size);
if(write(sockfd, buffer, strlen(buffer)) < 0){
fprintf(stderr,"CMD_UPDATE Error:%s\n",strerror(errno));
exit(1);
}
sleep(1);
uint32_t i;
for(i=0;i<statbuf.st_size/sigbuf_lens;i++)
{
if( fread(buffer, sigbuf_lens, 1, fil) <= 0)
{
printf("Cannot Read the File!\n");
exit(0);
}
if(write(sockfd, buffer, sigbuf_lens) < 0){
fprintf(stderr,"UPDATE DATA Error:%s\n",strerror(errno));
exit(1);
}
//if(i%10 == 0) {
printf("\rUpdateing %d/%lld", sigbuf_lens*i, statbuf.st_size);
printf(" %lld%%", (i*100)/(statbuf.st_size/sigbuf_lens));
fflush(stdout);
}
usleep(100 * 1000);// 设置数据包之间延时的时间
}
if(statbuf.st_size%sigbuf_lens)//∑¢ÀÕŒƒº˛ƒ©Œ≤≤ø∑÷{
if( fread(buffer, statbuf.st_size%sigbuf_lens, 1, fil) <= 0) {
printf("Cannot Read the File!\n");
exit(0);
}
if(write(sockfd, buffer, statbuf.st_size%sigbuf_lens) < 0){
fprintf(stderr,"UODATE DATA Error:%s\n",strerror(errno));
exit(1);
}
}
//printf("\rUpdateing %d/%d", statbuf.st_size, statbuf.st_size);
//printf(" %d%%\n", 100);
printf("\n");
fflush(stdout);
sleep(1);
sprintf(buffer, CMD_UPDATE_REBOOT, inet_ntoa(sa.sin_addr));
if(write(sockfd, buffer, strlen(buffer)) < 0){
fprintf(stderr,"CMD_UPDATE_REBOOT Error:%s\n",strerror(errno));
exit(1);
}
printf("Update End\n");
}
// 发送完成 搞定