PHP + SOCKET because the SOCKET is not connected and the address is not provided, the request for sending or receiving data is not accepted

Source: Internet
Author: User
PHP + SOCKET because the SOCKET is not connected and the address is not provided, the request for sending or receiving data is not accepted. This post was last edited by subendong in December 2011-06-0316: 39: 41. as two posts were published consecutively, none of them returned, please seek help first: PHP + SOCKET because the SOCKET is not connected and the address is not provided, the request for sending or receiving data is not accepted.
This post was last edited by subendong at 16:39:41 on. since two posts were published consecutively, no one returned, I first applied to the moderator to delete the previous two similar posts, otherwise it would be useless. Just like garbage.
I hope you can make some modifications based on the code provided by me so that the code can run correctly. thank you!

The details are as follows:
Warning: socket_read (): unable to read from socket [0]: The address is not provided because the socket is not connected and (when a sendto call sends a datagram socket, the request for sending or receiving data is not accepted.
I created two files xx. php that are executed on the server. X. php is a file executed on the client.
Xx. php
 // Set some basic variables
Global $ lat, $ lnt;
$ Host = "192.168.1.110 ";
$ Port = 8080;
// 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 );
If (! $ Spawn) break;
$ Output = "congratulations, you have connected server:". $ host ."! ";
Socket_write ($ spawn, $ output, strlen ($ output) or die ("error \ n ");
$ Input = socket_read ($ socket, 100000 );
Socket_write ($ spawn, $ input, strlen ($ input) or die ("error \ n ");
Socket_close ($ spawn );
}
}
Socket_close ();
?>




X. php
 // Set some basic variables
$ Host = "192.168.1.110 ";
$ Port = 8080;
// 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 );
If ($ result)
{
$ Output = "abc ";
Socket_write ($ socket, $ output, strlen ($ output ));
$ Input = socket_read ($ socket, 1024 );
}
Echo $ input;
?>



Open xx. php on the server, and then open x. php on the client. the server prompts the above error message, which is the socket_read error.
Which of the following experts helped me find a lot of information that I had not solved.
------ Solution --------------------
I will give you an available test example.
// Client
// Set error handling
Error_reporting (E_ALL );
// Set the processing time
Set_time_limit (0 );

$ Ip = ""; // IP address
$ Port = 1000; // port number

$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // Create a SOCKET
If ($ socket)
Echo "socket_create () successful!
\ N ";
Else
Echo "socket_create () failed:". socket_strerror ($ socket )."
\ N ";

$ Conn = socket_connect ($ socket, $ ip, $ port); // establish a SOCKET connection

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.