- Notifications
You must be signed in to change notification settings - Fork 255
Description
Hi @bakpakin and all,
I found that it seems impossible to implement a TFTP client using the basic Janet net APIs.
The net/connect function binds a port to the socket upon connection, which makes it difficult to implement the TFTP protocol's behavior where the server's response needs to be received on a different, temporary port.
Therefore, to implement a protocol that requires flexible port allocation, like TFTP, a lower-level API such as net/socket that creates an unbound socket seems necessary.
I have attached simplified C examples of a TFTP server/client.
The core of these examples is as follows:
Server:
// Create socket and bind to port 8080 listen_sock = socket(AF_INET, SOCK_DGRAM, 0); bind(listen_sock, ...); // Receive message from client recvfrom(listen_sock, buffer, BUF_SIZE, 0, ...); // Create a new socket without bind() to send the response new_sock = socket(AF_INET, SOCK_DGRAM, 0); sendto(new_sock, "world", ..., client_addr, ...); Client:
// Create a socket (without binding) client_sock = socket(AF_INET, SOCK_DGRAM, 0); // Send initial request to server on port 8080 (an ephemeral port is automatically assigned) sendto(client_sock, "hello", ..., server_addr, ...); // Receive the server's response from a new port recvfrom(client_sock, buffer, BUF_SIZE, 0, ..., &from_addr, ...); // from_addr.sin_port will contain the ephemeral port number The log when the example runs successfully is shown below.
Server:
Server listening on 127.0.0.1:8080 Server received: hello from port 54379 Server sent: world from a new socket Client:
Client socket created (unbound) Client sent: hello Client received: world from port 60914 As I understand it, this action seems difficult to implement with net/connect.
I would appreciate for any feedback.
Thank you.