php socket通信(tcp/udp)
注意
1.在socket_bind的时候ip地址不能真回环地址如127.0.0.1
2.server.php后台跑起来的时候 nohup php server.php > /var/tmp/a.log 2>&1 &
一: udp 方式
1) server.php
<span style="color: #000000;">php </span><span style="color: #008000;">//</span><span style="color: #008000;">error_reporting( E_ALL );</span> <span style="color: #008080;">set_time_limit</span>( 0<span style="color: #000000;"> ); </span><span style="color: #008080;">ob_implicit_flush</span><span style="color: #000000;">(); </span><span style="color: #800080;">$socket</span> = socket_create( AF_INET, SOCK_DGRAM,<span style="color: #000000;"> SOL_UDP ); </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$socket</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;"> ) { </span><span style="color: #0000ff;">echo</span> "socket_create() failed:reason:" . socket_strerror( socket_last_error() ) . "\n"<span style="color: #000000;">; } </span><span style="color: #800080;">$ok</span> = socket_bind( <span style="color: #800080;">$socket</span>, '202.85.218.133', 11109<span style="color: #000000;"> ); </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$ok</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;"> ) { </span><span style="color: #0000ff;">echo</span> "socket_bind() failed:reason:" . socket_strerror( socket_last_error( <span style="color: #800080;">$socket</span><span style="color: #000000;"> ) ); } </span><span style="color: #0000ff;">while</span> ( <span style="color: #0000ff;">true</span><span style="color: #000000;"> ) { </span><span style="color: #800080;">$from</span> = ""<span style="color: #000000;">; </span><span style="color: #800080;">$port</span> = 0<span style="color: #000000;">; socket_recvfrom( </span><span style="color: #800080;">$socket</span>, <span style="color: #800080;">$buf</span>,1024, 0, <span style="color: #800080;">$from</span>, <span style="color: #800080;">$port</span><span style="color: #000000;"> ); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$buf</span><span style="color: #000000;">; </span><span style="color: #008080;">usleep</span>( 1000<span style="color: #000000;"> ); } </span>?>
2) client.php
<span style="color: #000000;">php </span><span style="color: #800080;">$sock</span> = socket_create(AF_INET, SOCK_DGRAM,<span style="color: #000000;"> SOL_UDP); </span><span style="color: #800080;">$msg</span> = 'hello'<span style="color: #000000;">; </span><span style="color: #800080;">$len</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$msg</span><span style="color: #000000;">); socket_sendto(</span><span style="color: #800080;">$sock</span>, <span style="color: #800080;">$msg</span>, <span style="color: #800080;">$len</span>, 0, '202.85.218.133', 11109<span style="color: #000000;">); socket_close(</span><span style="color: #800080;">$sock</span><span style="color: #000000;">); </span>?>
一: TCP 方式
1)server.php
<span style="color: #000000;">php </span><span style="color: #008000;">//</span><span style="color: #008000;">error_reporting( E_ALL );</span> <span style="color: #008080;">set_time_limit</span>( 0<span style="color: #000000;"> ); </span><span style="color: #008080;">ob_implicit_flush</span><span style="color: #000000;">(); </span><span style="color: #800080;">$socket</span> = socket_create( AF_INET, SOCK_STREAM,<span style="color: #000000;"> SOL_TCP ); socket_bind( </span><span style="color: #800080;">$socket</span>, '192.168.2.143', 11109<span style="color: #000000;"> ); socket_listen(</span><span style="color: #800080;">$socket</span><span style="color: #000000;">); </span><span style="color: #800080;">$acpt</span>=socket_accept(<span style="color: #800080;">$socket</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "Acpt!\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">while</span> ( <span style="color: #800080;">$acpt</span><span style="color: #000000;"> ) { </span><span style="color: #800080;">$words</span>=<span style="color: #008080;">fgets</span><span style="color: #000000;">(STDIN); socket_write(</span><span style="color: #800080;">$acpt</span>,<span style="color: #800080;">$words</span><span style="color: #000000;">); </span><span style="color: #800080;">$hear</span>=socket_read(<span style="color: #800080;">$acpt</span>,1024<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$hear</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>("bye\r\n"==<span style="color: #800080;">$hear</span><span style="color: #000000;">){ socket_shutdown(</span><span style="color: #800080;">$acpt</span><span style="color: #000000;">); </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; } </span><span style="color: #008080;">usleep</span>( 1000<span style="color: #000000;"> ); } socket_close(</span><span style="color: #800080;">$socket</span><span style="color: #000000;">) </span>?>
2) client.php
<span style="color: #000000;">php </span><span style="color: #800080;">$socket</span> = socket_create(AF_INET, SOCK_STREAM,<span style="color: #000000;"> SOL_TCP); </span><span style="color: #800080;">$con</span>=socket_connect(<span style="color: #800080;">$socket</span>,'192.168.2.143',11109<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$con</span>){socket_close(<span style="color: #800080;">$socket</span>);<span style="color: #0000ff;">exit</span><span style="color: #000000;">;} </span><span style="color: #0000ff;">echo</span> "Link\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$con</span><span style="color: #000000;">){ </span><span style="color: #800080;">$hear</span>=socket_read(<span style="color: #800080;">$socket</span>,1024<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$hear</span><span style="color: #000000;">; </span><span style="color: #800080;">$words</span>=<span style="color: #008080;">fgets</span><span style="color: #000000;">(STDIN); socket_write(</span><span style="color: #800080;">$socket</span>,<span style="color: #800080;">$words</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$words</span>=="bye\r\n"){<span style="color: #0000ff;">break</span><span style="color: #000000;">;} } socket_shutdown(</span><span style="color: #800080;">$socket</span><span style="color: #000000;">); socket_close(</span><span style="color: #800080;">$sock</span><span style="color: #000000;">); </span>?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

