Network programming----------socket programming for a simple TCP protocol

Source: Internet
Author: User

First, we need to know a few things about TCP:

1.TCP Features: Connection-oriented reliability transmission

The 2.TCP three-time handshake establishes the connection and four waves to release the connection, but why does TCP need three handshake to establish the connection?

A: Because the two handshake cannot guarantee the reliability, if the last failure, then the client will re-initiate the connection request, then must consume the resources, and three times handshake obviously can achieve this purpose.

Why is 3.TCP connection-oriented?

A: Because TCP is the transport layer of the Protocol, the lower layer of the transport level is the network layer, the IP protocol is the network layer of protocol, but IP only provides the best-effort transport mechanism, and the transport layer of the upper level is the application layer, it is responsible for the lower data out, Therefore, the reliability of the data at this time to ensure that the responsibility to protect the transport layer on the TCP protocol (the same layer of UDP is not responsible for data detection and other functions). And the precondition of reliability guarantee is connection-oriented.

Who will guarantee the 4.TCP reliability transmission mechanism?

A: The TCP message segment format has 32-bit sequence number and 32-bit confirmation sequence number two parts, so the reliability is obviously guaranteed by these two parts.

5. What are the TCP-related timers?

A: There are four kinds: (1) Retransmission timer: Solve the problem of time-out retransmission

(2) Stick timer: Solve the deadlock problem caused by 0 window size

(3) keepalive timer: Resolves a long-time non-data transfer between two TCP connections

(4) 2MSL timer: Measure the time when a connection is time-wait. (When the server disconnects, if the new client then requests a broken port connection, then the server will be in time-wait waiting for the original client to request the connection again.)

OK, here's the point about TCP, but it's worth knowing. Ha ha!

Below, we use the socket for simple TCP programming

What exactly is the socket we used to say? Yes, that's what we know------------. IP address + port number =socket

First, learn the basic functions.




All right, the code is officially posted!

Server-Side TCP_SERVER.C












Makefile File:



Client code tcp_client.c:














































Network programming----------socket programming for a simple TCP protocol

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.