How do I communicate over telnet using cfml?

I have a game server that communicates only via telnet. I have a Lucee server running in the same server room, and wanted to write a small web app that could send commands to the game server. Seemed simple enough, but tens of hours later I had experienced a lot of coming up empty googling and various attempted approaches (like tst10) that didn’t work right.

I eventually got this to work by using Java to establish and manage the telnet connection, with a database table as a go-between so my main thread could see what the telnet connection was seeing. My working code is below.

This code needs substantially cleaned up, but is a working proof of concept showing the basics of how it can be done. Hopefully others that have the same need and have been banging their head against the wall find it useful.

<cfcomponent>

    <cffunction returntype="Struct" name="telnetConnect" access="public" output="no">
        <cfargument name="SID"          type="numeric"  required="yes">
        <cfargument name="telnetAdd" type="string" required="yes">
        <cfargument name="telnetPort" type="numeric" required="yes">

        <cfset retVal           = structNew()>
        <cfset retVal.success   = true>

        <cftry>
            <cfparam name="application.telnet" default="#arrayNew()#">
            <cfparam name="application.telnet[arguments.SID]" default="#structNew()#">
            <cfparam name="application.telnet[arguments.SID].telnetClient" default="">
            <cfparam name="application.telnet[arguments.SID].lastData" default="#Now()#">

            <cfset application.telnet[arguments.SID].telnetData = "">
            <cfset application.telnet[arguments.SID].isReading  = false>

            <cfscript>
                // If there is an existing connection for this server; close it.
                telnetClose(arguments.SID);
                sleep(25);

                // Load the Apache Commons Net TelnetClient class
                application.telnet[arguments.SID].telnetClient = createObject("java", "org.apache.commons.net.telnet.TelnetClient").init();

                // Connect to the Telnet server
                application.telnet[arguments.SID].telnetClient.connect(arguments.telnetAdd, arguments.telnetPort);

                // Get input and output streams
                application.telnet[arguments.SID].inputStream = application.telnet[arguments.SID].telnetClient.getInputStream();
                application.telnet[arguments.SID].outputStream = application.telnet[arguments.SID].telnetClient.getOutputStream();
            </cfscript>

            <cfcatch>
                <cfset retVal.success   = false>
                <cfset retVal.cfcatch   = cfcatch>
            </cfcatch>
        </cftry>

        <cfreturn retVal />
    </cffunction>

    <cffunction returntype="Struct" name="telnetSend" access="public" output="no">
        <cfargument name="SID"          type="numeric"  required="yes">
        <cfargument name="message"      type="string"   required="yes">

        <cfset retValSend           = structNew()>
        <cfset retValSend.success   = true>

        <cftry>

            <cfscript>
                command = "#arguments.message##chr(13)#";
                application.telnet[arguments.SID].outputStream.write(command.getBytes());
                application.telnet[arguments.SID].outputStream.flush();
            </cfscript>

            <cfcatch>
                <cfset retValSend.success   = false>
                <cfset retValSend.cfcatch   = cfcatch>
            </cfcatch>
        </cftry>

        <cfreturn retValSend />
    </cffunction>

    <cffunction returntype="Struct" name="telnetRead" access="public" output="no">
        <cfargument name="SID" type="string" required="yes">

        <cfset var retVal = structNew()>
        <cfset retVal.success = true>

        <cftry>
            <cfset application.telnet[arguments.SID].isReading  = true>

            <cfscript>
                // Create an InputStreamReader from the InputStream
                inputStreamReader = createObject("java", "java.io.InputStreamReader").init(application.telnet[arguments.SID].inputStream, "UTF-8");
                
                // Create a char array for reading
                charBuffer = createObject("java", "java.lang.reflect.Array").newInstance(createObject("java", "java.lang.Character").TYPE, 1024);
                charsRead = 0;
                totalRead = 0;
                maxRead = 5120; // Max chars to read at a time
            </cfscript>

            <cfloop condition="totalRead lt maxRead">
                <cfset charsRead = inputStreamReader.read(charBuffer, 0, 1024)>
                <cfif charsRead is -1>
                    <cfbreak>
                </cfif>

                <cfset dataChunk = createObject("java", "java.lang.String").init(charBuffer, 0, charsRead)>
                <cfset totalRead += charsRead>

                <cfset application.telnet[arguments.SID].telnetData &= dataChunk>

                <cfif dataChunk contains chr(13)>
                    <cfloop list="#dataChunk#" delimiters="#chr(13)#" index="thisLine">
                        <cfquery name="saveChunk">
                            insert into telnetMessages  (mdate, message)
                                        values          (Now(),
                                                         <cfqueryparam cfsqltype="varchar" value="#thisLine#">)
                        </cfquery>
                        <cfset application.telnet[arguments.SID].lastData   = Now()>
                    </cfloop>
                <cfelse>
                    <cfquery name="saveChunk">
                        insert into telnetMessages  (mdate, message)
                                    values          (Now(),
                                                     <cfqueryparam cfsqltype="varchar" value="#dataChunk#">)
                    </cfquery>
                    <cfset application.telnet[arguments.SID].lastData   = Now()>
                </cfif>
            </cfloop>

            <cfset application.telnet[arguments.SID].isReading  = false>

            <cfcatch>
                <cfset application.telnet[arguments.SID].isReading  = false>
                <cfset retVal.success = false>
                <cfset retVal.cfcatch = cfcatch>
            </cfcatch>
            <cffinally>
                <cfset application.telnet[arguments.SID].isReading  = false>
            </cffinally>
        </cftry>

        <cfreturn retVal />
    </cffunction>

    <cffunction returntype="Struct" name="telnetClose" access="public" output="no">
        <cfargument name="SID"          type="numeric"  required="yes">

        <cfset retValClose          = structNew()>
        <cfset retValClose.success  = true>

        <cftry>
            <cfscript>
                if (application.telnet[arguments.SID].telnetClient.isConnected()) {
                    application.telnet[arguments.SID].telnetClient.disconnect();
                }
            </cfscript>

            <cfcatch>
                <cfset retValClose.success  = false>
                <cfset retValClose.cfcatch  = cfcatch>
            </cfcatch>
        </cftry>

        <cfreturn retValClose />
    </cffunction>

</cfcomponent>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật