Basic TCP socket programming (1)

Source: Internet
Author: User

Basic TCP socket programming (1)

All TCP-based socket programming clients and servers start from the socket call and return a socket descriptor. The client then calls the connect function, and the server calls the bind, listen, and accept functions. The socket is usually closed using the standard close function, but you can also use the shutdown function to close the socket. The following is an analysis of the function processes called during socket programming. The following is a flowchart of TCP socket-based programming:

Socket Functions

A socket is an abstraction of the Communication endpoint to implement communication between the end-to-end. Similar to an application that uses a file descriptor to access a file, socket descriptors are required to access the socket. Any socket programming must call the socket function to obtain the socket descriptor. The following describes the function:

 
 
  1. /* Socket */
  2. /*
  3. * Function: Create a socket descriptor;
  4. * Return value: If successful, the socket non-negative descriptor is returned. If an error occurs, the value-1 is returned;
  5. * Function prototype:
  6. */
  7. # Include <sys/socket. h>
  8. Int socket (int family, int type, int protocol );
  9. /*
  10. * Note:
  11. * Similar to open operations on common files, socket is a return descriptor. Subsequent operations are based on this descriptor;
  12. * Family indicates the socket communication domain. Different values determine the socket address type. The general values are as follows:
  13. * 1) AF_INET IPv4 internet domain
  14. * 2) AF_INET6 IPv6 Internet Domain
  15. * 3) AF_UNIX Unix domain
  16. * 4) AF_ROUTE route socket
  17. * 5) AF_KEY key socket
  18. * 6) AF_UNSPEC is not specified
  19. *
  20. * Determine the socket type. The common types are as follows:
  21. * 1) _stream ordered, reliable, and bidirectional connection-oriented byte stream socket
  22. * 2) SOCK_DGRAM fixed-length, connectionless, and unreliable datagram socket
  23. * 3) original socket of SOCK_RAW
  24. * 4) SOCK_SEQPACKET is a fixed, ordered, and reliable connection-oriented ordered grouping socket.
  25. *
  26. * Protocol specifies the protocol. common values are as follows:
  27. * 1) 0: select the default Protocol corresponding to the type.
  28. * 2) IPPROTO_TCP transmission protocol
  29. * 3) IPPROTO_UDP UDP transmission protocol
  30. * 4) IPPROTO_SCTP SCTP transmission protocol
  31. * 5) IPPROTO_TIPC TIPC transmission protocol
  32. *
  33. */

Connect Function

When processing connection-oriented network services, such as TCP, a connection must be established between the requested process socket and the process socket that provides the service before data is exchanged. The TCP client can call function connect to establish a connection with the TCP server. The function is described as follows:

 
 
  1. /*
  2. * Function: establishes a connection, that is, the client uses this function to establish a connection with the server;
  3. * Return value: 0 is returned if the operation is successful, and-1 is returned if an error occurs;
  4. * Function prototype:
  5. */
  6. # Include <sys/socket. h>
  7. Int connect (int sockfd, const struct sockaddr * servaddr, socklen_t addrlen );
  8. /*
  9. * Note:
  10. * Sockfd is the socket descriptor called by the system, that is, the socket descriptor returned by the socket function;
  11. * Servaddr is the address of the target socket. The socket address structure must contain the destination IP address and destination port number, that is, the server address to communicate;
  12. * Addrlen is the size of the destination socket address;
  13. *
  14. * If sockfd is not bound to an address, connect binds the caller with a default address, that is, the kernel determines the source IP address and selects a temporary port number as the source port number;
  15. */

The TCP client does not have to call the bind function before calling function connect, because the kernel determines the source IP address and selects a temporary port as the source port number. If the TCP socket calls the connect function, a TCP connection is established (three-way handshake is executed), and the result is returned only when the connection is established successfully or when an error occurs. If an error is returned, the following situations may occur:

If the TCP client does not receive a response from the SYN packet segment, an ETIMEOUT error is returned;

If the response of the client's SYN packet segment is RST (indicating reset), it indicates that the server host has no processes waiting to connect to the specified port. It is just a hard error. The client immediately returns the ECONNERFUSED error as soon as it receives the RST;

RST is a TCP packet segment sent when a TCP error occurs. When three conditions of the RST are generated:

The destination is the SYN of a port, but there is no server being monitored on the port;

TCP wants to cancel an existing connection;

TCP receives a message segment on a non-existent connection;

This is a soft error if the SYN sent by the client raises an ICMP error with an inaccessible destination on a certain vro. The client host kernel saves the message and continues sending SYN (re-transmission) at a certain interval ). If no response is received after a specified time, the saved message (ICMP error) is returned as an EHOSTUNREACH or ENETUNREACH error.


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.