初始提交

This commit is contained in:
Gary Gan 2025-04-18 18:23:15 +08:00
commit 3ae572a20a
5 changed files with 108 additions and 0 deletions

36
.gitignore vendored Normal file
View File

@ -0,0 +1,36 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
build/
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
.vscode/
.cache/

5
CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.10.0)
project(unix_network_programming VERSION 0.1.0 LANGUAGES C CXX)
add_executable(unix_network_programming main.cpp)

17
CMakePresets.json Normal file
View File

@ -0,0 +1,17 @@
{
"version": 8,
"configurePresets": [
{
"name": "clang",
"displayName": "Clang 17.0.0 arm64-apple-darwin24.3.0",
"description": "正在使用编译器: C = /usr/bin/clang, CXX = /usr/bin/clang++",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
"CMAKE_C_COMPILER": "/usr/bin/clang",
"CMAKE_CXX_COMPILER": "/usr/bin/clang++",
"CMAKE_BUILD_TYPE": "Debug"
}
}
]
}

45
lib/sock_ntop.c Normal file
View File

@ -0,0 +1,45 @@
#include <cstdio>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
char *
sock_ntop(const struct sockaddr *sa, socklen_t salen)
{
char portstr[8];
static char str[128]; // Unix domain is largest
switch (sa -> sa_family) {
case AF_INET: {
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL)
return NULL;
if (ntohs(sin->sin_port) != 0) {
snprintf(portstr, sizeof(portstr), ":%d", ntohs(sin->sin_port));
strcat(str, portstr);
}
return str;
}
case AF_INET6: {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
str[0] = '[';
if (inet_ntop(AF_INET6, &sin6->sin6_addr, str+1, sizeof(str) - 1) == NULL)
return NULL;
if (ntohs(sin6->sin6_port) != 0) {
// 如果sin6中端口数字解释正常的话
snprintf(portstr, sizeof(portstr), "]:%d", ntohs(sin6->sin6_port));
strcat(str, portstr);
return str;
}
// 排除'['
return str + 1;
}
}
return NULL;
}

5
main.cpp Normal file
View File

@ -0,0 +1,5 @@
#include <iostream>
int main(int, char**){
std::cout << "Hello, from unix_network_programming!\n";
}