Chapter 8 Basic UDP socket programming

TCP: connection-oriented, reliable byte stream.
UDP: connectionless, unreliable datagram protocol.

UDP: DNS Domain Name System, NFS network file system, SNMP SNMP.

#include <SYS / socket.h>
an ssize_t recvfrom (int sockfd, BUFF void *, size_t nbytes, the flags int,
             struct from the sockaddr *, * the socklen_t addrlen);
Returns: OK -> the number of bytes read, ERROR -> - 1.
sockfd: socket descriptor.
buff: write pointer points to the buffer.
nbytes: size of the buffer.
the flags:
from: protocol address pointer points to.
addrlen: length pointer to point to a structure.

#include <SYS / socket.h>
an ssize_t the sendto (int sockfd, const void * BUFF, size_t nbytes, the flags int,
            const struct to the sockaddr *, the socklen_t addrlen);
Returns: OK -> the number of bytes read, ERROR -> -1.
sockfd: socket descriptor.
buff: a buffer read pointer points.
nbytes: size of the buffer.
flags:
to: a pointer to a protocol address.
addrlen: length pointer to point to a structure.

For datagram protocol, recvfrom returns a value of 0 is possible: it does not mean that the other side has closed the connection, it returns 0 to read TCP socket different. Because UDP is a connectionless, which did not close things such as UDP connections and the like.

recvfrom and sendto can be used in TCP, although in general there is no reason to do so.
Generally, TCP servers are concurrent, and UDP servers are iterative.

Weak End System Model: Most IP address to achieve the purpose of receiving any data packet present a host IP address, regardless of the interface data packets arrive.
Strong end-system model: only accept reach consistent interface with the destination address of the datagram.

Reproduced in: https: //www.cnblogs.com/learne/archive/2009/08/12/1544669.html

Guess you like

Origin blog.csdn.net/weixin_33757609/article/details/93228118