Introduction to socket-based UDP and TCP Programming

Source: Internet
Author: User

I. Overview

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two different communication protocols at the transport layer in the network architecture TCP/IP model.

TCP: Transmission Control Protocol, a connection-oriented protocol that provides a reliable full-duplex byte stream for user processes. The TCP set interface is a type of streamsocket interface.

UDP: User Datagram Protocol. UDP is a connectionless protocol. A udp interface is a type of data PACKET socket.

II. Introduction to TCP and UDP

1) Basic tcp client-server programming framework

Note: (three-way handshake)
1. The client sends a SYN segment (synchronization serial number) to indicate the server port to be connected by the customer and the initialization serial number (ISN ).
2. the server sends back the SYN packet segment containing the server's initial serial number as the response. At the same time, set the check serial number (ACK) to the customer's ISN plus 1 to confirm the customer's Syn packet segment. A syn occupies a sequence number.
3. The customer must set the validation serial number to ISN plus 1 of the server to confirm the SYN packet segment of the server.

2) Basic tcp client-server programming framework Flowchart

3) Comparison of UDP and TCP:

From the above flowchart, we can see that UDP does not have a three-way handshake process.

To put it simply. UDP processing has fewer details than TCP. UDP cannot ensure that the message is sent to (It also reports that the message is not sent to) the destination. UDP does not guarantee the Transmission sequence of data packets. After UDP sends data, it can only be expected to reach the destination.

TCP advantages and disadvantages:

Advantages:
1. TCP explicitly creates and terminates connections in an agreed manner.
2. TCP ensures reliable, ordered (packets are received in the sent order), and non-repeated data transmission.
3. TCP processing flow control.
4. Data priority is allowed.
5. If the data is not transmitted, the TCP interface returns an error condition.
6. TCP maintains continuity and divides data blocks into smaller shards to handle big data blocks. -No need for programmers to know

Disadvantage: TCP must create (and maintain) a connection when transferring data. This connection adds overhead to the communication process, making it slower than UDP.

Advantages and disadvantages of UDP:
1. UDP does not require a connection
2. UDP does not incur any overhead because the receiver accepts the receipt of the packet (or automatically retransmits the packet when the packet does not arrive correctly.
3. UDP is designed for short applications and message control.
4. When a data packet is connected to a data packet, UDP requires less network bandwidth than TDP.

Iii. socket programming

A socket interface is an API of a TCP/IP network. A socket interface defines many functions or routines that can be used by programmers to develop applications on a TCP/IP network. To learn TCP/IP network programming on the internet, you must understand the socket interface.

The socket interface designer first places the interface in the UNIX operating system. If you understand the input and output of Unix systems, you can easily understand socket. Network socket data transmission is a special type of I/O, and socket is also a file descriptor. Socket also has a function called socket () similar to opening a file. This function returns an integer socket descriptor, and subsequent connection establishment and data transmission operations are implemented through this socket. There are two common socket types: stream socket (sock_stream) and datagram socket (sock_dgram ). Streaming is a connection-oriented socket for connection-oriented TCP Service applications; datagram socket is a connectionless socket that corresponds to connectionless UDP Service applications.

1. SOCKET call library functions mainly include:

Create socket
Socket (AF, type, Protocol)

Establish connection between address and Socket
BIND (sockid, localaddr, addrlen)

The server listens to client requests.
Listen (sockid, quenlen)

Establish connections between servers and clients (for TCP connections)
Client request connection
Connect (sockid, destaddr, addrlen)
The server waits for receiving client connection requests from the socket numbered sockid.
Newsockid = accept (sockid, clientaddr, paddrlen)

Send/receive data
Connection orientation:Send (sockid, buff, bufflen)
Recv ()
Connection-free:Sendto (sockid, buff ,..., Addrlen)
Recvfrom ()

Release socket
Close (sockid)

 

Details: Http://blog.csdn.net/pbymw8iwm/article/details/6748726

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.