UDP communication via CTRL

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
Search

Post Reply
4 posts • Page 1 of 1
Smiffy
Posts: 33
Joined: Thu May 18, 2017 2:21 pm

UDP communication via CTRL

Post by Smiffy » Fri Aug 09, 2019 8:27 am

I am wanting to write my own TFTP server in CTRL script. This should be entirely possible the TFTP protocol is quite simple. I want to do it in CTRL as that way I do not have to worry about future maintenance (windows updates) as I would if I wrote a windows api, and also the code can be maintained by staff without specialist C# knowledge.
1) I have successfully opened a UDP connection. However in doing so I have hard code the IP of the local host machine into the UDP open command. Ideally I would like to call a function to get the localhost machine reference for the UDP open command. I cannot just use "localhost" as the machine has several interfaces, not just the one.
2) The data I intend to read / write is a mixture of word and string data. When receiving into a string using udpRead() I get an error as some of the UDP packet data is not string data. So I tried to use a blob, but that doesn't work either as nothing is returned when I debug print the blob variable after the read. So how do I receive a mix data type UDP packet? It seems very inflexible if the udpRead can only read string data.

gschijndel
Posts: 71
Joined: Tue Jan 15, 2019 2:12 pm

Re: UDP communication via CTRL

Post by gschijndel » Wed Aug 14, 2019 1:14 pm

1) By using an empty string as host parameter it should listen to all interfaces.
2) I have read binary data with 'udpRead', so I am sure this is possible. Perhaps you could post your read part to identify the error.

Smiffy
Posts: 33
Joined: Thu May 18, 2017 2:21 pm

Re: UDP communication via CTRL

Post by Smiffy » Wed Aug 14, 2019 3:53 pm

Here is my code.
Rxbuf is the receive variable. It is a string, but I have tried a blob and that doesn't work either.

Code: Select all

bool OpenPort()
{
    dyn_errClass Err;
    unsigned port = 69;

    TFTPPort = udpOpen("172.16.7.133", port);
    DebugN(TFTPPort);

    if (TFTPPort < 0)
    {
        Err = getLastError();
        DebugN(Err);
        ClosePort();
        return FALSE;
    }
    return TRUE;
}

void ClosePort()
{
   udpClose(TFTPPort);
}

main()
{
    int State = INIT;
    string RxBuf;
    dyn_string WriteHost;
    unsigned WritePort;
    dyn_errClass Err;

    for(;;)
    {
        switch(State)
        {
            case INIT:
            ClosePort();
            if (OpenPort())
                State = GETDATAPKT;
            break;

            case GETDATAPKT:
            if (udpRead(TFTPPort, RxBuf, WriteHost, WritePort, TIMEOUT) > 0)
            {
                DebugN(WriteHost,WritePort,RXBuf);
            }
            else
            {
                Err = getLastError();
                DebugN(Err);
                if(dynlen(Err) > 0)
                {
                    DebugN(Err);
                }
            }
            break;
        }
        delay(0,1000);
    }
}
Last edited by gschijndel on Wed Aug 14, 2019 5:45 pm, edited 1 time in total.
Reason: Added code tags

gschijndel
Posts: 71
Joined: Tue Jan 15, 2019 2:12 pm

Re: UDP communication via CTRL

Post by gschijndel » Fri Aug 16, 2019 10:14 am

Except that 'WriteHost' should be a string instead of dyn_string I do not see something wrong.

I have attached an example, which should help you get started with a TFTP server.
Attachments
tftpServer.xml
(1.95 KiB) Downloaded 11 times

Post Reply
4 posts • Page 1 of 1