将 JavaScript WebSocket 连接到 C winsock [英] Connect JavaScript WebSocket to C winsock
问题描述
我设法将我的 JavaScript 客户端连接到 C++ 服务器(我使用的是 winsock),并且服务器收到一个 HTTP 标头,但之后我无法发送或接收任何其他内容.服务器发送消息(send 返回发送的字节数),但没有任何内容到达客户端.
I managed to connect my JavaScript client to C++ server (I'm using winsock), and the server receives a HTTP header, but I can't send or receive anything else after that. The server sends the message (send returns the number of bytes sent), but nothing arrives on the client.
客户端:JavaScript
Client: JavaScript
function WebSocketTest()
{
if ("WebSocket" in window)
{
var ws = new WebSocket("ws://192.168.43.205:80");
ws.onopen = function()
{
ws.send("Message to send");//this doesn't work
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
alert("Message is received...");
document.getElementById("message").innerHTML += received_msg;
};
ws.onclose = function()
{
alert("Connection is closed...");
};
ws.onerror = function(error){
alert('Error detected: ' + error);
}
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
服务器:C/C++
#include <stdio.h>
#include "winsock2.h"
#include <iostream>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
#define _WINSOCK_DEPRECATED_NO_WARNINGS
using namespace std;
void main() {
int size;
char buffer[1024];
WSADATA wsaData;
sockaddr_in service;
SOCKET ListenSocket, AcceptSocket;
service.sin_family = AF_INET;
service.sin_addr.s_addr = INADDR_ANY;
service.sin_port = htons(10011);
WSAStartup(MAKEWORD(2, 2), &wsaData);
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bind(ListenSocket, (SOCKADDR*)&service, sizeof(service));
listen(ListenSocket, SOMAXCONN);
AcceptSocket = accept(ListenSocket, NULL, NULL);
cout << "Client connected.\n";
size = recv(AcceptSocket, buffer, 1024, 0);
string msg(buffer, size);
/*
msg contains this:
GET / HTTP/1.1
Host: 172.16.199.150:10011
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://localhost
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,hu;q=0.6
Sec-WebSocket-Key: 34qTtPYjnRJheHKQowePRg==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
*/
string abc = "abc";
auto cmsg = abc.c_str();
size = send(AcceptSocket, cmsg, abc.size(), 0);
// size == 3, which means send was successful, but client doesn't receive anything
// hangs here, even tho client sent a message
size = recv(AcceptSocket, buffer, 1024, 0);
}
推荐答案
一个 webSocket 连接使用一个完整的连接方案和它自己的数据格式(例如,它是它自己的协议).要接收 webSocket 连接,您必须支持整个 webSocket 协议,包括 HTTP 连接方案、安全协商和数据帧格式.
A webSocket connection uses a whole connection scheme and it's own data format (e.g. it is its own protocol). To receive webSocket connections , you have to support the whole webSocket protocol, including the HTTP connection scheme, the security negotiation and the data frame format.
由于许多语言都有许多预先编写的实现,因此大多数人会获取并使用现有的 webSocket 服务器实现,而不是从头开始重写它.
Since there are many pre-written implementations for lots of languages, most people will obtain and use an existing webSocket server implementation rather than rewrite it all from scratch.
您可以看到写作所需的概述在那篇文章中的 webSocket 服务器.
这篇关于将 JavaScript WebSocket 连接到 C winsock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!