diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a442b3..0d559f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(simple-kanban) include(TestBigEndian) test_big_endian(isBigEndian) if (${isBigEndian}) - set ( WORDS_BIGENDIAN 1 ) + add_definitions(-DWORDS_BIGENDIAN) endif() #add_definitions(-DLISTEN_ON_IPV6) diff --git a/README.md b/README.md index 607eff6..b4ea6c5 100644 --- a/README.md +++ b/README.md @@ -72,10 +72,10 @@ make ## 0. 启动程序 ```bash -./simple-kanban-client 127.0.0.1 7777 [-r] +./simple-kanban-client 127.0.0.1 7777 ``` -接下来即可按照上面的交互流程开始使用。其中末尾的`-r`为可选项(程序只判断参数个数所以请务必放到末尾),表示以与本机相反的顺序发送文件长度。 +接下来即可按照上面的交互流程开始使用。 ## 1. 发送命令 diff --git a/client.c b/client.c index 4198540..f4bf24c 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 [-r] +int main(int argc,char *argv[]) { //usage: ./client host port ssize_t numbytes; puts("break!"); 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; file_size = (uint32_t)size_of_file(buf); #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,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."); else puts("Send file error."); #else - 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); + 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 e73309a..31ba4ec 100644 --- a/server.c +++ b/server.c @@ -145,9 +145,15 @@ int send_all(char* file_path, THREADTIMER *timer) { re = !sendfile(fileno(fp), timer->accept_fd, 0, &len, &hdtr, 0); if(!re) perror("Sendfile"); #else - #if WORDS_BIGENDIAN - uint32_t little_fs = htonl(file_size); - send(timer->accept_fd, &little_fs sizeof(uint32_t), 0); + #ifdef WORDS_BIGENDIAN + uint32_t little_fs; + 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 send(timer->accept_fd, &file_size, sizeof(uint32_t), 0); #endif @@ -219,7 +225,16 @@ int s2_set(THREADTIMER *timer) { int s3_set_data(THREADTIMER *timer) { timer->status = 0; - uint32_t file_size = *(uint32_t*)(timer->data); + #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); + #endif printf("Set data size: %u\n", file_size); int is_first_data = 0; if(timer->numbytes == sizeof(uint32_t)) {