Possible to make a TCP socket not bound to port?

I’ve found a C++ script that receives TCP packets on a certain port, utilizing Unreal’s FTcpSocketBuilder functionality.

It needs to know the local port to be specified, since it requires a local endpoint to be bound to (or at least it seems so, since I couldn’t get it to work otherwise, might just be that I’m a noob). (see code)

//makes an endpoint defined by the machine's ip and a specific port
FIPv4Endpoint Endpoint(clientIP, clientPort);

//makes a listensocket bound to that endpoint
FSocket* ListenSocket = FTcpSocketBuilder(*YourChosenSocketName)
	.AsReusable()
	.BoundToEndpoint(Endpoint)
	.Listening(8);

When the software that I’m streaming the TCP packets from allows me to specify the client’s port, it’s all ok.

However, the software I am using can’t do that. It uses the apparently common approach of asking the client for a port first (which the client designates from a pool of so-called ephemeral ports), and streaming to that port. That means that the client port is different every time a new stream starts, and the thing that’s fixed is the server’s port.

My question is - is there a way for Unreal to identify which port the stream is taking place at (since I know the source ip, source port and destination ip)? In other words, does the TCP socket have to be bound to a pre-defined port?

An alternative solution (which would probably be considered off-topic in an Unreal answer hub) would involve some way of specifying a fixed port for the packets to be sent to on the server side. I assume it can be done by

  1. tricking the streamer software into
    believing the client returned a
    certain port as an available
    ephemeral one
  2. re-routing all packets it sends to
    any port of a certain ip to a
    specified port (this, however,
    involves a risk or accidentally
    including some packet headed to that
    ip that wasn’t part of the stream)
  3. any other viable option that I might
    not think of since I am very
    inexperienced with all of this

Cheers :slight_smile:


EDIT:


I think I should put this more simply. I am using 's script and have no idea why it’s so long and convoluted. I read through it and grasped the general concepts as well as the flow of logic, but I don’t have any idea how some of those functions work, what to modify there and why specifically. I should’ve started with saying I have very little experience with C++ and TCP in general.

So, I’ve implemented 's script, so everything works when I sent packets from this software (that I use for testing only), and as you can see I can specify receiving port as proven by a wireshark capture.

However it doesn’t work when I send them from this other software (which I intend to use in the final setup), which specifies the sending port, as you can see from the second wireshark capture, since the receiving port for that stream is different every time (but the sending one is always the same).

How do I make it possible for 's script to read packets the other software is sending?

Socket always need port to be defied, there no way around it specially on server which needs static port for client to know where to connect (if you don’t define port in client or don’t let even specify, client use default one, for example HTTP use port 80, if you don’t define port in browser it connects to 80). In case of client socket, port number usually does not matter, so software pick next free one from some specified pool of ports, UE4 seems to have a function for that:

Server knows which client port sending packets, since packets have source port information in them and API usally let you get that information. In case of TCP you just esablish connection from one ip/port to another once and you just reuse that connection, TCP APIs usally work around connection session, insted of signing destination on each of packets (but looking on UE4 APIs that might not be the case :p). If FSocket you got RecvFrom:

Whcih give you address fronm where packet got recived

You seem to making server (listen socket), i’m not sure what software you deal with, but if client can’t specify port, it most likely means it’s using default port and you need to figure out which one is it (if documentation does not tell use “netstat -o” and “tasklist” to figure pid of client in windows console). If it really asks server for port, it means it’s own protocol, because TCP or UDP does not support something like that, it needs specified port so that things also requires port to ask. If you want feather help you need to tell me what are you trying to connect to.

The BoundToAddress, BoundToEndpoint and BoundToPort methods on FTcpSocketBuilder are optional. You would normally only use these for server (aka. listen) sockets. For client sockets, you can then later use FSocket::Connect to connect to a remote server with a specific IP/port.

If you create a server socket, then yes, it needs to be bound to a local IP/port, otherwise it wouldn’t know what to listen to. However, there you can also postpone the binding by calling FSocket::Bind at a later time.

Here’s an example:

FSocket* ListenSocket = FTcpSocketBuilder(*YourChosenSocketName)
     .AsReusable();


ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);

ListenSocket->Bind(*SocketSubsystem->CreateInternetAddr(Endpoint.GetAddress().GetValue(), Endpoint.GetPort()));

ListenSocket->Listen(8);

Take a look at the FTcpSocketBuilder implementation in TcpSocketBuilder.h. It’s really just a convenience wrapper around the rather atrocious Socket API.

thanks for the feedback, although i don’t fully follow

it’s definetely not using a standard port (but a different one every time, like you said, from a pool of ports), and it definetely uses TCP. I don’t know what you are reffering to as server and client, maybe it’s different in the context of “receiver / sender” and in the TCP packet context? I assumed the UE4 side is the client, and the software that is streaming - the server. I might be wrong.

please see edit of original question for more info and screenshots

thank you for the answer, but I am still confused

I don’t understand which is the client and which is server in my case, since you seem to be reffering to an UE4 implementaion of a server. My UE4 program only needs to read the packets that are already being sent by a different software (which makes me assume it’s the client but I might be wrong), and in that different software I can only specify the port from which the streaming happens, so the port on the other machine not the UE4 one.

's script (that I provided a link to in the question) deals with exactly that - getting packets. However, if that is possible to be done with Socket->Connect, does it mean I will have to replace a part of that script? (since implementing it without removing the part that’s already doing that is making duplicate functionality)

please see edit of original question, now with screenshots

Well you making listening port, so in network state point you make a server

What is that “other software”?

I don’t mean client and server in the UE4 game sense, but in a socket networking sense. TCP is a connection oriented protocol, and it means that someone has to initiate the handshake for establishing a connection. The server socket is the socket that is accepting incoming connections, the client socket is the socket that is initiating the connection. Once the connection is established, both sockets can send and receive data between each other at any time.

It sounds to me like you are trying to receive data from a streaming server. In that case, the streaming software represents the server. Your game, which acts as the client, will need to establish the connection to the streaming server. It does this by calling FSocket::Connect with the IP and port number of your streaming server software, i.e. 10.3.0.65:33433.

When you create a client socket in your game, the local port number that it is bound to is generally irrelevant. Unless you specify one yourself, the operating system will pick an available port for you. What really matters is that your client socket is connecting to a port number on the server side that the server is really listening on. Otherwise, a connection could never be established. The client, by initiating the connection request to the streaming server, will automatically tell the server its own local port number, whatever that is. When the client establishes connection to the server, the server knows from then on which network port in your game to send data back to. This is part of the TCP protocol.

You can find more information about the TCP connection establishment on Wikipedia’s TCP page. Make sure you understand how this works.

it’s a real-time face tracker called FaceShift, unfortunately recently discontinued and allegedly bought by Apple