1
0
mirror of https://github.com/fumiama/simple-kanban.git synced 2026-07-02 09:00:27 +08:00

修正大端系统下的错误

This commit is contained in:
fumiama
2021-05-07 22:53:18 +08:00
parent a07876cb33
commit 77fc742f18
4 changed files with 24 additions and 13 deletions

View File

@@ -3,7 +3,7 @@ project(simple-kanban)
include(TestBigEndian) include(TestBigEndian)
test_big_endian(isBigEndian) test_big_endian(isBigEndian)
if (${isBigEndian}) if (${isBigEndian})
set ( WORDS_BIGENDIAN 1 ) add_definitions(-DWORDS_BIGENDIAN)
endif() endif()
#add_definitions(-DLISTEN_ON_IPV6) #add_definitions(-DLISTEN_ON_IPV6)

View File

@@ -72,10 +72,10 @@ make
## 0. 启动程序 ## 0. 启动程序
```bash ```bash
./simple-kanban-client 127.0.0.1 7777 [-r] ./simple-kanban-client 127.0.0.1 7777
``` ```
接下来即可按照上面的交互流程开始使用。其中末尾的`-r`为可选项(程序只判断参数个数所以请务必放到末尾),表示以与本机相反的顺序发送文件长度。 接下来即可按照上面的交互流程开始使用。
## 1. 发送命令 ## 1. 发送命令

View File

@@ -43,7 +43,7 @@ off_t size_of_file(const char* fname) {
else return -1; else return -1;
} }
int main(int argc,char *argv[]) { //usage: ./client host port [-r] int main(int argc,char *argv[]) { //usage: ./client host port
ssize_t numbytes; ssize_t numbytes;
puts("break!"); puts("break!");
while((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1); while((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1);
@@ -75,7 +75,6 @@ int main(int argc,char *argv[]) { //usage: ./client host port [-r]
off_t len = 0; off_t len = 0;
file_size = (uint32_t)size_of_file(buf); file_size = (uint32_t)size_of_file(buf);
#if __APPLE__ #if __APPLE__
if(argc == 4) file_size = htonl(file_size);
struct iovec headers; struct iovec headers;
headers.iov_base = &file_size; headers.iov_base = &file_size;
headers.iov_len = sizeof(uint32_t); headers.iov_len = sizeof(uint32_t);
@@ -86,10 +85,7 @@ int main(int argc,char *argv[]) { //usage: ./client host port [-r]
if(!sendfile(fileno(fp), sockfd, 0, &len, &hdtr, 0)) puts("Send file success."); if(!sendfile(fileno(fp), sockfd, 0, &len, &hdtr, 0)) puts("Send file success.");
else puts("Send file error."); else puts("Send file error.");
#else #else
if(argc == 4) { send(sockfd, &file_size, sizeof(uint32_t), 0);
uint32_t fs = htonl(file_size);
send(sockfd, &fs, sizeof(uint32_t), 0);
} else send(sockfd, &file_size, sizeof(uint32_t), 0);
if(!sendfile(sockfd, fileno(fp), &len, file_size)) puts("Send file success."); if(!sendfile(sockfd, fileno(fp), &len, file_size)) puts("Send file success.");
else puts("Send file error."); else puts("Send file error.");
#endif #endif

View File

@@ -145,9 +145,15 @@ int send_all(char* file_path, THREADTIMER *timer) {
re = !sendfile(fileno(fp), timer->accept_fd, 0, &len, &hdtr, 0); re = !sendfile(fileno(fp), timer->accept_fd, 0, &len, &hdtr, 0);
if(!re) perror("Sendfile"); if(!re) perror("Sendfile");
#else #else
#if WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
uint32_t little_fs = htonl(file_size); uint32_t little_fs;
send(timer->accept_fd, &little_fs sizeof(uint32_t), 0); char* q = (char*)(&little_fs);
char* p = (char*)(&file_size);
q[0] = p[3];
q[1] = p[2];
q[2] = p[1];
q[3] = p[0];
send(timer->accept_fd, &little_fs, sizeof(uint32_t), 0);
#else #else
send(timer->accept_fd, &file_size, sizeof(uint32_t), 0); send(timer->accept_fd, &file_size, sizeof(uint32_t), 0);
#endif #endif
@@ -219,7 +225,16 @@ int s2_set(THREADTIMER *timer) {
int s3_set_data(THREADTIMER *timer) { int s3_set_data(THREADTIMER *timer) {
timer->status = 0; timer->status = 0;
#ifdef WORDS_BIGENDIAN
uint32_t file_size;
char* p = (char*)(&file_size);
p[0] = timer->data[3];
p[1] = timer->data[2];
p[2] = timer->data[1];
p[3] = timer->data[0];
#else
uint32_t file_size = *(uint32_t*)(timer->data); uint32_t file_size = *(uint32_t*)(timer->data);
#endif
printf("Set data size: %u\n", file_size); printf("Set data size: %u\n", file_size);
int is_first_data = 0; int is_first_data = 0;
if(timer->numbytes == sizeof(uint32_t)) { if(timer->numbytes == sizeof(uint32_t)) {