beej-network-programming/main.c

127 lines
2.6 KiB
C

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdint.h>
#pragma comment(lib, "ws2_32.lib")
/*
Address info
- ai_flag: address info flag
- ai_family: addres info family AF_INET (ipv4) or AF_INET6 (ipv6)
- ai_socktype: socket type SOCK_STREAM or SOCK_DGRAM
- ai_protocol: use 0 for any
- ai_addrlen: size of the ai_addr (below)
- ai_addr: pointer sockaddr struct
- ai_canonname: canonical hostname
- ai_next: linked list to next node
*/
/*typedef struct addrinfo {
int ai_flag; // address info flags
int ai_family; // address info famitly ipv4/6
int ai_socktype;
int ai_protocol;
size_t ai_addrlen;
sockaddr *ai_addr;
char *ai_canonname;
addrinfo *ai_next;
} addrinfo;
*/
/*
Socket address info
- sa_family: AF_XX (e.g AF_INET)
- sa_data: 14 bytes of protocol address, the destination address as well as the port number
*/
/*typedef struct sockaddr {
unsigned short sa_family;
char *sa_data[14];
} sockaddr;
*/
/*
Socket Adress Internet (ipv4)
This struct is used to replace sockaddr in instances where family = AF_INET
- sin_family: AF_INET (ALWAYS)
- sin_port: port
- sin_addr: address struct
- sin_zero: padding to match sockaddr
*/
/*typedef sockaddr_in {
short int sin_family;
unsigned short int sin_port;
in_addr sin_addr;
unsigned char sin_zero[8];
} sockaddr_in;
typedef struct in_addr {
uint32_t s_addr;
} in_addr;
*/
int main()
{
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
if (LOBYTE(wsaData.wVersion) != 2 ||
HIBYTE(wsaData.wVersion) != 2)
{
fprintf(stderr,"Versiion 2.2 of Winsock is not available.\n");
WSACleanup();
exit(2);
}
}
// char ip4[INET_ADDRSTRLEN];
// struct sockaddr_in sa; // ipv4 ip address
// inet_pton(AF_INET, "10.12.110.57", &(sa.sin_addr));
// inet_ntop(AF_INET, &(sa.sin_addr), ip4, INET_ADDRSTRLEN);
// printf("the ipv4 address is: %s\n", ip4);
// printf("hellow world!\n");
int status;
struct addrinfo hints;
struct addrinfo *servinfo;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
status = getaddrinfo(NULL, "3490", &hints, &servinfo);
if (status != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
freeaddrinfo(servinfo);
return(0);
}