diff --git a/CMakeLists.txt b/CMakeLists.txt index e8b4552..0147d1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,13 @@ project(simple-kanban) -add_executable(simple-kanban server.c) # 生成可执行文件 -add_executable(simple-kanban-client client.c) # 生成可执行文件 -target_link_libraries( # 目标库 - simple-kanban - - # 目标库需要链接的库 - pthread) -target_link_libraries( # 目标库 - simple-kanban-client - - # 目标库需要链接的库 - pthread) + +include(TestBigEndian) +test_big_endian(isBigEndian) +if (${isBigEndian}) + set ( WORDS_BIGENDIAN 1 ) +endif() + +add_executable(simple-kanban server.c) +add_executable(simple-kanban-client client.c) + +target_link_libraries(simple-kanban pthread) +target_link_libraries(simple-kanban-client pthread) diff --git a/README.md b/README.md index 5613b17..607eff6 100644 --- a/README.md +++ b/README.md @@ -72,10 +72,10 @@ make ## 0. 启动程序 ```bash -./simple-kanban-client 127.0.0.1 7777 [-b] +./simple-kanban-client 127.0.0.1 7777 [-r] ``` -接下来即可按照上面的交互流程开始使用。其中末尾的`-b`为可选项(程序只判断参数个数所以请务必放到末尾),表示以大端序发送文件长度。 +接下来即可按照上面的交互流程开始使用。其中末尾的`-r`为可选项(程序只判断参数个数所以请务必放到末尾),表示以与本机相反的顺序发送文件长度。 ## 1. 发送命令 diff --git a/client.c b/client.c index 37f2382..4198540 100644 --- a/client.c +++ b/client.c @@ -43,7 +43,7 @@ off_t size_of_file(const char* fname) { else return -1; } -int main(int argc,char *argv[]) { //usage: ./client host port [-d] +int main(int argc,char *argv[]) { //usage: ./client host port [-r] ssize_t numbytes; puts("break!"); while((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1); @@ -74,8 +74,8 @@ int main(int argc,char *argv[]) { //usage: ./client host port [-d] if(fp) { off_t len = 0; file_size = (uint32_t)size_of_file(buf); - if(argc == 4) file_size = htonl(file_size); #if __APPLE__ + if(argc == 4) file_size = htonl(file_size); struct iovec headers; headers.iov_base = &file_size; headers.iov_len = sizeof(uint32_t); @@ -86,7 +86,10 @@ int main(int argc,char *argv[]) { //usage: ./client host port [-d] if(!sendfile(fileno(fp), sockfd, 0, &len, &hdtr, 0)) puts("Send file success."); else puts("Send file error."); #else - send(sockfd, &file_size, sizeof(uint32_t), 0); + if(argc == 4) { + 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."); else puts("Send file error."); #endif diff --git a/server.c b/server.c index 027870e..4ea157c 100644 --- a/server.c +++ b/server.c @@ -121,6 +121,7 @@ int send_all(char* file_path, THREADTIMER *timer) { printf("Get file size: %u bytes.\n", file_size); off_t len = 0; #if __APPLE__ + //苹果基本没有大端,不作处理 struct sf_hdtr hdtr; struct iovec headers; headers.iov_base = &file_size; @@ -132,7 +133,12 @@ int send_all(char* file_path, THREADTIMER *timer) { re = !sendfile(fileno(fp), timer->accept_fd, 0, &len, &hdtr, 0); if(!re) perror("Sendfile"); #else - send(timer->accept_fd, &file_size, sizeof(uint32_t), 0); + #if WORDS_BIGENDIAN + uint32_t little_fs = htonl(file_size); + send(timer->accept_fd, &little_fs sizeof(uint32_t), 0); + #else + send(timer->accept_fd, &file_size, sizeof(uint32_t), 0); + #endif re = sendfile(timer->accept_fd, fileno(fp), &len, file_size) >= 0; if(!re) perror("Sendfile"); #endif