您的位置 首页 php

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

 <?php

//error_reporting( E_ALL );
set_time_limit( 0 );
ob_implicit_flush();
$socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
if ( $socket === false ) {
echo "socket_create() failed:reason:" . socket_strerror( socket_last_error() ) . "\n";
}
$ok = socket_bind( $socket, '202.85.218.133', 11109 );
if ( $ok === false ) {
echo "socket_bind() failed:reason:" . socket_strerror( socket_last_error( $socket ) );
}
while ( true ) {
$from = "";
$port = 0;
socket_recvfrom( $socket, $buf,1024, 0, $from, $port );
echo $buf;
 usleep ( 1000 );
}
?>  

2) client.php

 <?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$msg = 'hello';
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, '202.85.218.133', 11109);
socket_close($sock);
?>  

二: TCP 方式

1)server.php

 <?php
//error_reporting( E_ALL );
set_time_limit( 0 );
ob_implicit_flush();
$socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
socket_bind( $socket, '192.168.2.143', 11109 );
socket_listen($socket);
$acpt=socket_accept($socket);
echo "Acpt!\n";
while ( $acpt ) {
$words=fgets(STDIN);
socket_write($acpt,$words);
$hear=socket_read($acpt,1024);
echo $hear;
if("bye\r\n"==$hear){
socket_shutdown($acpt);
break;
}
usleep( 1000 );
}
socket_close($socket)
?>  

2) client.php

 <?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$con=socket_connect($socket,'192.168.2.143',11109);
if(!$con){socket_close($socket);exit;}
echo "Link\n";
while($con){
$hear=socket_read($socket,1024);
echo $hear;
$words=fgets(STDIN);
socket_write($socket,$words);
if($words=="bye\r\n"){break;}
}
socket_shutdown($socket);
socket_close($sock);
?>  

文章来源:智云一二三科技

文章标题:php socket通信(tcp/udp)

文章地址:https://www.zhihuclub.com/77739.shtml

关于作者: 智云科技

热门文章

评论已关闭

38条评论

  1. When the algae die and decompose, this dramatically reduces the amount of oxygen in the water creating hypoxic zones, more commonly called dead zones, in which organisms cannot survive

  2. Your doctor and Hims support staff help you make sense of it all, but it would be a lot more efficient if patients could review that information without working for it

  3. This definition presents a challenging problem for patients without other medical problems but with symptoms of low testosterone T who do not meet the biochemical criteria for therapy.

  4. Caution should be used with concomitant intake of drugs which are known to significantly inhibit cytochrome P 450 IIIA 4.

  5. Menopause 2009; 16 1167 77 This contains the highest naturally occurring melatonin of any food beverage on the planet

  6. PubMed PEG PLA nanoparticles modified with APTEDB peptide for enhanced anti angiogenic and anti glioma therapy

  7. Serious Use Alternative 1 rifampin decreases levels of nintedanib by P glycoprotein MDR1 efflux transporter

  8. C, mGFP myelin rings were present at 16 fold higher density in the rostral compared with the caudal segment of the CC p t test Although currently available risk assessment tools can identify the number of cases of breast cancer expected to arise in a given population, better tools for predicting breast cancer risk in individual women are needed

  9. Immunohistochemical analysis of NO GC deletion in vascular smooth muscle So far, the people have been followed for up to 2 years

  10. PR, Tottenham 13th Jun 2021 The clinical symptoms of recurrence provide tangible evidence of the seriousness of the situation, and the emotions that had been elicited at the time of diagnosis tend to recur and are intensified, particularly with respect to depressive symptoms

  11. This is probably the most complicated portion of the DUTCH test but do it in a step- by- step fashion It is a non ionic polyol surface active agent used as a fecal softener and preventive bloat treatment in cattle

  12. In vitro fertilization is a treatment for infertility and is not done as a test, but it does give us some information about egg and embryo quality Corticosteroids, such as prednisone, are frequently prescribed for asthma and can cause similar medication side effects

  13. In most cases, most men will display two symptoms, normally one of the sexually related and one more from the list Taking an accurate medication and allergy history in the current era of polypharmacy is vital, particularly in light of the ever expanding availability of prescription and non prescription drugs, herbal remedies and supplements

  14. I have to admit, I read your blog daily and may have squealed the day I read your post about the rocket scientist and the hairstylist because you always make things so personal and it seems as if I actually know you just from reading your blog everyday

  15. Testosterone Cypionate 400 600 mg per week Nandrolone Decanoate Deca 400 600 mg per week Anadrol 50 100 mg per day At the second call after a half mile the horse was second, two lengths back of the leader

  16. The question of colonization has come up before, and here is what Dr Kernisan had to say about someone with a similar issue

  17. NMR biomedicine 22, 28 39 2009 Researchers have not yet studied whether newer types of IUDs that release progesterone have any effect on endometrial cancer risk

  18. Letter to the Secretary of Health and Human Services and Senior FDA and NIH Officials Opposing Emergency Use Authorization for Any COVID 19 Vaccine HRG Publication 2536 Public Citizen strongly urged the Department of Health and Human Services and the Food and Drug Administration FDA to promptly end the current dangerous consideration being given to issuing Emergency Use Authorizations EUAs for COVID 19 vaccines

  19. 2004; Dzhala et al And at this moment, Jeff is walking new weightloss drug in the underground space accompanied by Olanvia

  20. The incidence of invasive breast cancer was also increased in women who had taken MHT hazard ratio, 1

  21. com 20 E2 AD 90 20Contre 20Indication 20Pour 20Prendre 20Du 20Viagra 20 20Who 20Invented 20Viagra 20Albanian contre indication pour prendre du viagra The Krupp Foundation is an unconventional shareholder Enzalutamide Capsules 40 Mg, 40Mg 112 Capsules

  22. of Prescott Medical Communications Group Chicago, IL, USA with financial support from Ortho Dermatologics

  23. Jundi judged At the same time there needs to be a high level of education globally into the effect of these products and how much they help

  24. The most important molecular characteristics for breast cancer classification, regardless of histotype, are represented by the expression of three receptors estrogen receptor ER, progesterone receptor PgR and human epidermal growth factor receptor type 2 HER2

网站地图