IP Addresses and Ports
Understanding how devices communicate over a network is essential in C programming for networking. Two fundamental concepts you must know are IP addresses and port numbers. An IPv4 address is a 32-bit number that uniquely identifies a device on a network. It is commonly written in dotted decimal form, such as 192.168.1.10, where each number represents one byte of the address. This address allows data to be routed to the correct device.
A port number is a 16-bit integer that helps identify a specific process or service running on a device. While the IP address ensures data reaches the correct device, the port number ensures it is delivered to the right application on that device. For example, web servers typically use port 80 for HTTP and port 443 for HTTPS. Ports range from 0 to 65535, with some ports reserved for common services.
print_endpoint.c
12345678910111213141516171819#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <netinet/in.h> int main() { struct sockaddr_in endpoint; memset(&endpoint, 0, sizeof(endpoint)); endpoint.sin_family = AF_INET; endpoint.sin_port = htons(8080); // Port 8080 in network byte order inet_pton(AF_INET, "192.168.1.100", &endpoint.sin_addr); char ip_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &endpoint.sin_addr, ip_str, sizeof(ip_str)); printf("IP Address: %s\n", ip_str); printf("Port: %d\n", ntohs(endpoint.sin_port)); return 0; }
Together, an IP address and a port number form a network endpoint. This pairing allows multiple services to operate simultaneously on the same device, each distinguished by its port number. In C networking, you use the sockaddr_in structure to represent an endpoint for IPv4. This structure contains fields for the IP address and port, enabling your program to specify where data should be sent or received.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Mahtavaa!
Completion arvosana parantunut arvoon 8.33
IP Addresses and Ports
Pyyhkäise näyttääksesi valikon
Understanding how devices communicate over a network is essential in C programming for networking. Two fundamental concepts you must know are IP addresses and port numbers. An IPv4 address is a 32-bit number that uniquely identifies a device on a network. It is commonly written in dotted decimal form, such as 192.168.1.10, where each number represents one byte of the address. This address allows data to be routed to the correct device.
A port number is a 16-bit integer that helps identify a specific process or service running on a device. While the IP address ensures data reaches the correct device, the port number ensures it is delivered to the right application on that device. For example, web servers typically use port 80 for HTTP and port 443 for HTTPS. Ports range from 0 to 65535, with some ports reserved for common services.
print_endpoint.c
12345678910111213141516171819#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <netinet/in.h> int main() { struct sockaddr_in endpoint; memset(&endpoint, 0, sizeof(endpoint)); endpoint.sin_family = AF_INET; endpoint.sin_port = htons(8080); // Port 8080 in network byte order inet_pton(AF_INET, "192.168.1.100", &endpoint.sin_addr); char ip_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &endpoint.sin_addr, ip_str, sizeof(ip_str)); printf("IP Address: %s\n", ip_str); printf("Port: %d\n", ntohs(endpoint.sin_port)); return 0; }
Together, an IP address and a port number form a network endpoint. This pairing allows multiple services to operate simultaneously on the same device, each distinguished by its port number. In C networking, you use the sockaddr_in structure to represent an endpoint for IPv4. This structure contains fields for the IP address and port, enabling your program to specify where data should be sent or received.
Kiitos palautteestasi!