Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Cross-platform socket in C++

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to achieve cross-platform socket in C++. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Network programming

Socket is cross-platform and basically common in Window and Linux, no matter, java php needs network, network programming is what every programmer needs to master, he is not complicated. The complexity is the understanding of the whole protocol and the understanding of network communication.

This blog is the most important purpose of learning about the whole network programming:

{be familiar with the development process under windows and linux, and be able to develop multithreaded network programs that support cross-platform. Understand the principle of socket programming, understand the protocol of udp and tcp, and analyze the protocol through packet grabbing tools. Through the study of this course, we can develop a network server that supports high concurrency. Through the development of http server and syslog server that support php, we can put network programming into practical applications. Master the thread library and regular expression library of censor 11. }.

The use of voice is: Cramp Cleavage, because of its high performance and good handling of high concurrency.

Finally, we can practice a http server that supports PHP scripts.

It should be noted that the Socket library is not the same as TCP/UDP programming, because it can also develop other

Some of the agreements.

Communication group: 140066160

Get to the point: (first talk about theory, in actual combat)

Layered explanation of 1TCPIP protocol

Learn network programming, he said based on what protocol. Instead of using a few functions, what protocol is TCP/IP based on?

We can't just use Socket to transfer the two programs and send protocols to each other. You have to understand the whole process of the agreement in order to solve it in practice.

Problem, you know what the reason is when you encounter a network problem.

Looking at the TCP/TP layer 4 protocol, I can see that we programmers actually need to use

These four layers, in the OSI model.

1. Network media layer

The physical layer and the data link layer correspond to the "network media layer"

Each layer is to solve a problem, it is to solve how one hardware device and another hardware device communicate physically. Communication needs to know the address, so the "network media layer" will eventually encapsulate a "MAC" address, the address of the hardware, to communicate.

2 IP layer

At this layer, the physical connection between one device and another device has been realized, but what we need is the data that is actually received. At this time, we need to determine who will receive it. Then we encapsulate an IP address for each device through the IP layer.

Then assigned by the switch, in this layer of program members need to do is: IP address to establish a connection, the current IP address is based on IPV4 is a 4-byte IP address. This network layer only needs to know the IP-to-IP address

What is the purpose of the agreement? How is the agreement implemented?

It is very simple, and the principle is also very simple. We add a header to the data sent by the user, and finally the receiver removes it through unpacking layer by layer.

In order to deeply understand the above picture, let's download a package grab software to have a look, Wireshark, which can be downloaded directly by Baidu, and take a look at the protocol of HTTP.

Grab a whole bag at random (Get's)

Let's take a look at the content of the analysis first.

1 frame the lowest frame data, that is to say, by layering the TCP, he will put the data

Cut the packet into frame by frame

2 Ethernet II he corresponds to the address, destination, and source address of the MAC at the link layer

3 Internet Protocol, this is the IP layer mentioned above, and here is the target ID

Source IP and other information

4 Transmission transport layer, port number, original port number, destination port

5 Hypertext Transfer Protocol application layer, in the picture is

The Http protocol will send a Get request to the

Need to do cross-platform, need to use Linux. Ubuntu 16.04. Get here first.

Thank you for reading! This is the end of the article on "how to achieve cross-platform socket in C++". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report