PHP under Windows sockets operation had better use the command line to run the program, as for why, I can feel,
First run PHP + application path, the message is said under your PHP did not join the path environment variable,
Then find your PHP installation directory, I was the
Then duplicate the above path, right click on my computer - property - senior - environment variables, added to the environment variables, pay attention to a; Number, see below
Then, save, and once again run CMD, OK to succeed, there will not be the prompt,
Next is the theme, first put the code
Copy the code
<? PHP
//make sure the connection when the client will not timeout
Set_time_limit (0);
$IP='127.0.0.1;
$port=1935;
/*
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The whole process * @ socket communication
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* @ socket_create
* @ socket_bind
* @ socket_listen
* @ socket_accept
* @ socket_read
* @ socket_write
* @ socket_close
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
*/
/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the following operations are manual -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
If (($sock=socket_create (AF_INET SOCK_STREAM, SOL_TCP)) & lt; 0 {
Echo "socket_create () the reason for failure is:" the socket_strerror ($sock). "\ n";
}
If ($ret (=socket_bind ($sock, IP, $$port)) & lt; 0 {
Echo "socket_bind () the reason for failure is:" the socket_strerror ($ret). "\ n";
}
If ($ret (=socket_listen ($sock, 4)) & lt; 0 {
Echo "socket_listen () the reason for failure is:" the socket_strerror ($ret). "\ n";
}
$count=0;
Do {
If (($msgsock=socket_accept ($sock)) & lt; 0 {
Echo "socket_accept () failed:" reason: "the socket_strerror ($msgsock)." \ n ";
break;
} else {
//sent to the client
$MSG="test success! \n";
Socket_write ($msgsock, $MSG, strlen ($MSG));
Echo "test success! \ n";
$buf=socket_read ($msgsock, 8192);
The information received $talkback=": $buf \ n";
Echo $talkback;
If (+ + $count & gt; {
=5)break;
};
}
//echo $buf;
Socket_close ($msgsock);
} the while (true);
Socket_close ($sock);
?>
Copy the code
This is a socket server code, then run the CMD, pay attention to is our program storage path,
Have not reflect, have now has already begun to run server-side program, port has started listening and run netstat ano can view port, mine is port 1935
, the port is already in the LISTENING state, then we just run the client application can be connected, the code
Copy the code
<? PHP
Error_reporting (E_ALL);
Set_time_limit (0);
Echo "& lt; The h2 & gt; TCP/IP Connection
$port=1935;
$IP="127.0.0.1";
/*
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The whole process * @ socket connection
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* @ socket_create
* @ socket_connect
* @ socket_write
* @ socket_read
* @ socket_close
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
*/
$sockets=socket_create (AF_INET SOCK_STREAM, SOL_TCP);
If ($socket & lt; 0 {
Echo "socket_create () failed:" reason: "the socket_strerror ($socket)." \ n ";
} else {
Echo "OK. \ n";
}
Echo "tries to connect '$IP ports' $port... \n";
$result=socket_connect ($sockets, $IP, $port);
If ($result & lt; 0 {
Echo "socket_connect () failed. \ nReason: ($result)". The socket_strerror ($result). "\ n";
} else {
Echo "connection OK \ n";
}
$in="Ho \ r \ n";
$in.="first" blood \ r \ n ";
$out=' ';
if(! Socket_write ($sockets, $in, strlen ($) in)) {
Echo "socket_write () failed:" reason: "the socket_strerror ($socket)." \ n ";
} else {
Echo "information sent to the server success! \n";
Echo "send content is: & lt; Font color='red' & gt; $in
";
}
While ($out=socket_read ($sockets, 8192)) {
Echo "receiving server returned information successfully! \n";
Echo "accept as follows: the content of the", $out;
}
Echo "close the SOCKET... \n";
Socket_close ($socket);
Echo "closed OK \ n";
?>
Copy the code
At this point the client has connected to the server, it is just a small example, a lot of things know principle of other it is good to do,
CodePudding user response:
The android version of the mobile phone can hold?CodePudding user response: