CppSocketP
Cpp Socket Programming Tutorial
Install / Use
/learn @amirrezatav/CppSocketPREADME
Socket Programming in C/C++
Introduction
Socket programming is a way of connecting two nodes on a network to communicate with each other.
One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection.
Server forms the listener socket while client reaches out to the server.
We can communicate easily without losing any data
If we take a real-life example then the socket we see in reality is a medium to connect two devices or systems.
It can be either a phone charger plugging into the socket or a USB cable into our laptop.
In the same way, Sockets let applications attach to the local network at different ports.
Every time a socket is created, the program has to specify the socket type as well as the domain address.
Sockets are a mechanism for exchanging data between processes. These processes can either be on the same machine, or on different machines connected via a network. Once a socket
connection is established, data can be sent in both directions until one of the endpoints closes the connection
The goal of this article is to teach you how to use the ClientSocket and ServerSocket classes in your own applications.
Client-Server Definition
Client-server denotes a relationship between cooperating programs in an application, composed of clients initiating requests for services and servers providing that function or service.
IP address definition
An IP address is a unique address that identifies a device on the internet or a local network.
IP stands for "Internet Protocol," which is the set of rules governing the format of data sent via the internet or local network.
In essence, IP addresses are the identifier that allows information to be sent between devices on a network: they contain location information and make devices accessible for communication. The internet needs a way to differentiate between different computers, routers, and websites. IP addresses provide a way of doing so and form an essential part of how the internet works.
What is an IP?
An IP address is a string of numbers separated by periods. IP addresses are expressed as a set of four numbers — an example address might be 192.158.1.38. Each number in the set can range from 0 to 255.
So, the full IP addressing range goes from 0.0.0.0 to 255.255.255.255.
IP addresses are not random. They are mathematically produced and allocated by the Internet Assigned Numbers Authority (IANA), a division of the Internet Corporation for Assigned Names and Numbers (ICANN). ICANN is a non-profit organization that was established in the United States in 1998 to help maintain the security of the internet and allow it to be usable by all. Each time anyone registers a domain on the internet, they go through a domain name registrar, who pays a small fee to ICANN to register the domain.
The IPv4 Address.
The common type of IP address (is known as IPv4, for “version 4”). Here’s an example of what an IP address might look like:
66.171.248.170
An IPv4 address consists of four numbers, each of which contains one to three digits, with a single dot (.) separating each number or set of digits. Each of the four numbers can range from 0 to 255.
Localhost
"Localhost" refers to the local computer that a program is running on. For example, if you are running a Web browser on your computer, your computer is considered to be the "localhost."
For example, a network administrator might use his local machine to start a Web server on one system and use a remote access program on another. These programs would run from computers other than the localhost.In the example above, the two non-local computers must be defined by their IP addresses. The local machine is defined as "localhost," which gives it an IP address of 127.0.0.1. This is considered a "loopback" address because the information sent to it is routed back to the local machine. Localhost is often used in Web scripting languages like PHP and ASP when defining what server the code should run from or where a database is located.
The IPv6 Address.
It’s called IPv6 and it offers a maximum number of IP address for today and for the future.
Whereas IPv4 supports a maximum of approximately 4.3 billion unique IP addresses, IPv6 supports, in theory, a maximum number that will never run out.
A theoretical maximum of 340,282,366,920,938,463,463,374,607,431,768,211,456. To be exact. In other words, we will never run out of IP addresses again.
An IPv6 address consists of eight groups of four hexadecimal digits. If a group consists of four zeros, the notation can be shortened using a colon to replace the zeros. Here’s an example IPv6 address:
2001:0db8:85a3:0000:0000:8a2e:0370:7334
Port
In computer networking, a port is a communication endpoint
A port is a virtual point where network connections start and end.
Ports are software-based and managed by a computer's operating system.
Each port is associated with a specific process or service.
Ports allow computers to easily differentiate between different kinds of traffic: emails go to a different port than webpages, for instance, even though both reach a computer over the same Internet connection.
Ports are standardized across all network-connected devices, with each port assigned a number. Most ports are reserved for certain protocols — for example, all Hypertext Transfer Protocol (HTTP) messages go to port 80. While IP addresses enable messages to go to and from specific devices, port numbers allow targeting of specific services or applications within those devices.
Vastly different types of data flow to and from a computer over the same network connection. The use of ports helps computers understand what to do with the data they receive.
Suppose Amirreza transfers an MP3 audio recording to Ali using the File Transfer Protocol (FTP). If Alice's computer passed the MP3 file data to Alice's email application, the email application would not know how to interpret it. But because Amirreza’s file transfer uses the port designated for FTP (port 21), Alice's computer is able to receive and store the file.
Port Range Groups
- 0 to 1023– Well known port numbers. Only special companies like Apple QuickTime, MSN, SQL Services, Gopher Services, and other prominent services have these port numbers.
- 1024 to 49151 – Registered ports; meaning they can be registered to specific protocols by software corporations.
- 49152 to 65536 – Dynamic or private ports; meaning that they can be used by just about anybody.
DNS
he process of DNS resolution involves converting a hostname (such as www.example.com) into a computer-friendly IP address (such as 192.168.1.1). An IP address is given to each device on the Internet, and that address is necessary to find the appropriate Internet device - like a street address is used to find a particular home. When a user wants to load a webpage, a translation must occur between what a user types into their web browser (example.com) and the machine-friendly address necessary to locate the example.com webpage.
What is an IP packet?
IP packets are created by adding an IP header to each packet of data before it is sent on its way. An IP header is just a series of bits (ones and zeros), and it records several pieces of information about the packet, including the sending and receiving IP address. IP headers also report:
- Header length
- Packet length
- Which transport protocol is being used (TCP, UDP, etc.)
There are two prevailing types of data transfer protocol in the Internet Protocol (IP) suite: . UDP – User Datagram Protocol . TCP – Transmission Control Protocol
What is TCP/IP?
(reliable, connection oriented)
The Transmission Control Protocol (TCP) is a transport protocol, meaning it dictates the way data is sent and received. A TCP header is included in the data portion of each packet that uses TCP/IP. Before transmitting data, TCP opens a connection with the recipient. TCP ensures that all packets arrive in order once transmission begins. Via TCP, the recipient will acknowledge receiving each packet that arrives. Missing packets will be sent again if receipt is not acknowledged.
TCP is designed for reliability, not speed. Because TCP has to make sure all packets arrive in order, loading data via TCP/IP can take longer if some packets are missing.
What is UDP/IP?
(unreliable, connectionless)
The User Datagram Protocol, or UDP, is another widely used transport protocol. It's faster than TCP, but it is also less reliable. UDP does not make sure all packets are delivered and in order, and it does not establish a connection before beginning or receiving transmissions.
What is HTTP?
The Hypertext Transfer Protocol (HTTP) is an application layer protocol for distributed.
HTTP is a protocol which allows the fetching of resources, such as HTML documents. It is the foundation of any data exchange on the Web and it is a client-server protocol, which means requests are initiated by the recipient, usually the Web browser. A complete document is reconstructed from the different sub-documents fetched, for instance text, layout description, images, videos, scripts, and more.
Clients and servers communicate by exchanging individual messages (as opposed to a stream of data). The messages sent by the client, usually a Web browser, are called requests and the messages sent by the server as an answer are called responses.
HTTP is a client-server protocol: requests are sent by one entity, the user-agent (or a proxy on behalf of it). Most of the time the user-agent is a Web browser, but it can be anything, for example a robot that crawls the Web to populate and maintain a search engine index.
Each individual request is sent to a server, which handles it and provides an answer, called the response. Between the client and the server there are numerous entities, collectively called proxies, which perform different operations and act as gateways
Related Skills
node-connect
347.6kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.6kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.6kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
