Analysis of socket programming structure based on TCP/IP and UDP protocol __ programming

Source: Internet
Author: User

1. Socket (socket)

Sockets originate from UNIX, and one of the basic philosophies of unix/linux is "everything is a file," which can be manipulated with the "open open–> read-write write/read–> turn off close" mode. Socket is an implementation of this mode, the socket is a special file, some of the socket function is the operation of it (read/write Io, open, closed). The white socket is the intermediate software abstraction layer of the application layer communicating with the TCP/IP protocol family, which is a set of interfaces. In the design model, the socket is actually a façade mode, which is the complex TCP/IP protocol family hidden behind the socket interface, for users, a group of simple interface is all, let the socket to organize data to meet the specified protocol.

With the application of UNIX, sockets have been introduced to Windows and other operating systems. Sockets typically exchange data only with sockets in the same area, and Windows Sockets support only one area of communication: The Internetwork (af_inet), which is used by a communication process that forgets the protocol cluster.


2.socket Communication Model




3. Decomposition and encapsulation of data transmitted based on TCP/IP protocol



4. Client/server Mode

In TCP/IP network applications, the main mode of interaction between two processes of communication is client/server mode * (Client/server), that is, customers like service their request, the server receives the request, provides the corresponding service.

Server:

(1) First, the server to start , open a communication channel and inform the computer, it is willing to a certain address and port to receive customer requests

(2) Wait for customer request to reach the port

(3) Receiving a service request, processing the customer request, after the service completes, closes the communication link between this new process and the customer, and terminates

(4) Return to the second step, waiting for another customer request

(5) Shutdown server

Client side:

(1) Open a communication channel and connect to the host-specific port on which the server resides

(2) Send the request to the server, wait and receive the reply, continue to make the request

(3) Close the communication channel and terminate after the request is completed


5. Type of socket socket

(1) streaming socket (SOCK_STREAM)

Provide connection-oriented, reliable data transfer service, no error, no duplication, and receive in Send Order (TCP protocol)

(2) Datagram Socket (SOCK_DGRAM)

Provides connectionless services, packets are sent in separate packages, no flawless warranties, data may be lost, and receive order confusion (UDP protocol)

(3) original socket (Sock_ram)


6. TCP (Connection-oriented)-based socket programming


The server side initializes the socket, then binds to the port (BIND), listens to the port (listen), calls the accept block, and waits for the client to connect. At this point if a client initializes a socket and then connects to the server (connect), if the connection succeeds, then 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, and then sends the response data to the client, the client reads the data, closes the connection, and ends the interaction.


7. Socket programming based on UDP (non-connection oriented)


The server first creates the socket, binds the socket (BIND) to a local address and port, and waits for data transfer (RECVFROM). This time if a client creates a socket and sends data to the server (sendto), the server establishes a connection, communicates the data, and closes the connection after the connection is completed.

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.