The client websocket cannot connect to PHPsocket.

Source: Internet
Author: User
The client websocket cannot be connected to PHPsocket. This post was last written by d6965921d at 2014-07-1610: 25: 53 Edit my client is written in c ++ websocket client. the requested address is ws: 127.0.0.1: 100322 & nbsp; (I request ws: echo. websocket. o client websocket cannot connect to PHP socket

This post was last edited by d6965921d at 10:25:53

My client uses c ++ to write websocket

On the client side, the requested address is ws: // 127.0.0.1: 100322 (when I request ws: // echo.websocket.org, this can indicate that the client code is correct and is on the server side)
Output
[1405476678: 7805] NOTICE: Initial logging level 7
[1405476678: 7806] NOTICE: Library version: 1.3 1544a2a
[1405476678: 7806] NOTICE: Started with daemon pid 0
[1405476678: 7806] NOTICE: static allocation: 4436 + (12x256 fds) = 7508 bytes
[1405476678: 8329] WARN: problems parsing header


Socket written by PHP




Error_reporting (E_ALL );
Set_time_limit (0 );
// Ob_implicit_flush ();

$ Address = '1970. 0.0.1 ';
$ Port = 100322;
// Create a port
If ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) = false ){
Echo "socket_create () failed: reason:". socket_strerror (socket_last_error (). "\ n ";
}

// Bind
If (socket_bind ($ sock, $ address, $ port) === false ){
Echo "socket_bind () failed: reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
}

// Listen
If (socket_listen ($ sock, 5) === false ){
Echo "socket_bind () failed: reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
}

Do {
// Get a link
If ($ msgsock = socket_accept ($ sock) === false ){
Echo "socket_accepty () failed: reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
Break;
}
// Send welcome to the client
$ Msg = "server send: welcome
";
Socket_write ($ msgsock, $ msg, strlen ($ msg ));
Echo 'read client message \ n ';
$ Buf = socket_read ($ msgsock, 8192 );
$ Talkback = "received message: $ buf \ n ";
Echo $ talkback;
If (false === socket_write ($ msgsock, $ talkback, strlen ($ talkback ))){
Echo "socket_write () failed reason:". socket_strerror (socket_last_error ($ sock). "\ n ";
} Else {
Echo 'send success ';
}
Socket_close ($ msgsock );
} While (true );
// Close the socket
Socket_close ($ sock );


?>

The following is the output from the client to the server's PHP socket.
192: socket apple $ php index. php
Read client message \ nreceived message: GET/HTTP/1.1
Pragma: no-cache
Cache-Control: no-cache
Host: 127.0.0.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: nfvps1_kazkyrfzqkptpua =
Origin: 127.0.0.1
Sec-WebSocket-Protocol: default-protocol
Sec-WebSocket-Extensions: deflate-frame
Sec-WebSocket-Version: 13


Send success
------ Solution --------------------
Your php socket server has not completed the websocket handshake. we suggest you read the websocket protocol documentation or refer to how others wrote it. for example, the following code contains the websocket handshake and sends websocket messages.
Https://github.com/walkor/workerman-chat/blob/master/applications/Chat/Event.php

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.