From ced6a6afcaf4a16214bf9d3f9ecf16431a7723aa Mon Sep 17 00:00:00 2001 From: fumiama Date: Mon, 3 May 2021 00:18:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=8D=E6=AC=A1=E9=80=82=E9=85=8Dlinux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.c | 6 +++++- server.c | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/client.c b/client.c index 3c7d773..265d4d7 100644 --- a/client.c +++ b/client.c @@ -47,6 +47,10 @@ int main(int argc,char *argv[]) { //usage: ./client host port ssize_t numbytes; puts("break!"); while((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1); + #if !__APPLE__ + int reuse = 1; + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); + #endif puts("Get sockfd"); their_addr.sin_family = AF_INET; their_addr.sin_port = htons(atoi(argv[2])); @@ -84,7 +88,7 @@ int main(int argc,char *argv[]) { //usage: ./client host port else puts("Send file error."); #else send(sockfd, &file_size, sizeof(uint32_t), 0); - if(!sendfile(sockfd, fileno(fp), &len, htonl(file_size))) puts("Send file success."); + if(!sendfile(sockfd, fileno(fp), &len, file_size)) puts("Send file success."); else puts("Send file error."); #endif fclose(fp); diff --git a/server.c b/server.c index 6c8fe85..217d3bf 100644 --- a/server.c +++ b/server.c @@ -76,8 +76,12 @@ int bindServer(uint16_t port, u_int try_times) { server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server_addr.sin_zero), 8); - + fd = socket(AF_INET, SOCK_STREAM, 0); + #if !__APPLE__ + int reuse = 1; + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); + #endif while(!~(result = bind(fd, (struct sockaddr *)&server_addr, struct_len)) && fail_count++ < try_times) sleep(1); if(!~result && fail_count >= try_times) { puts("Bind server failure!"); @@ -137,7 +141,7 @@ int sendAll(char* file_path, THREADTIMER *timer) { sendfile(fileno(fp), timer->accept_fd, 0, &len, &hdtr, 0); #else send(timer->accept_fd, &file_size, sizeof(uint32_t), 0); - sendfile(timer->accept_fd, fileno(fp), &len, htonl(file_size)); + sendfile(timer->accept_fd, fileno(fp), &len, file_size); #endif printf("Send %lld bytes.\n", len); closeFile(fp);