简体   繁体   中英

Connect JavaScript WebSocket to C winsock

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.

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!"); } } 

Server: 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);
}

A webSocket connection uses a whole connection scheme and it's own data format (eg 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.

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.

You can see an overview of what it takes for Writing webSocket servers in that article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM