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.
UDP communication via CTRL
- gschijndel
- Posts:376
- Joined: Tue Jan 15, 2019 3:12 pm
Re: UDP communication via CTRL
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.
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:45
- Joined: Thu May 18, 2017 4:21 pm
Re: UDP communication via CTRL
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.
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 7:45 pm, edited 1 time in total.
Reason: Added code tags
Reason: Added code tags
- gschijndel
- Posts:376
- Joined: Tue Jan 15, 2019 3:12 pm
Re: UDP communication via CTRL
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.
I have attached an example, which should help you get started with a TFTP server.
- Attachments
-
- tftpServer.xml
- (1.95 KiB) Downloaded 283 times