LINUX:TCP and UDP are different

Source: Internet
Author: User
Tags function prototype

Linux Network Programming 4--Personal Summary

TCP and UDP communication flow

The basic steps for TCP communication are as follows:

Server: Socket---BIND---listen---while (1) {---accept---recv---send---close---}------close

Client: Socket------------------------------Connect---Send---recv-----------------close

The basic steps for UDP communication are as follows:

Server: Socket---bind---recvfrom---sendto----close

Client: Socket----------sendto----recvfrom---Close

Function prototype TCP
Tcp#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h > #include <unistd.h> #include <string.h> #include <stdio.h> #include <stdlib.h>server:int socket (int domain, int type, int protocol), int bind (int fd_server, struct sockaddr *addr_server, int addrlen); /* Bind the server contact, let the client have clear contact method can connect */int listen (int fd_server, int backlog); /* Convert Fd_server to passive socket, and the kernel will listen the requested contact into the queue */int accept (int fd_server, struct sockaddr *addr_client, int *addrlen); /* Return to the other end of the client socket to establish a connection *//* addr_client as an outgoing parameter that holds the contact information for the requesting connector, such as the need not to deliberately null */client:int connect (int fd_client,struct SOCKADDR *addr_server, int addrlen);             /* Through the server contact Addr_server to connect to the server *//* as a server, must bind contact, otherwise the requester does not have a clear contact method can be connected, as a client, can not be bound contact, its port number will be automatically assigned by the system.                              The key to establishing the connection is that the Accept function returns a descriptor for the socket at the other end of the client socket descriptor, and the client's contact can also be obtained using the outgoing function in the accept. * * After connect:/* establishes the connection, you can use the socket descriptor to send the received message */int recv (int sockfd,void *buf,inT len,unsigned int flags); 
int send (int s,const void * Msg,int len,unsigned int flags);
/* End of Session, close */int close (int fd);
Udp
Udpserver:int socket (int domain, int type, int protocol),/* Bind server contact, let client have clear contact method can connect */int bind (int fd_server, struct so CKADDR *addr_server, int addrlen); /* Since Fd_server has already bound contact (addr_server), the requestor sends the message via Addr_server to the server fd_server, so the server can receive the message via Fd_server, And the requester's contact can be obtained via outgoing parameter addr_client */int recvfrom (int fd_server, void *buf, int len, unsigned int flags, STRUC T sockaddr *addr_client, int *addrlen);   /* The requestor's contact information (addr_client) is bound by the system itself, and the server gets addr_client through the recvfrom's outgoing parameters.                  Therefore, when the server sends a message, it can be sent to the client fd_client via client contact addr_client. */int sendto (int fd_server, const void *msg, int len, unsigned int flags, const struct SOCKADDR *addr_client, int addrlen) client:/* client contact, the server can be obtained at recvfrom, so it is not necessary to bind */int SendTo at the beginning (int fd_server, const void *msg, int len, unsigned int fla GS, const struct SOCKADDR *addr_client, int addrlen); int recvfrom (int fd_server, void *buf, int len, unsigned int flags, s Truct sockaddr *addr_client, int *addrlen); /* In fact, UTP Communication does not have a socket connection, and bothAre communicated by means of contact (IP and port number).   The server needs to bind the contact at the beginning, otherwise the requester does not have a clear contact way to connect.            When the server recvfrom, not only can receive the client's message, but also can obtain the client's contact information. */
Summarize

1. TCP communication will establish a connection. The socket port of the server will bind the contact method, give the client a definite contact way to connect. The server listen to the client's connect and puts its contact information into the task request queue maintained by the kernel. After the server accept, get the corresponding socket port to communicate with the client, so as to establish the connection! The two sides then communicate through the socket port.

2. UDP communication does not establish a connection. The socket port of the server will also bind the contact method. The client sends a (SENDTO) message to the server through this contact, because this contact is bound to the server's socket port, so the server must be able to receive (RECVFROM) messages, at the same time through the outgoing parameters of Recvfrom to obtain the client's contact information, This allows messages to be sent to the client through the client's contact. That is, UTP communication is based on the contact method (IP and port number). is an unreliable means of communication.

3. Why is the accept and UDP recvfrom in TCP get contact information via outgoing parameters? If you don't understand it, you can send a letter by analogy, and when someone sends you a message, always write your own contact information.

LINUX:TCP and UDP are different

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.