For TCP/IP, UDP, socket programming These words you are not very strange, right? With the development of network technology, these words are filled with our ears. Then I would like to ask:
1. What is TCP/IP, UDP?
2. Where is the socket?
3. What is the socket?
4. Will you use them?
What is TCP/IP, UDP?
TCP/IP (Transmission Control protocol/internet Protocol), the Transmission Control Protocol/Internetwork Protocol, is an industrial standard protocol set that is designed for wide area networks (WANs).
UDP (User data Protocol, Subscriber Datagram Protocol) is a protocol corresponding to TCP. It is a part of the TCP/IP protocol family.
Here is a diagram that shows the relationship of these protocols.
The TCP/IP protocol family includes transport layer, network layer and link layer. Now you know the relationship between TCP/IP and UDP.
In Figure 1, we don't see the socket shadow, so where is it? Or use the diagram to speak, at a glance.
What is a socket?
The socket is the intermediate software abstraction layer of the application layer communicating with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, which is the complex TCP/IP protocol family hidden behind the socket interface, for users, a group of simple interface is all, let the socket to organize data to meet the specified protocol.
Will you use them?
Predecessors have done a lot of things to us, the network communication between a lot of simple, but after all, there are quite a lot of work to do. Previously heard socket programming, think it is more advanced programming knowledge, but as long as the understanding of the work of socket programming, the veil of mystery will be uncovered.
A scene in life. You have to call a friend, dial first, the friend heard the phone call after the phone, then you and your friend set up a connection, you can talk. And so on, hang up the phone and end this conversation. The life of the scene explains how this works, perhaps the TCP/IP protocol family is born in life, this is not necessarily.
Start with the server side. The server side initializes the socket, then binds to the port (BIND), listens to the port (listen), calls the accept block, and waits for the client to connect. At this point if a client initializes a socket and then connects to the server (connect), if the connection succeeds, then the connection between the client and the server is established. The client sends a data request, the server receives the request and processes the request, and then sends the response data to the client, the client reads the data, closes the connection, and ends the interaction.
Socket related functions:
———————————————————————————————-
Socket_accept () accepts a socket connection
Socket_bind () binds the socket to an IP address and port
Socket_clear_error () Clears the socket error or the last error code
Socket_close () closes a socket resource
Socket_connect () Start a socket connection
Socket_create_listen () opens a socket listener on the specified port
Socket_create_pair () produces a pair of indistinguishable sockets into an array
Socket_create () produces a socket, which is equivalent to generating a socket data structure
Socket_get_option () Get socket option
Socket_getpeername () Gets the IP address of a remote similar host
Socket_getsockname () Gets the IP address of the local socket
Socket_iovec_add () adds a new vector to a scatter/aggregate array
Socket_iovec_alloc () This function creates a IOVEC data structure capable of sending and receiving read and write
Socket_iovec_delete () deletes an already allocated Iovec
Socket_iovec_fetch () returns the data for the specified Iovec resource
Socket_iovec_free () frees a Iovec resource
Socket_iovec_set () sets the Iovec data new value
Socket_last_error () Gets the last error code for the current socket
Socket_listen () listens for all connections by the specified socket
Socket_read () reads the specified length of data
SOCKET_READV () reads data coming from a scatter/aggregate array
SOCKET_RECV () end data from socket to cache
Socket_recvfrom () accepts data from the specified socket and, if not specified, the default current socket
Socket_recvmsg () received a message from Iovec
Socket_select () multi-channel selection
Socket_send () This function sends data to the connected socket
SOCKET_SENDMSG () Send message to socket
Socket_sendto () sends a message to the socket at the specified address
Socket_set_block () is set to block mode in the socket
Socket_set_nonblock () socket is set to not block mode
Socket_set_option () Set socket options
Socket_shutdown () This function allows you to close a read, write, or specified socket
Socket_strerror () returns a detailed error for the specified error number
Socket_write () write data to the socket cache
Socket_writev () write data to a scatter/aggregate array
Case ONE: Socket communication Demo
Server side:
PY by Http://my.oschina.net/cart
<?php
Ensure that the client is not timed out when connected
Set_time_limit (0);
$ip =