Linux Network Programming 4--Personal Summary

Source: Internet
Author: User
Tags function prototype contact form

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/inch.h>#include<arpa/inet.h>#include<unistd.h>#include<string.h>#include<stdio.h>#include<stdlib.h>Server:intSocketintDomainintTypeintprotocol);intBindintFd_server,structSockaddr *addr_server,intAddrlen);/*bind the server contact, so that the client has a clear contact information can be connected*/intListenintFd_server,intbacklog);/*convert Fd_server to passive sockets while the kernel puts the requested contact listen to the queue*/intAcceptintFd_server,structSockaddr *addr_client,int*addrlen);/*return to the other end of the client socket to establish a connection*//*Addr_client is the outgoing parameter, which holds the contact information of the requesting connector, if it does not need to be intentionally null*/Client:intConnect (intFd_client,structSockaddr *addr_server,intAddrlen);/*Addr_server to connect to the server via the server's contact form*//*as a server, be sure to bind contact, otherwise the requester does not have a clear contact method can be connected, as a client, you can not bind 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:/*Once the connection is established, you can use the socket descriptor to send the received message.*/intRecvintSOCKFD,void*buf,intlen,unsignedintflags);
int Send (int s,constvoid * msg,intint  flags);
/**/int close (int fd);
Udp
Udpserver:intSocketintDomainintTypeintprotocol);/*bind the server contact, so that the client has a clear contact information can be connected*/intBindintFd_server,structSockaddr *addr_server,intAddrlen); /*because Fd_server is already bound by the contact method (Addr_server), the requestor sends the message through Addr_server to the server fd_server, so the server can receive the message via Fd_server, And the requester's contact information can be obtained through the outgoing parameter addr_client*/intRecvfrom (intFd_server,void*buf,intLen, unsignedintFlagsstructSockaddr *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. */intSendTointFd_server,Const void*msg,intLen, unsignedintFlagsConst structSockaddr *addr_client,intAddrlen); Client:/*client contact, the server can be acquired at Recvfrom, so it is not necessary to bind at the outset*/intSendTointFd_server,Const void*msg,intLen, unsignedintFlagsConst structSockaddr *addr_client,intAddrlen);intRecvfrom (intFd_server,void*buf,intLen, unsignedintFlagsstructSockaddr *addr_client,int*Addrlen); /*in fact, the UTP communication method does not establish a socket connection, both sides by means of contact (IP and port number) to communicate.   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 Network Programming 4--personal Summary

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.