Use php for socket programming in linux, and report a connection rejection exception. please take a look.

Source: Internet
Author: User
In linux, php is used for socket programming and a connection rejection error is reported. please refer to the following link for details: 57 editing I was just getting started with Linux and php and socket (haha ....), I found some information on the Internet and wrote a very basic client to use php for socket programming in ser linux. I reported a connection rejection error. please take a look.
At last, this post was edited by lovegis0101 at 08:40:57. I was just getting started with Linux and php and socket (haha ....), I found some information on the Internet and wrote two basic php scripts on the client and server. in Windows, the test can communicate with each other. Then, the client script is placed in Linux, but the following error is reported:
PHP Warning: socket_connect (): unable to connect [111]: Connection refused in/socketClient. php on line 12, 12 is to call the socket_connect method, the error here
The system environment is like this. I have installed a Linux virtual machine on a Windows host. the Windows system ip address is 192.168.0.2 and the Linux system ip address is 192.168.102. the Two System networks are connected through bridging, linux can connect to the network through Windows.
This problem has plagued me for several days. I have not found a solution for searching online. please help me to see it.
Attach the script code:
Server:

// Set some basic variables
Global $ lat, $ lnt;
$ Host = "127.0.0.1 ";
$ Port = 8888;
// Set the timeout value
Set_time_limit (0 );
// Create a Socket
$ CommonProtocol = getprotobyname ("tcp ");
$ Socket = socket_create (AF_INET, SOCK_STREAM, $ commonProtocol );

// Bind the Socket to the port
$ Result = socket_bind ($ socket, $ host, $ port) or die ("cocould not bind to socket \ n ");
$ Ret = socket_listen ($ socket, 5 );
If ($ ret ){
While (true ){
$ Spawn = socket_accept ($ socket) or die ("cocould not accept incoming connection \ n ");;
Echo ("socket connected \ n ");
If (! $ Spawn) break;
$ Output = "Welcome to the PHP Test Server... nice \ n ";
Socket_write ($ spawn, $ output, strlen ($ output) or die ("error \ n ");
While ($ input = socket_read ($ spawn, 64 )){
Echo ($ input );
}
Echo ("\ n ");
Socket_close ($ spawn );
}
}
?>

Client:

// Set some basic variables
$ Host = "192.168.0.2 ";
$ Port = 8888;
// Set the timeout value
Set_time_limit (0 );
// Create a Socket
$ CommonProtocol = getprotobyname ("tcp ");
$ Socket = socket_create (AF_INET, SOCK_STREAM, $ commonProtocol );
If (! $ Socket) echo "can't creat socket ";
// Bind the Socket to the port
$ Result = socket_connect ($ socket, $ host, $ port) or die (socket_strerror ());
If ($ result ){
$ Output = "hello server! "; // Lat: 30.582029016593196; lnt: 103.98662567138672.
Socket_write ($ socket, $ output, strlen ($ output ));
$ Input = socket_read ($ socket, 1024 );
Echo ($ input );
}
?>
Share: More

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.