Unveil Socket programming (1)

Source: Internet
Author: User

Are you familiar with TCP/IP, UDP, and Socket programming? With the development of network technology, these words are filled with our ears. So I want to ask:

1. What are TCP/IP and UDP?

2. Where is the Socket?

3. What is Socket?

4. Will you use them?

What is TCP/IP, UDP?

TCP/IPTransmission Control Protocol/Internet Protocol) is an industrial standard Protocol set designed for Wan WANs.

UDPUser Data Protocol (User Datagram Protocol) is the Protocol corresponding to TCP. It belongs to the TCP/IP protocol family.

The following figure shows the relationship between these protocols.

Figure 1

TCP/IP protocol families include transport layer, network layer, and link layer. Now you know the relationship between TCP/IP and UDP.

SocketWhere?

In Figure 1, we don't see the Socket shadow, so where is it? You can still use graphs to speak clearly.

Figure 2

The original Socket is here.

SocketWhat is it?

Socket is an intermediate software abstraction layer for communications between the application layer and the TCP/IP protocol family. It is a group of interfaces. In the design mode, Socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. for users, a set of simple interfaces are all, let the Socket organize the data to conform to the specified protocol.

Will you use them?

Our predecessors have already done a lot for us, and the communication between networks is much simpler, but after all there is still a lot of work to do. I have heard about Socket programming before and think it is a relatively advanced programming knowledge. But as long as I understand the working principle of Socket programming, the mysterious veil will be uncovered.

A scenario in life. You need to call a friend to make a dial-up call. When a friend hears the ringtone and calls the phone, you and your friend can establish a connection to speak. After the communication is over, stop the call. The scenario in life explains this working principle. Maybe the TCP/IP protocol family is born in life, which is not necessarily true.

Figure 3

Start with the server. The server first initializes the Socket, then binds it to the port (bind), listens to the port (listen), calls accept, and waits for the client to connect. At this time, if a client initializes a Socket and connects to the server (connect), if the connection is successful, the connection between the client and the server is established. The client sends a data request. The server receives the request and processes the request. Then, the response data is sent to the client. The client reads the data and closes the connection. The interaction ends.

Here I will give a simple example. We are following the TCP protocol path, as shown in figure 2 ). The example is written in MFC. The running interface is as follows:

 


Figure 4

Figure 5

Enter the Server IP address and sent data on the client, and then press the send button. The server receives the data and then responds to the client. The client reads the response data and displays it on the interface.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.