套接字 (socket) 是一个通信端的标识符,由IP地址和端口号组成,例如<192.168.10.31:194>.作用是提供一个数据传输通道。
套接字主要有3种类型。
1)流式socket(SOCKET_STREAM):提供可靠地、面向连接的通信流。使用TCP协议,从而保证数据的正确性。
2)数据包socket(SOCKET_DGRAM):提供一种无连接的服务,使用DUP协议,通过相互独立的数据报传输,协议不保证传输的可靠性和数据的原始顺序。
3)原始socket:原始socket允许对底层协议如IP进行直接访问,功能强大,用户可以通过该socket开发自己的协议。
三次握手
四次挥手
对于W5500里面的部分源码贴出:
/**
*@brief This Socket function initialize the channel in perticular mode,
and set the port and wait for W5200 done it.
建立一个socket
*@param s: socket number.
*@param protocol: The socket to chose.
*@param port:The port to bind.
*@param flag: Set some bit of MR,such as **< No Delayed Ack(TCP) flag.
*@return 1 for sucess else 0.
*/
/**
*@brief This function established the connection for the channel in Active (client) mode.
This function waits for the untill the connection is established.
*@param s: socket number.
*@param addr: The server IP address to connect
*@param port: The server IP port to connect
*@return 1 for success else 0.
*/