first commit
This commit is contained in:
@@ -0,0 +1,279 @@
|
||||
#include "utils.h"
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "cJSON.h"
|
||||
#include <time.h>
|
||||
|
||||
char header[1024] = {0};
|
||||
char send_request[1024] = {0};
|
||||
char send_end[1024] = {0};
|
||||
char http_boundary[64] = {0};
|
||||
|
||||
unsigned long get_file_size(const char *path) // 获取文件大小
|
||||
{
|
||||
unsigned long filesize = -1;
|
||||
struct stat statbuff;
|
||||
if (stat(path, &statbuff) < 0)
|
||||
{
|
||||
return filesize;
|
||||
}
|
||||
else
|
||||
{
|
||||
filesize = statbuff.st_size;
|
||||
}
|
||||
return filesize;
|
||||
}
|
||||
|
||||
int create_socket(const char *ip, const unsigned int port)
|
||||
{
|
||||
int cfd = -1;
|
||||
struct sockaddr_in s_addr, c_addr;
|
||||
cfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (-1 == cfd)
|
||||
{
|
||||
printf("socket create fail\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
bzero(&s_addr, sizeof(struct sockaddr_in));
|
||||
s_addr.sin_family = AF_INET;
|
||||
s_addr.sin_addr.s_addr = inet_addr(ip);
|
||||
s_addr.sin_port = htons(port);
|
||||
|
||||
if (-1 == connect(cfd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr)))
|
||||
{
|
||||
printf("conect failed\n");
|
||||
return -1;
|
||||
}
|
||||
return cfd;
|
||||
}
|
||||
|
||||
void close_socket(int sockfd)
|
||||
{
|
||||
if (sockfd < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
close(sockfd);
|
||||
}
|
||||
|
||||
int post_json(const char *ip, const unsigned int port, const char *host, const char *uri, cJSON *data)
|
||||
{
|
||||
int cfd = -1, recbytes = -1;
|
||||
struct sockaddr_in s_addr, c_addr;
|
||||
char buffer[1024 * 10] = {0};
|
||||
|
||||
cfd = create_socket(ip, port);
|
||||
if (cfd < 0)
|
||||
{
|
||||
printf("create sockfd failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *strs = cJSON_Print(data);
|
||||
|
||||
int totalsize = strlen(strs);
|
||||
unsigned long head_len = snprintf(header, 1024, HTTP_HEAD, uri, host, "Content-Type: application/json", totalsize); // 头信息
|
||||
totalsize += head_len;
|
||||
|
||||
char *request = (char *)malloc(totalsize); // 申请内存用于存放要发送的数据
|
||||
if (request == NULL)
|
||||
{
|
||||
printf("malloc request fail !\r\n");
|
||||
close_socket(cfd);
|
||||
return -1;
|
||||
}
|
||||
request[0] = '\0';
|
||||
strcat(request, header); // http头信息
|
||||
strcat(request, strs); // json数据
|
||||
free(strs);
|
||||
|
||||
if (-1 == write(cfd, request, totalsize))
|
||||
{
|
||||
printf("send http package fail!\r\n");
|
||||
close_socket(cfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (-1 == (recbytes = read(cfd, buffer, 10240)))
|
||||
{
|
||||
printf("read http ACK fail !\r\n");
|
||||
close_socket(cfd);
|
||||
return -1;
|
||||
}
|
||||
buffer[recbytes] = '\0';
|
||||
|
||||
printf("got response %s\n", buffer);
|
||||
|
||||
free(request);
|
||||
close_socket(cfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int post_data(const char *ip, const unsigned int port, char *host, char *uri, const char *filepath)
|
||||
{
|
||||
int cfd = -1, recbytes = -1;
|
||||
struct sockaddr_in s_addr, c_addr;
|
||||
char buffer[1024 * 10] = {0};
|
||||
|
||||
/*
|
||||
cfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (-1 == cfd) {
|
||||
printf("socket create fail\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
bzero(&s_addr, sizeof(struct sockaddr_in));
|
||||
s_addr.sin_family = AF_INET;
|
||||
s_addr.sin_addr.s_addr = inet_addr(ip);
|
||||
s_addr.sin_port = htons(port);
|
||||
|
||||
if (-1 == connect(cfd, (struct sockaddr*)&s_addr, sizeof(struct sockaddr))) {
|
||||
printf("conect failed\n");
|
||||
return -1;
|
||||
}
|
||||
*/
|
||||
|
||||
cfd = create_socket(ip, port);
|
||||
if (cfd < 0)
|
||||
{
|
||||
printf("create sockfd failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
long long int timestamp;
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
timestamp = (long long int)tv.tv_sec * 1000 + tv.tv_usec;
|
||||
snprintf(http_boundary, 64, "---------------------------%lld", timestamp);
|
||||
|
||||
unsigned long totalsize = 0;
|
||||
unsigned long filesize = get_file_size(filepath); // 文件大小
|
||||
unsigned long request_len = snprintf(send_request, 1024, UPLOAD_REQUEST, http_boundary, filepath); // 请求信息
|
||||
unsigned long end_len = snprintf(send_end, 1024, "\r\n--%s--\r\n", http_boundary); // 结束信息
|
||||
totalsize = filesize + request_len + end_len;
|
||||
|
||||
char application[128] = {0};
|
||||
snprintf(application, 128, "multipart/form-data; boundary=%s", http_boundary);
|
||||
unsigned long head_len = snprintf(header, 1024, HTTP_HEAD, uri, host, application, totalsize); // 头信息
|
||||
totalsize += head_len;
|
||||
|
||||
char *request = (char *)malloc(totalsize); // 申请内存用于存放要发送的数据
|
||||
if (request == NULL)
|
||||
{
|
||||
printf("malloc request fail !\r\n");
|
||||
close_socket(cfd);
|
||||
return -1;
|
||||
}
|
||||
request[0] = '\0';
|
||||
|
||||
strcat(request, header); // http头信息
|
||||
strcat(request, send_request); // 文件图片请求信息
|
||||
FILE *fp = fopen(filepath, "rb+"); // 打开要上传的图片
|
||||
if (fp == NULL)
|
||||
{
|
||||
printf("open file fail!\r\n");
|
||||
close_socket(cfd);
|
||||
return -1;
|
||||
}
|
||||
int readbyte = fread(request + head_len + request_len, 1, filesize, fp); // 读取上传的图片信息
|
||||
if (readbyte < 1024) // 小于1024个字节 则认为图片有问题
|
||||
{
|
||||
printf("Read picture data fail!\r\n");
|
||||
close_socket(cfd);
|
||||
return -1;
|
||||
}
|
||||
memcpy(request + head_len + request_len + filesize, send_end, end_len); // http结束信息
|
||||
|
||||
/********* 发送http 请求 ***********/
|
||||
if (-1 == write(cfd, request, totalsize))
|
||||
{
|
||||
printf("send http package fail!\r\n");
|
||||
close_socket(cfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (-1 == (recbytes = read(cfd, buffer, 10240)))
|
||||
{
|
||||
printf("read http ACK fail !\r\n");
|
||||
close_socket(cfd);
|
||||
return -1;
|
||||
}
|
||||
buffer[recbytes] = '\0';
|
||||
|
||||
printf("got response %s\n", buffer);
|
||||
|
||||
free(request);
|
||||
fclose(fp);
|
||||
close_socket(cfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// outbuf should be larger than 15 digits 20060102150405\0,
|
||||
// 返回这个时间片里,还剩下多少秒时间
|
||||
int get_current_time_gap(current_time_gap_t *gap)
|
||||
{
|
||||
time_t now;
|
||||
|
||||
now = time(NULL);
|
||||
int resgap = now % TIMEGAP;
|
||||
|
||||
gap->now = now;
|
||||
gap->now_gap = now - resgap;
|
||||
gap->gap_remaining = TIMEGAP - resgap;
|
||||
gap->next_gap = now - resgap + TIMEGAP;
|
||||
return 0;
|
||||
}
|
||||
|
||||
double current_time_stamp()
|
||||
{
|
||||
time_t now;
|
||||
now = time(NULL);
|
||||
return double(now);
|
||||
}
|
||||
|
||||
uint64_t convert_time_to_name(time_t now)
|
||||
{
|
||||
struct tm now_time;
|
||||
localtime_r(&now, &now_time);
|
||||
|
||||
uint64_t res = ((uint64_t)(1900 + now_time.tm_year)) * 1e10 + ((uint64_t)(1 + now_time.tm_mon)) * 1e8 +
|
||||
((uint64_t)now_time.tm_mday) * 1e6 + ((uint64_t)now_time.tm_hour) * 1e4 +
|
||||
((uint64_t)now_time.tm_min) * 1e2 + (uint64_t)now_time.tm_sec;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
int read_data_from_file(const char *path, char **out_data)
|
||||
{
|
||||
FILE *fp = fopen(path, "rb");
|
||||
if(fp == NULL) {
|
||||
printf("fopen %s fail!\n", path);
|
||||
return -1;
|
||||
}
|
||||
fseek(fp, 0, SEEK_END);
|
||||
int file_size = ftell(fp);
|
||||
char *data = (char *)malloc(file_size+1);
|
||||
data[file_size] = 0;
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
if(file_size != fread(data, 1, file_size, fp)) {
|
||||
printf("fread %s fail!\n", path);
|
||||
free(data);
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
if(fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
*out_data = data;
|
||||
return file_size;
|
||||
}
|
||||
Reference in New Issue
Block a user