feat: initial set and sockets

This commit is contained in:
2025-09-12 03:17:08 +02:00
parent 56fa31d087
commit a699745785
7 changed files with 291 additions and 290 deletions

View File

@@ -1,7 +1,5 @@
#include "mysocket.h" #include "mysocket.h"
#include <winsock2.h>
int sock_platform_init() { int sock_platform_init() {
#ifdef _WIN32 #ifdef _WIN32
WSADATA wsaData; WSADATA wsaData;

View File

@@ -3,15 +3,18 @@
#ifdef _WIN32 #ifdef _WIN32
/* Windows */ /* Windows */
#define _WIN32_WINNT 0x0600
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#else #else
/* Unix */ /* Unix */
#define __USE_XOPEN2K
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#endif #endif

View File

@@ -1,30 +1,29 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "../../socket/mysocket.h" #include "../../socket/mysocket.h"
void test_socket1() { void test_socket1(void) {
sock_platform_init(); sock_platform_init();
struct addrinfo hints, *res, *p; struct addrinfo hints, *res, *p;
char ipstr[INET6_ADDRSTRLEN]; char ipstr[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof hints); memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
char hostname[1024]; char hostname[1024];
printf("hostname> "); printf("hostname> ");
fscanf(stdin, "%s", hostname); fscanf(stdin, "%s", hostname);
if (getaddrinfo(hostname, NULL, &hints, &res) != 0) { if (getaddrinfo(hostname, NULL, &hints, &res) != 0) {
fprintf(stderr, "getaddrinfo()\n"); fprintf(stderr, "getaddrinfo() failed\n");
sock_platform_shutdown(); sock_platform_shutdown();
return;
} }
for (p = res; p != NULL; p = p->ai_next) { for (p = res; p != NULL; p = p->ai_next) {
void *addr = 0; void *addr = 0;
char *ipver = 0; char *ipver = 0;
struct sockaddr_in *ipv4; struct sockaddr_in *ipv4;
if (p->ai_family == AF_INET) { if (p->ai_family == AF_INET) {
ipv4 = (struct sockaddr_in *)p->ai_addr; ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr); addr = &(ipv4->sin_addr);
@@ -36,5 +35,6 @@ void test_socket1() {
printf("%s: %s\n", ipver, ipstr); printf("%s: %s\n", ipver, ipstr);
} }
freeaddrinfo(res);
sock_platform_shutdown(); sock_platform_shutdown();
} }