diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dbe9c82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/ \ No newline at end of file diff --git a/10/.gitignore b/10/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/10/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/11/.gitignore b/11/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/11/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/12/.gitignore b/12/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/12/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/13/.gitignore b/13/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/13/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/14/.gitignore b/14/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/14/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/15/.gitignore b/15/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/15/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/16/.gitignore b/16/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/16/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/5/.gitignore b/5/.gitignore new file mode 100644 index 0000000..3c36a47 --- /dev/null +++ b/5/.gitignore @@ -0,0 +1,7 @@ +* + +!.gitignore +!Makefile +!*.cpp +!*.h +!*.cc \ No newline at end of file diff --git a/5/5-3testlisten.cpp b/5/5-3testlisten.cpp index 7196c8b..a31ec84 100644 --- a/5/5-3testlisten.cpp +++ b/5/5-3testlisten.cpp @@ -8,6 +8,11 @@ #include #include +// 判断是macOS +#ifdef __APPLE__ +#include +#endif + static bool stop = false; static void handle_term( int sig ) { diff --git a/5/5-5testaccept.cpp b/5/5-5testaccept.cpp index a2b95b0..b4f43c2 100644 --- a/5/5-5testaccept.cpp +++ b/5/5-5testaccept.cpp @@ -8,47 +8,54 @@ #include #include -int main( int argc, char* argv[] ) +// 判断是macOS +#ifdef __APPLE__ +#include +#endif + +int main(int argc, char *argv[]) { - if( argc <= 2 ) + if (argc <= 2) { - printf( "usage: %s ip_address port_number\n", basename( argv[0] ) ); + printf("usage: %s ip_address port_number\n", basename(argv[0])); return 1; } - const char* ip = argv[1]; - int port = atoi( argv[2] ); + // 接受IP和端口 + const char *ip = argv[1]; + int port = atoi(argv[2]); + + // IPv4 套接字地址结构 struct sockaddr_in address; - bzero( &address, sizeof( address ) ); + bzero(&address, sizeof(address)); address.sin_family = AF_INET; - inet_pton( AF_INET, ip, &address.sin_addr ); - address.sin_port = htons( port ); + inet_pton(AF_INET, ip, &address.sin_addr); + address.sin_port = htons(port); - int sock = socket( PF_INET, SOCK_STREAM, 0 ); - assert( sock>= 0 ); + int sock = socket(PF_INET, SOCK_STREAM, 0); + assert(sock>= 0); - int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) ); - assert( ret != -1 ); + int ret = bind(sock, (struct sockaddr *)&address, sizeof(address)); + assert(ret != -1); - ret = listen( sock, 5 ); - assert( ret != -1 ); + ret = listen(sock, 5); + assert(ret != -1); struct sockaddr_in client; - socklen_t client_addrlength = sizeof( client ); - int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); - if ( connfd < 0 ) + socklen_t client_addrlength = sizeof(client); + int connfd = accept(sock, (struct sockaddr *)&client, &client_addrlength); + if (connfd < 0) { - printf( "errno is: %d\n", errno ); + printf("errno is: %d\n", errno); } else { - char remote[INET_ADDRSTRLEN ]; - printf( "connected with ip: %s and port: %d\n", - inet_ntop( AF_INET, &client.sin_addr, remote, INET_ADDRSTRLEN ), ntohs( client.sin_port ) ); - close( connfd ); + char remote[INET_ADDRSTRLEN]; + printf("connected with ip: %s and port: %d\n", + inet_ntop(AF_INET, &client.sin_addr, remote, INET_ADDRSTRLEN), ntohs(client.sin_port)); + close(connfd); } - close( sock ); + close(sock); return 0; } - diff --git a/5/5-6oobsend.cpp b/5/5-6oobsend.cpp index 70fa13e..7c58257 100644 --- a/5/5-6oobsend.cpp +++ b/5/5-6oobsend.cpp @@ -7,6 +7,11 @@ #include #include +// 判断是macOS +#ifdef __APPLE__ +#include +#endif + int main( int argc, char* argv[] ) { if( argc <= 2 ) diff --git a/5/5-7oobrecv.cpp b/5/5-7oobrecv.cpp index cd0b070..0b77a10 100644 --- a/5/5-7oobrecv.cpp +++ b/5/5-7oobrecv.cpp @@ -7,6 +7,10 @@ #include #include #include +// 判断是macOS +#ifdef __APPLE__ +#include +#endif #define BUF_SIZE 1024 diff --git a/5/Makefile b/5/Makefile new file mode 100644 index 0000000..af21aae --- /dev/null +++ b/5/Makefile @@ -0,0 +1,2 @@ +accept.out: 5-5testaccept.cpp + gcc 5-5testaccept.cpp -o accept.out \ No newline at end of file diff --git a/6/.gitignore b/6/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/6/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/6/6-2testwritev.cpp b/6/6-2testwritev.cpp index 12d6c21..3107b36 100644 --- a/6/6-2testwritev.cpp +++ b/6/6-2testwritev.cpp @@ -9,68 +9,80 @@ #include #include #include +#include #include #define BUFFER_SIZE 1024 -static const char* status_line[2] = { "200 OK", "500 Internal server error" }; +static const char *status_line[2] = {"200 OK", "500 Internal server error"}; -int main( int argc, char* argv[] ) +int main(int argc, char *argv[]) { - if( argc <= 3 ) + if (argc <= 3) { - printf( "usage: %s ip_address port_number filename\n", basename( argv[0] ) ); + printf("usage: %s ip_address port_number filename\n", basename(argv[0])); return 1; } - const char* ip = argv[1]; - int port = atoi( argv[2] ); - const char* file_name = argv[3]; + // 接受IP地址 + const char *ip = argv[1]; + // 就收端口 + int port = atoi(argv[2]); + // 接受文件名 + const char *file_name = argv[3]; + + // 初始化IP地址信息 struct sockaddr_in address; - bzero( &address, sizeof( address ) ); + bzero(&address, sizeof(address)); address.sin_family = AF_INET; - inet_pton( AF_INET, ip, &address.sin_addr ); - address.sin_port = htons( port ); + inet_pton(AF_INET, ip, &address.sin_addr); + address.sin_port = htons(port); - int sock = socket( PF_INET, SOCK_STREAM, 0 ); - assert( sock>= 0 ); + // 创建socket + int sock = socket(PF_INET, SOCK_STREAM, 0); + assert(sock>= 0); - int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) ); - assert( ret != -1 ); + // 绑定命名socket + int ret = bind(sock, (struct sockaddr *)&address, sizeof(address)); + assert(ret != -1); - ret = listen( sock, 5 ); - assert( ret != -1 ); + // 监听socket并设置半连接队列大小 + ret = listen(sock, 5); + assert(ret != -1); + // 接受来自客户端的连接 struct sockaddr_in client; - socklen_t client_addrlength = sizeof( client ); - int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); - if ( connfd < 0 ) + socklen_t client_addrlength = sizeof(client); + int connfd = accept(sock, (struct sockaddr *)&client, &client_addrlength); + + + if (connfd < 0) { - printf( "errno is: %d\n", errno ); + printf("errno is: %d\n", errno); } else { - char header_buf[ BUFFER_SIZE ]; - memset( header_buf, '0円', BUFFER_SIZE ); - char* file_buf; + char header_buf[BUFFER_SIZE]; + memset(header_buf, '0円', BUFFER_SIZE); + char *file_buf; struct stat file_stat; bool valid = true; int len = 0; - if( stat( file_name, &file_stat ) < 0 ) + if (stat(file_name, &file_stat) < 0) { valid = false; } else { - if( S_ISDIR( file_stat.st_mode ) ) + if (S_ISDIR(file_stat.st_mode)) { valid = false; } - else if( file_stat.st_mode & S_IROTH ) + else if (file_stat.st_mode & S_IROTH) { - int fd = open( file_name, O_RDONLY ); - file_buf = new char [ file_stat.st_size + 1 ]; - memset( file_buf, '0円', file_stat.st_size + 1 ); - if ( read( fd, file_buf, file_stat.st_size ) < 0 ) + int fd = open(file_name, O_RDONLY); + file_buf = new char[file_stat.st_size + 1]; + memset(file_buf, '0円', file_stat.st_size + 1); + if (read(fd, file_buf, file_stat.st_size) < 0) { valid = false; } @@ -80,34 +92,32 @@ int main( int argc, char* argv[] ) valid = false; } } - - if( valid ) + + if (valid) { - ret = snprintf( header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[0] ); + ret = snprintf(header_buf, BUFFER_SIZE - 1, "%s %s\r\n", "HTTP/1.1", status_line[0]); len += ret; - ret = snprintf( header_buf + len, BUFFER_SIZE-1-len, - "Content-Length: %d\r\n", file_stat.st_size ); + ret = snprintf(header_buf + len, BUFFER_SIZE - 1 - len, "Content-Length: %ld\r\n", file_stat.st_size); len += ret; - ret = snprintf( header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n" ); + ret = snprintf(header_buf + len, BUFFER_SIZE - 1 - len, "%s", "\r\n"); struct iovec iv[2]; - iv[ 0 ].iov_base = header_buf; - iv[ 0 ].iov_len = strlen( header_buf ); - iv[ 1 ].iov_base = file_buf; - iv[ 1 ].iov_len = file_stat.st_size; - ret = writev( connfd, iv, 2 ); + iv[0].iov_base = header_buf; + iv[0].iov_len = strlen(header_buf); + iv[1].iov_base = file_buf; + iv[1].iov_len = file_stat.st_size; + ret = writev(connfd, iv, 2); } else { - ret = snprintf( header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[1] ); + ret = snprintf(header_buf, BUFFER_SIZE - 1, "%s %s\r\n", "HTTP/1.1", status_line[1]); len += ret; - ret = snprintf( header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n" ); - send( connfd, header_buf, strlen( header_buf ), 0 ); + ret = snprintf(header_buf + len, BUFFER_SIZE - 1 - len, "%s", "\r\n"); + send(connfd, header_buf, strlen(header_buf), 0); } - close( connfd ); - delete [] file_buf; + close(connfd); + delete[] file_buf; } - close( sock ); + close(sock); return 0; } - diff --git a/6/6-3testsendfile.cpp b/6/6-3testsendfile.cpp index 803d025..035c233 100644 --- a/6/6-3testsendfile.cpp +++ b/6/6-3testsendfile.cpp @@ -12,51 +12,69 @@ #include #include -int main( int argc, char* argv[] ) +int main(int argc, char *argv[]) { - if( argc <= 3 ) + if (argc <= 3) { - printf( "usage: %s ip_address port_number filename\n", basename( argv[0] ) ); + printf("usage: %s ip_address port_number filename\n", basename(argv[0])); return 1; } - const char* ip = argv[1]; - int port = atoi( argv[2] ); - const char* file_name = argv[3]; + // 接受IP地址 + const char *ip = argv[1]; - int filefd = open( file_name, O_RDONLY ); - assert( filefd> 0 ); + // 接受端口号 + int port = atoi(argv[2]); + + // 接受文件地址 + const char *file_name = argv[3]; + + // 打开文件,获取文件句柄 + int filefd = open(file_name, O_RDONLY); + assert(filefd> 0); + + // 通过文件描述符获取文件状态 struct stat stat_buf; - fstat( filefd, &stat_buf ); + fstat(filefd, &stat_buf); + // 初始化IP地址结构体 struct sockaddr_in address; - bzero( &address, sizeof( address ) ); + bzero(&address, sizeof(address)); + + // 设置协议族 address.sin_family = AF_INET; - inet_pton( AF_INET, ip, &address.sin_addr ); - address.sin_port = htons( port ); + // 将IP地址字符串表示转化为网络字节序整数表示 + inet_pton(AF_INET, ip, &address.sin_addr); + // 赋值端口 + address.sin_port = htons(port); - int sock = socket( PF_INET, SOCK_STREAM, 0 ); - assert( sock>= 0 ); + // 创建socket + int sock = socket(PF_INET, SOCK_STREAM, 0); + assert(sock>= 0); - int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) ); - assert( ret != -1 ); + // 命名绑定socket到指定的地址 + int ret = bind(sock, (struct sockaddr *)&address, sizeof(address)); + assert(ret != -1); - ret = listen( sock, 5 ); - assert( ret != -1 ); + // 开启监听并设置半连接队列为5 + ret = listen(sock, 5); + assert(ret != -1); + // 接受来自客户端的连接,并接受地址 struct sockaddr_in client; - socklen_t client_addrlength = sizeof( client ); - int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); - if ( connfd < 0 ) + socklen_t client_addrlength = sizeof(client); + int connfd = accept(sock, (struct sockaddr *)&client, &client_addrlength); + + if (connfd < 0) { - printf( "errno is: %d\n", errno ); + printf("errno is: %d\n", errno); } else { - sendfile( connfd, filefd, NULL, stat_buf.st_size ); - close( connfd ); + // 发送文件 + sendfile(connfd, filefd, NULL, stat_buf.st_size); + close(connfd); } - close( sock ); + close(sock); return 0; } - diff --git a/6/Makefile b/6/Makefile new file mode 100644 index 0000000..f097a8c --- /dev/null +++ b/6/Makefile @@ -0,0 +1,11 @@ +# dup.out: 6-1testdup.cpp +# gcc 6-1testdup.cpp -o dup.out + +writev.out: 6-2testwritev.cpp + g++ 6-2testwritev.cpp -o writev.out + +sendfile.out: 6-3testsendfile.cpp + gcc 6-3testsendfile.cpp -o sendfile.out + +clean: + rm *.out \ No newline at end of file diff --git a/7/.gitignore b/7/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/7/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/8/.gitignore b/8/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/8/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/9/.gitignore b/9/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/9/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile diff --git a/springsnail/.gitignore b/springsnail/.gitignore new file mode 100644 index 0000000..44b95ae --- /dev/null +++ b/springsnail/.gitignore @@ -0,0 +1,4 @@ +* + +!.gitignore +!Makefile

AltStyle によって変換されたページ (->オリジナル) /