Phpsocket problems! Socket php udp socket_write ()
A recent project requires php socket communication. now I have tested that the client can be successfully sent to the server under TCP, and the server can also return the client information.
However, in UDP, the program will have a problem. you can receive the information on the client, but if socket_write () is returned, the returned address cannot be found... Then the client cannot accept the information returned by the server!
In this case, I want to help solve this problem.
PHP Warning: socket_write (): unable to write to socket [0]: The socket is not connected.
And (when a sendto call is used to send a datagram socket) no address is provided to send or receive data requests.
Not accepted.
Reply to discussion (solution)
Have you done anything about this?
No. the error message above is clear, that is, there is no connection or the address is incorrect.
Let's look at the code. it's useless to guess.
Here is the server
Set_time_limit (0 );
Ob_implicit_flush ();
Echo "create SOCKT... \ n ";
$ Socket = socket_create (AF_INET, SOCK_DGRAM, SOL_UDP );
If ($ socket = false ){
Echo "socket_create () failed: reason:". socket_strerror (socket_last_error ($ socket). "\ n ";
}
Echo "bind SOCKT... \ n ";
$ OK = socket_bind ($ socket, '192. 168.1.59', 192 );
If ($ OK = false ){
Echo "socket_bind () failed: reason:". socket_strerror (socket_last_error ($ socket ));
}
Else
{
Echo "SOCKT bound successfully... \ n ";
}
$ Count = 0;
Socket_set_block ($ socket );
Do {
$ Msg = "The server is sent to the client !!! \ N ";
Socket_sendto ($ socket, $ msg, strlen ($ msg), 0, '192. 168.1.59 ', 192 );
Echo "test succeeded \ n ";
$ Form = "";
// $ Port = 0;
Socket_recvfrom ($ socket, $ buf, 1024,0, $ form, $ port );
Echo "test succeeded \ n ";
$ Talkback = "received message: $ buf \ n ";
Echo $ buf1;
Echo $ talkback;
} While (true );
Socket_close ($ socket );
?>
Here is the client
$ Server_ip = "192.168.1.59 ";
$ Port = 8686;
Echo "create SOCKT... \ n ";
$ Socket = socket_create (AF_INET, SOCK_DGRAM, SOL_UDP) or die ("sockt create failed ");
Socket_bind ($ socket, '192. 168.1.59 ', 192 );
If (! Socket_connect ($ socket, '192. 168.1.59 ', $ port )){
Print ("Couldn't connect:". socket_strerror (socket_last_error (). "\ n ");
}
Socket_sendto ($ socket, $ str, $ arrayLen, 0, '192. 168.1.59 ', 192 );
Echo "sent to server information! \ N ";
Echo "sent content: $ str
";
$ Form = "";
$ Port1 = 0;
Socket_recvfrom ($ socket, $ buf, 1024,0, $ form, $ port1 );
Echo $ form;
Echo $ port1;
Echo "message returned successfully! ". $ Buf;
Echo"
";
Echo "disable SOCKET... \ n ";
Socket_close ($ socket );
Echo "disable OK \ n ";
?>
Let's take a look.
// $ Port = 0; the comment here should also be removed .. I have been doing various tests. No.
Let's take a look.
That is, the server can accept the message, the client cannot receive the message returned by the server, or the message cannot be returned.
Why is there no listen and accept on the server?
Connect is not ...... Dizzy
Because UDP is not TCP...
Connect is added on the client...
In addition, I use socket_recvfrom and socket_sendto followed by an address. Therefore, you do not need to add connect here. it is used when I test socket_write and socket_read on the client connect.
Er, UDP...
Let me see it again.
The client should recv where it is connected, and there is no reason to separate the ports
Does the client have to bind it?
Well, there is no need to bind it .. Isn't there any way for me to do this, and then perform various tests. The client BIND was not bound before, and it does not work here.
I deleted the BIND, and it also showed this effect!
Do not use udp. Tcp is a standard recommended method. Udp is not supported in many software applications.
Curl may support udp
Here is a simple UDP example.
Http://www.zrwm.com /? P = 1281
I don't want to use UDP either. the TCP test is successful, but the project must use UDP!
Thank you very much for the code on the link snmr_com you sent to me. after modification, it succeeded!