Networking & Internet Functions
Core Networking
-
getaddrinfo
Resolves a hostname or service name to an address structure.
-
gethostbyname
Retrieves the IP address of a host from its name.
-
send
Sends data on a connected socket.
-
recv
Receives data from a connected socket.
-
socket
Creates a socket for communication.
-
connect
Establishes a connection to a remote socket.
-
bind
Associates a local address with a socket.
-
listen
Puts a socket into a listening mode for incoming connection requests.
-
accept
Accepts a connection on a listening socket.
Internet Protocols (HTTP, FTP)
-
InternetOpen
Initializes an application for use with the Internet functions.
-
InternetConnect
Establishes a connection to a specified Internet server.
-
HttpOpenRequest
Opens an HTTP request.
-
HttpSessionRequest
Sends the specified request to the HTTP server.
-
InternetReadFile
Reads data from the specified Internet resource.
DNS and Name Resolution
-
DnsQuery
Queries DNS for records of a specified type.
-
GetHostName
Retrieves the standard host name for the current computer.
getaddrinfo
Resolves a hostname or service name to an address structure.
Parameters
- pNodeName: The hostname or network name to resolve.
- pServiceName: The service name or port number to resolve.
- pHints: A pointer to an
ADDRINFOstructure that specifies criteria for the returned address structures. - ppResult: A pointer to a pointer that receives the address of a linked list of one or more ADDRINFO structures.
Return Value
- On success, returns
0(zero). - On failure, returns a non-zero error code.
Remarks
- This is the recommended function for protocol-independent name resolution.
- Frees the memory returned by
getaddrinfousingfreeaddrinfo.
Example
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
ADDRINFO hints, *result, *ptr;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("www.example.com", "80", &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
printf("Successfully resolved www.example.com:\n");
for(ptr=result; ptr != NULL ; ptr=ptr->ai_next) {
if (ptr->ai_family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)ptr->ai_addr;
printf(" IPv4 Address: %s\n", inet_ntoa(ipv4->sin_addr));
} else if (ptr->ai_family == AF_INET6) {
// Handle IPv6 if necessary
printf(" IPv6 Address found (handling omitted for brevity).\n");
}
}
freeaddrinfo(result);
WSACleanup();
return 0;
}
InternetOpen
Initializes an application for use with the Internet functions.
Parameters
- lpszAgent: Pointer to a null-terminated string that specifies the name of the calling application or object.
- dwAccessType: Specifies the type of access to the Internet. Can be
INTERNET_OPEN_TYPE_PRECONFIG,INTERNET_OPEN_TYPE_DIRECT, orINTERNET_OPEN_TYPE_PROXY. - lpszProxyName: Pointer to a null-terminated string that specifies the name and, if necessary, the port number of the proxy server.
- lpszProxyBypass: Pointer to a null-terminated string that specifies a semicolon-separated list of servers that bypass the proxy server.
- dwFlags: Specifies whether to use asynchronous or synchronous operations.
Return Value
- If the function succeeds, it returns a valid Internet handle.
- If the function fails, it returns
NULL.
Remarks
- This function must be called before any other Internet functions (except
InternetGetLastResponseInfo). - The returned handle should be closed by calling
InternetCloseHandle.
Example
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#pragma comment(lib, "Wininet.lib")
int main() {
HINTERNET hInternet;
// Initialize Internet functions for direct access
hInternet = InternetOpen(L"MyAwesomeApp/1.0",
INTERNET_OPEN_TYPE_DIRECT,
NULL,
NULL,
0); // 0 for synchronous operations
if (hInternet == NULL) {
printf("InternetOpen failed. Error: %lu\n", GetLastError());
return 1;
}
printf("InternetOpen succeeded. Handle: %p\n", hInternet);
// Use hInternet for subsequent Internet operations...
// Close the handle when done
InternetCloseHandle(hInternet);
printf("Internet handle closed.\n");
return 0;
}