Socket programming based on TCP/UDP

Source: Internet
Author: User

Socket programming based on TCP (connection-oriented)
Server-Side order:
1. Create socket (socket)
2. Bind sockets to a local address and port (BIND)
3. Set the socket to listen mode, ready to receive customer requests (listen)
4. Wait for the arrival of the customer's request; When the request is brought in, accept the connection request and return a new socket corresponding to this connection (accept)
5. Communicating with the client using the returned socket (SEND/RECV)
6. Return, wait for another customer request
7. Close socket (closesocket)
Client program:
1. Create socket (socket)
2. Send a connection request to the server (connect)
3. Communicating with the server (SEND/RECV)
4. Close socket (closesocket)

How does the three handshake that TCP establishes the connection take place in the socket of the several functions? Please see:

As you can see, when the client calls connect, the connection request is triggered, the SYN J packet is sent to the server, then connect enters the blocking state, the server hears the connection request, receives the SYN J packet, calls the Accept function to receive the request to send SYN K to the client, ACK j+ 1, then accept into the blocking state, the client receives the server SYN K, after the ACK j+1, then connect returns, and the Syn K Confirmation, the server received an ACK k+1, accept return, this three times the handshake is completed, the connection is established.

Summary: The client's connect returns in the second time of the three handshake, while the server-side accept is returned for the third time in the three-time handshake.

UDP-based socket programming for non-connected
Server-side (receive-side) programs:
1. Create socket (socket)
2. Bind sockets to a local address and port (BIND)
3. Waiting to receive data (RECVFROM)
4. Close socket (closesocket)
Client (sender-side) program
1. Create socket (socket)
2. Sending data to the server (sendto)
3. Close socket (closesocket)

How to implement TCP protocol with UDP protocol?

A little idea: using the UDP protocol to add a TCP header to the packet allows it to implement the basic functions of TCP, including retransmission, sliding windows, slow start and congestion control, and the size of the sliding window can be specified by the command line. That is, the application layer encapsulates a TCP header, and then parses the stack to offset a bit more than a few bytes.

Resources:

[1] Linux socket programming (not limited to Linux).
Http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html

[2] How to implement TCP protocol stack based on UDP?
http://bbs.csdn.net/topics/280046868

Socket programming based on TCP/UDP

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.