'Create TcpListener from HttpListener

I have created my own websocket class that extends the abstract System.Net.WebSockets.WebSocket Class. my class uses TcpListener and TcpClient to communicate. After this server receives an HTTP-Get-formatted request that asks to upgrade to a websocket connection, I am able to complete the handshake successfully and communicate with a websocket client.

Separately I have a simple HTTP server that sends and receives HTTP requests using HttpListener and HTTPClient.

Now I want to combine them.

I would like this HTTP server to, upon receiving a websocket request, transfer the "connection" to my websocket server to handle. However I am struggling to conceptually understand what a TCP "connection" is.

I know that I can create a TCPClient using an existing socket, but I am unsure how to retrieve the existing HTTPListener's socket (maybe it can't be exposed?). And for that matter I am unsure what would happen if I tried to have a TCPClient and HTTPListener sharing the same socket.

So how do I construct a TCPListener from an existing HTTPListener?



Solution 1:[1]

However I am struggling to conceptually understand what a TCP "connection" is.

RFC 793, Transmission Control Protocol (and the subsequent RFCs that update it) is the standard for TCP. It explains what a TCP connection is, and later goes into more detail:

Multiplexing:

To allow for many processes within a single Host to use TCP communication facilities simultaneously, the TCP provides a set of addresses or ports within each host. Concatenated with the network and host addresses from the internet communication layer, this forms a socket. A pair of sockets uniquely identifies each connection. That is, a socket may be simultaneously used in multiple connections.

The binding of ports to processes is handled independently by each Host. However, it proves useful to attach frequently used processes (e.g., a "logger" or timesharing service) to fixed sockets which are made known to the public. These services can then be accessed through the known addresses. Establishing and learning the port addresses of other processes may involve more dynamic mechanisms.

Connections:

The reliability and flow control mechanisms described above require that TCPs initialize and maintain certain status information for each data stream. The combination of this information, including sockets, sequence numbers, and window sizes, is called a connection. Each connection is uniquely specified by a pair of sockets identifying its two sides.

When two processes wish to communicate, their TCP's must first establish a connection (initialize the status information on each side). When their communication is complete, the connection is terminated or closed to free the resources for other uses.

Since connections must be established between unreliable hosts and over the unreliable internet communication system, a handshake mechanism with clock-based sequence numbers is used to avoid erroneous initialization of connections.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ron Maupin