heinbali01 wrote on Friday, August 31, 2018:
Hi Claudio,
if I understand, the socket process is tied to port number instead to
IP address, then I need to create a socket for each port I intend to use.
Indeed, you create a socket for each port number and not for each IP-address + port-number combination.
Just like you would in +TCP /single.
For example, in my case I have 2 port: the first at IP 192.168.0.0 and the second at 192.168.1.0.
Those numbers are IP-addresses, not port numbers.
The first port ( HT: IP-address ) is used for the connection of a telnet client (port 23)
and a HTTP client (port 80), while the second port ( IP-address ) for a FTP client (port 21).
You will need 3 sockets and bind them to:
0.0.0.0 prt 23
0.0.0.0 prt 80
0.0.0.0 prt 21
So I need to do the following:
- Create 2 network interface and 2 network node
In the software the nodes are called end-points
The application defines one or more interfaces, and each interface can have one or more end-points ( or IP-addresses ).
- Call
FreeRTOS_IPStart()
function
- Create a socket set (
FreeRTOS_CreateSocketSet
).
- Create my 3 socket (for telnet, HTTP, FTP) and initialize them ( bind(), setsockopt(), x_listen() )
- Add sockets with FreeRTOS_FD_SET().
- Create a process(loop) for socket acceptation for each of my sockets (
FreeRTOS_accept()
)
Looks all good
If a connection is requested to a port then FreeRTOS_accept()
function return
a socket structure pointer for this connection.
True. FreeRTOS_accept()
will return a new socket. Note that this socket will inherit all properties of the parent socket ( i.e. the listening socket ).
Once the connection is closed, the socket must be freed by calling FreeRTOS_closesocket()
.
It is important to set the buffer properties of the parent socket, because once a child-socket is receiving data, the receive buffer-size ( FREERTOS_SO_RCVBUF
) can not be changed any more.
Is my procedure correct?
Looks OK to me.
But if I connect the FTP client to port 0 (instead of port 1)
Where do you bind this socket? On a host?
Normally you bind a client socket to port 0, which means a random ( anonymous ) port number.
Binding to a low ( non-zero ) port number will most probably fail because they are reserved for the OS.
Normally:
● A server socket is bound to a well-known ( non-zero ) port number
● A client socket is bound to port number 0
But this rule is not obligatory.
One example: FTP in PASSIVE mode, opens a server socket for a data connection, which is bound to port 0 in order to get a random port number. The client will connect to this socket.
The server socket only expects a single client, so xBacklog
equals 1:
FreeRTOS_listen( xServerSocket, 1 );
and try to connect the client to 192.168.0.0:21, the FreeRTOS_accept()
function will accept the connection?
Yes indeed.
Note that you can determine the maximum number of connected clients to a socket. See the second parameter of FreeRTOS_listen()
: xBacklog
.
If yes, if I want to know the IP address of my connection in order to
distinguish the port (and eventually write an error message) can I use
the address structure filled by FreeRTOS_accept()
function?
Yes the parameter pxAddress
of FreeRTOS_accept()
will contain the address of the remote peer (in network-endian order).