Is it possible to bind client-side WebSockets to a specific local port?

97 Views Asked by At

I am trying to open a WebSocket connection between a browser-based client and C++ server, but without requiring any additional network configuration on the server machine besides allowing the connection through the firewall. I plan to accomplish this by using TCP hole-punching (which I understand makes this more of a "peer-to-peer" connection, but the WebSocket protocol only understands "server-client" architecture). I have a rendezvous server set up (as described in this paper) that accepts WebSocket connections over TCP.

The problem is that in order to successfully form a connection between the browser client and C++ server, both need to open multiple sockets using the same local port (to allow each other's packets through NAT). This is no problem in the C++ code, but I have not yet found a way to bind a WebSocket to a specific local port with client side Javascript. Another consideration is whether or not it is possible to bind multiple client-side WebSockets to the same port, but that is not the focus of this question.

tl;dr code snippet:

let socket = new WebSocket("ws://192.0.2.69:12345");
// Can we specify the port that we want to connect FROM? (client-side socket binding)

// Normally just randomly selected by OS - NOT what I want
// 12345 is the port we want to connect to on the server - unrelated
0

There are 0 best solutions below