Phptcpsocket learning Summary

Source: Internet
Author: User
PHP is best to run the program through the command line when operating the socket in WIN. You can understand why. First, run the php + program path. This prompt indicates that your php is not added to the environment variable path. Find your php installation directory, copy the above path, right-click my computer-properties-advanced-Environment Change

PHP is best to run the program through the command line when operating the socket in WIN. You can understand why. First, run the php + program path. This prompt indicates that your php is not added to the environment variable path. Find your php installation directory, copy the above path, right-click my computer-properties-advanced-Environment Change

PHP is best to run the program through the command line when operating the socket in WIN. You can understand why.

First, run the php + program path. This prompt indicates that your php is not added to the environment variable path.

Then find your php installation directory, which is mine

Copy the above path, right-click my computer-properties-advanced-environment variable, and add it to the environment variable. Note that there is a number.

Click OK and save. Run cmd again. OK is successful. The prompt will not appear.

Next is the topic. Put the code first.

 // Ensure that no timeout occurs when you connect to the client
Set_time_limit (0 );

$ Ip = '1970. 0.0.1 ';
$ Port = 1935;

/*
+ -------------------------------
* @ Socket: entire communication process
+ -------------------------------
* @ Socket_create
* @ Socket_bind
* @ Socket_listen
* @ Socket_accept
* @ Socket_read
* @ Socket_write
* @ Socket_close
+ --------------------------------
*/

/* ---------------- The following operations are all in the manual -------------------*/
If ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) <0 ){
Echo "socket_create () failed because:". socket_strerror ($ sock). "\ n ";
}

If ($ ret = socket_bind ($ sock, $ ip, $ port) <0 ){
Echo "socket_bind () failed because:". socket_strerror ($ ret). "\ n ";
}

If ($ ret = socket_listen ($ sock, 4) <0 ){
Echo "socket_listen () failed because:". socket_strerror ($ ret). "\ n ";
}

$ Count = 0;

Do {
If ($ msgsock = socket_accept ($ sock) <0 ){
Echo "socket_accept () failed: reason:". socket_strerror ($ msgsock). "\ n ";
Break;
} Else {

// Send to client
$ Msg = "test successful! \ N ";
Socket_write ($ msgsock, $ msg, strlen ($ msg ));

Echo "test succeeded \ n ";
$ Buf = socket_read ($ msgsock, 8192 );


$ Talkback = "received message: $ buf \ n ";
Echo $ talkback;

If (++ $ count> = 5 ){
Break;
};


}
// Echo $ buf;
Socket_close ($ msgsock );

} While (true );

Socket_close ($ sock );
?>

This is the socket server code. Then run cmd. Note the path where your program is stored.

Not reflected. The current server-side program has started to run and the port has started to listen. Run netstat-ano to check the port status. My port number is port 1935.

Check that the port is already in the LISTENING status. Next, we only need to run the client program to connect. Code on

 Error_reporting (E_ALL );
Set_time_limit (0 );
Echo "TCP/IP Connection \ n ";

$ Port = 1935;
$ Ip = "127.0.0.1 ";

/*
+ -------------------------------
* @ Socket connection process
+ -------------------------------
* @ Socket_create
* @ Socket_connect
* @ Socket_write
* @ Socket_read
* @ Socket_close
+ --------------------------------
*/

$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
If ($ socket <0 ){
Echo "socket_create () failed: reason:". socket_strerror ($ socket). "\ n ";
} Else {
Echo "OK. \ n ";
}

Echo "trying to connect '$ ip' port' $ port'... \ n ";
$ Result = socket_connect ($ socket, $ ip, $ port );
If ($ result <0 ){
Echo "socket_connect () failed. \ nReason: ($ result)". socket_strerror ($ result). "\ n ";
} Else {
Echo "Connect OK \ n ";
}

$ In = "Ho \ r \ n ";
$ In. = "first blood \ r \ n ";
$ Out = '';

If (! Socket_write ($ socket, $ in, strlen ($ in ))){
Echo "socket_write () failed: reason:". socket_strerror ($ socket). "\ n ";
} Else {
Echo "sent to server information! \ N ";
Echo "sends the following content: $ in
";
}

While ($ out = socket_read ($ socket, 8192 )){
Echo "received server return message successful! \ N ";
Echo "received content:", $ out;
}


Echo "Disable SOCKET... \ n ";
Socket_close ($ socket );
Echo "Disable OK \ n ";
?>


Now the client is connected to the server. This is just a small example. A lot of things can be done if they know the principle.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.