Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn IP Addresses and Ports | Networking Foundations in C
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Networking Basics

bookIP 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

print_endpoint.c

copy
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.

question mark

What is the primary purpose of a port number in networking?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how to use the sockaddr_in structure in C?

What are some common port numbers and their uses?

How do IP addresses and port numbers work together in a real-world example?

bookIP Addresses and Ports

Swipe to show menu

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

print_endpoint.c

copy
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.

question mark

What is the primary purpose of a port number in networking?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2
some-alt