Search code examples
c#websocketqtcpserver

Cannot establish proper server-client connection using websocket protocol


Goal:

Establish working server-client connection based on websocket protocol between QTcpServer and C# client. For now, via localhost.

Problem:

Server works with telnet client but not with any other client (e.g. C# client, websocket test client chrome plugin). C# client works with C# server or other websocket server (e.g. wss://socketsbay.com/wss/v2/2/demo/). Working in this case means: A connection is established, meaning server and client recognize that by firing some corresponding OnOpen event. Data can be sent both ways.

Monitoring the network activity using WireShark, the following can be observed:

enter image description here

Everything beneath the red line happens, when e.g. the QTcpServer successfully connects with the telnet client. Looks like the expected http handshake. After that, data can be exchanged. On the other hand, the lines above the red line appear if e.g. I try to connect the QTcpServer with a client other than telnet. After the handshake, it looks like the client sends the header of the handshake again? (Direction verified via wireshark: Client->Server). Anyways, after that the client websocket state is stuck on Connecting. The server thinks the connection is established though but data exchange is not possible.

What could be the problem here? I cannot figure out whether it's a server- or client-side problem, since there are scenarios, as mentioned, where both sides work.


Solution

  • Turns out the problem was that the server was expecting a regular TCP connection while the clients were all using Websocket protocol on top. Therefore it was not possible to create a working connection, although it seemed like client and server connected.

    The following post goes more into detail about the differences between Websockets and regular TcpSockets: Diff websockets and sockets