Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Related functions and example usage of php socket

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the usage of related functions and examples of php socket". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of related functions and examples of php socket".

What is TCP/IP, UDP?

TCP/IP (Transmission Control Protocol/Internet Protocol), the transmission control protocol / internetwork protocol, is an industry standard protocol set designed for the wide area network (WANs).

UDP (User Data Protocol, user Datagram Protocol) is the protocol corresponding to TCP. It belongs to one of the TCP/IP protocol families.

Here is a diagram that shows the relationship between these protocols.

The TCP/IP protocol family includes the transport layer, the network layer and the link layer. Now you know the relationship between TCP/IP and UDP.

Where is Socket?

In figure 1, we don't see the shadow of Socket, so where is it? Still use the picture to speak, it is clear at a glance.

So Socket is here.

What is Socket?

Socket is the middle software abstraction layer for the communication between the application layer and the TCP/IP protocol family, and it is a group of interfaces. In the design pattern, Socket is actually a facade pattern, which hides the complex TCP/IP protocol family behind the Socket interface. For users, a simple set of interfaces is all, allowing Socket to organize the data to comply with the specified protocol.

Can you use them?

Our predecessors have done a lot for us, and the communication between networks is much easier, but after all, there is still a lot of work to be done. When I heard about Socket programming in the past, I thought it was a relatively advanced programming knowledge, but as long as I found out how Socket programming works, the mystery was lifted.

A scene in life. If you want to call a friend, dial first, and the friend will pick up the phone when he hears the ringtone, and then you and your friend will have a connection and can talk. When the exchange is over, hang up the phone and end the conversation. The scenes in life explain how this works. Maybe the TCP/IP protocol family was born in life, but not necessarily.

Let's start with the server side. The server initializes Socket, then binds to the port (bind), listen the port, calls accept blocking, 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 is successful, the connection between the client and the server is established. The client sends the data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, finally closes the connection, and an interaction ends.

Socket related functions:

-

Socket_accept () accepts a Socket connection

Socket_bind () binds 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 () starts a socket connection

Socket_create_listen () opens a socket snooping on the designated port

Socket_create_pair () produces an undifferentiated pair of socket into an array

Socket_create () generates a socket, which is equivalent to generating a socket data structure

Socket_get_option () gets the 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 dispersed / aggregated array

The function socket_iovec_alloc () creates an iovec data structure that can send and receive reads and writes.

Socket_iovec_delete () deletes an assigned iovec

Socket_iovec_fetch () returns data for the specified iovec resource

Socket_iovec_free () releases an iovec resource

Socket_iovec_set () sets the new data value of iovec

Socket_last_error () gets the last error code of the current socket

Socket_listen () listens for all connections by the specified socket

Socket_read () reads data of specified length

Socket_readv () reads data from dispersed / aggregated arrays

Socket_recv () terminates data from socket to cache

Socket_recvfrom () accepts data from the specified socket, and defaults to the current socket if no one is specified

Socket_recvmsg () receives messages from iovec

Socket_select () Multiplexing

The function socket_send () sends data to the connected socket

Socket_sendmsg () sends a message to socket

Socket_sendto () sends a message to the socket at the specified address

Socket_set_block () is set to block mode in socket

Set to non-block mode in socket_set_nonblock () socket

Socket_set_option () sets the socket option

The function socket_shutdown () allows you to turn off read, write, or specified socket.

Socket_strerror () returns the detailed error of the specified error number

Socket_write () writes data to socket cache

Socket_writev () writes data to a dispersed / aggregated array

Case 1: socket communication demonstration

Server side:

This is the server code of socket. Then run cmd, pay attention to the storage path of your own program.

No response, now the server program has started to run, the port has begun to listen. Run netstat-ano to check the port. Mine is port 1935.

Look, the port is already in the LISTENING state. Next, we just need to run the client program to connect. Upper code

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report