Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
3
Erster Beitrag:
vor 2 Jahren
Letzter Beitrag:
vor 2 Jahren
Beteiligte Autoren:
Arie, Piet van Zanten

WD20 - write a telnet client

Startbeitrag von Arie am 27.05.2016 14:22

Hi,

one of mhy clients uses weight eqiuipment which sends data to a telnet client. There it's written to a ascii file for further use.

Can I write a simple Telnet client in Windev too? Is it just a matter of listening on tcpip port 23 and capture all incoming data?

Antworten:

Hi Arie,

Years ago I made a little program (WD15 ) that manages playlists on a Linux device using Telnet.
Perhaps this code is useful to you:


PROCEDURE Telnet_Open_List()
sFile,sReturn,sIP,sPlayListInternal is string
x,sCommandstring is string
nPort is int

sIP=INIRead("Net","IP","",sIniName)
nPort=INIRead("Net","TelnetPort","23",sIniName)
sPlayListInternal=INIRead("Net","Playlist_internal","usr/local/etc/dvdplayer/playlist",sIniName)

WHEN EXCEPTION IN
sFile=fTempFile("tmp",sCurrentRootDir)
DO
Error("Kan geen tijdelijk bestand aanmaken op "+sCurrentRootDir,"Controleer of de Mediaplayer is verbonden en ingeschakeld.")
RETURN
END
IF sFile="" THEN
Error("Kan geen tijdelijk bestand aanmaken op "+sCurrentRootDir)
RETURN
END

IF NOT SocketExist("TelNet") THEN
IF NOT SocketConnect("TelNet",nPort,sIP) THEN
Error("Fout bij verbinding met "+sIP+":"+nPort+CR+ErrorInfo())
HourGlass(False)
RETURN
END
END
SocketChangeTransmissionMode("TelNet",SocketNoEndTag)

sReturn=DataHandler() //Login
IF sReturn="TIMEOUT" THEN
Error("Timeout bij login")
RETURN
END
SocketWrite("TelNet","root"+ CR)
sReturn=DataHandler() //welcome and prompt

x=sLocalDir+fExtractPath(sFile,fFileName+fExtension)
x=Replace(x,"\","/")
sCommandstring="cp "+sPlayListInternal+" "+x
SocketWrite("TelNet",sCommandstring+CR)
sReturn=DataHandler() //Prompt
SocketWrite("TelNet","exit"+CR)
SocketClose("TelNet")

IF NOT ReadPlayList(sFile,Charact(0)) THEN
Error("Kan tijdelijk bestand niet lezen "+sFile)
RETURN
END

IF NOT fDelete(sFile) THEN
Error(StringBuild("Kan tijdelijk bestand %1 niet verwijderen. ",sFile))
END

PROCEDURE DataHandler(nTimeOut=10000) //10 seconden timeout
b,n,nState,nNow is int
x,sData,sResult,sDate is string
bConfirm is boolean

nNow=TimeToInteger(TimeSys())
sDate=DateSys()
LOOP
IF sDateDateSys() THEN
nNow-=8640000 //24*60*60*100
END
IF TimeToInteger(TimeSys())-nNow>nTimeOut THEN RESULT "TIMEOUT"

Multitask(-1)
sData=SocketRead("TelNet",False)
IF sData="" THEN BREAK
// Loop through each available byte returned from the socket connection.
FOR n=1 _TO_ Length((sData))
// Read next available byte.
x=sData[[n]]
b=Asc(x)
SWITCH nState
CASE 0
// If the current byte is the "Interpret as Command" code, set the state to 1.
IF b=tn_IAC THEN
nState = 1
ELSE
sResult+=x
END
CASE 1
// If the current byte is the "DO" code, set the state to 2.
IF b=tn_DO THEN
nState=2
bConfirm=True
ELSE IF b=tn_WILL THEN
nState=2
bConfirm=False
ELSE
nState=0
// Blindly reject the option.
END
CASE 2
//Write the "Interpret as Command" code, "WONT" code,
//and current byte to the socket and send the contents
//to the server by calling the flush() method.
IF bConfirm THEN SocketWrite("TelNet",Charact(tn_IAC)+Charact(tn_WONT)+Charact(b))
nState = 0
END
END
END
RESULT sResult


Best regards,
Piet

von Piet van Zanten - am 28.05.2016 07:49
Great thanks!

von Arie - am 28.05.2016 13:19
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.