According to news from this site on June 24, at the keynote speech of the HDC2024 Huawei Developer Conference on June 21, Gong Ti, President of Huawei Terminal BG Software Department, officially announced Huawei’s self-developed Cangjie programming language. This language has been developed for 5 years and is now available for developer preview. Huawei's official developer website has now launched the official introductory tutorial video of Cangjie programming language to facilitate developers to get started and understand it. This tutorial will take users to experience Cangjie, learn Cangjie, and apply Cangjie, including using Cangjie language to estimate pi, calculate the stem and branch rules for each month of 2024, see N ways of expressing binary trees in Cangjie language, and use enumeration types to implement Algebraic calculations, signal system simulation using interfaces and extensions, and new syntax using Cangjie macros, etc. This site has tutorial access address: ht

This site reported on June 21 that at the HDC2024 Huawei Developer Conference this afternoon, Gong Ti, President of Huawei Terminal BG Software Department, officially announced Huawei’s self-developed Cangjie programming language and released a developer preview version of HarmonyOSNEXT Cangjie language. This is the first time Huawei has publicly released the Cangjie programming language. Gong Ti said: "In 2019, the Cangjie programming language project was born at Huawei. After 5 years of R&D accumulation and heavy R&D investment, it finally meets global developers today. Cangjie programming language integrates modern language features, comprehensive compilation optimization and Runtime implementation and out-of-the-box IDE tool chain support create a friendly development experience and excellent program performance for developers. "According to reports, Cangjie programming language is an all-scenario intelligence tool.

According to news from this site on June 21, Huawei’s self-developed Cangjie programming language was officially unveiled today, and the official announced the launch of HarmonyOSNEXT Cangjie language developer preview version Beta recruitment. This upgrade is an early adopter upgrade to the developer preview version, which provides Cangjie language SDK, developer guides and related DevEcoStudio plug-ins for developers to use Cangjie language to develop, debug and run HarmonyOSNext applications. Registration period: June 21, 2024 - October 21, 2024 Application requirements: This HarmonyOSNEXT Cangjie Language Developer Preview Beta recruitment event is only open to the following developers: 1) Real names have been completed in the Huawei Developer Alliance Certification; 2) Complete H

According to news from this site on June 22, Huawei yesterday introduced Huawei’s self-developed programming language-Cangjie to developers around the world. This is the first public appearance of Cangjie programming language. According to inquiries on this site, Tianjin University and Beijing University of Aeronautics and Astronautics were deeply involved in the research and development of Huawei’s “Cangjie”. Tianjin University: Cangjie Programming Language Compiler The software engineering team of the Department of Intelligence and Computing of Tianjin University joined hands with the Huawei Cangjie team to deeply participate in the quality assurance research of the Cangjie programming language compiler. According to reports, the Cangjie compiler is the basic software that is symbiotic with the Cangjie programming language. In the preparatory stage of the Cangjie programming language, a high-quality compiler that matches it became one of the core goals. As the Cangjie programming language evolves, the Cangjie compiler is constantly being upgraded and improved. In the past five years, Tianjin University

According to news from this site on June 21, before the HDC2024 Huawei Developer Conference, Huawei’s self-developed Cangjie programming language was officially unveiled, and the Cangjie official website is now online. The official website introduction shows that Cangjie programming language is a new generation programming language for all-scenario intelligence, focusing on "native intelligence, natural all-scenarios, high performance, and strong security." Integrate into the Hongmeng ecosystem to provide developers with a good programming experience. The official website attached to this site introduces as follows: Native intelligent programming framework embedded with AgentDSL, organic integration of natural language & programming language; multi-Agent collaboration, simplified symbolic expression, free combination of patterns, supporting the development of various intelligent applications. Innately lightweight and scalable runtime for all scenes, modular layered design, no matter how small the memory is, it can be accommodated; all-scenario domain expansion

Original title: "How does a wireless mouse become wireless?" 》Wireless mice have gradually become a standard feature of today’s office computers. From now on, we no longer have to drag long cords around. But, how does a wireless mouse work? Today we will learn about the development history of the No.1 wireless mouse. Did you know that the wireless mouse is now 40 years old? In 1984, Logitech developed the world's first wireless mouse, but this wireless mouse used infrared as a The signal carrier is said to look like the picture below, but later failed due to performance reasons. It was not until ten years later in 1994 that Logitech finally successfully developed a wireless mouse that works at 27MHz. This 27MHz frequency also became the wireless mouse for a long time.

According to news on June 21, this afternoon, Huawei Developer Conference 2024 will be officially opened. "Pure-blood Hongmeng" Harmony OS NEXT is naturally a top priority. According to the plan previously revealed by Yu Chengdong, the public beta may be officially announced this afternoon, and ordinary consumers can also try out "pure-blood Harmony". According to reports, the first batch of supported mobile phones are the Mate60 series and Pura70 series. It is worth noting that as a "pure-blooded Hongmeng", HarmonyOSNEXT has removed the traditional Linux kernel and AOSP Android open source code and developed the entire stack in-house. According to the latest report from Sina Technology, Huawei will also complete the last link of Hongmeng Ecosystem and expand its presence in the world.

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not
