Sign in to follow this  
Followers 0
jvanegmond

Proper Telnet Chat

6 posts in this topic

This is a pretty good example of what a telnet chat should be like.. It is client less instant messaging.

I hope some of the newbies here will pick this up and evolve a bit beyond it's current state. It's pretty basic still, but it works great in a LAN!

#include <File.au3>
#include <Array.au3>

Global Const $nWelcomeMessage = "|| Welcome to the chat!"

Dim $sMaxConnections = 50
Dim $sSocket[$sMaxConnections], $sBuffer[$sMaxConnections], $sNickname[$sMaxConnections]

TCPStartup()

$sMainSocket = TCPListen(@IPAddress1,23,5)

While 1
    $sNewSocket = TCPAccept($sMainSocket)
    If $sNewSocket > -1 Then
        For $x = 0 to UBound($sSocket)-1
            If Not $sSocket[$x] Then
                $sSocket[$x] = $sNewSocket
                TCPSend($sSocket[$x],$nWelcomeMessage)
                TCPSend($sSocket[$x], @CRLF & "|| " & @CRLF & "|| Please type a nickname and press enter" & @CRLF & @CRLF & ">")
                ExitLoop
            EndIf
        Next
    EndIf
    For $x = 0 to UBound($sSocket)-1
        If $sSocket[$x] Then
            $sData = TCPRecv($sSocket[$x],100)
            $sBuffer[$x] &= $sData
            If @error Then
                TCPCloseSocket($sSocket[$x])
                $sSocket[$x] = ""
                $sBuffer[$x] = ""
                $sNickname[$x] = ""
            ElseIf Asc($sData) = 0x8 Then ;backspace received
                $len = StringLen($sBuffer[$x])
                $sBuffer[$x] = StringTrimRight($sBuffer[$x],2) ; trim the buffer
                If $len = 1 Then
                    TCPSend($sSocket[$x],">")
                Else
                    TCPSend($sSocket[$x]," " & Chr(0x8))
                EndIf
            EndIf
            If StringInStr($sBuffer[$x],@CRLF) Then
                $sBuffer[$x] = StringTrimRight($sBuffer[$x],2)
                If $sNickname[$x] = "" Then
                    For $j = 0 to UBound($sNickname)-1
                        If $sNickname[$j] = $sBuffer[$x] Then
                            ExitLoop
                        EndIf
                    Next
                    If $j = $sMaxConnections Then
                        $sNickname[$x] = $sBuffer[$x]
                        TCPSend($sSocket[$x],@CRLF & "|| Welcome " & $sNickname[$x] & ". You are now entering chat. " & _ 
                        "There are " & GetNumberOfUsers() & " user(s) chatting." & @CRLF & "||" & @CRLF & GetListOfUsers() & @CRLF & $sNickname[$x] & ">")
                    Else
                        TCPSend($sSocket[$x], @CRLF & "|| Nickname taken. Please pick a new one. " & @CRLF & @CRLF & ">")
                    EndIf
                Else
                    If $sBuffer[$x] = "/quit" Then
                        TCPCloseSocket($sSocket[$x])
                        $sSocket[$x] = ""
                        $sBuffer[$x] = ""
                        $sNickname[$x] = ""
                    ElseIf $sBuffer[$x] = "/list" Then
                        TCPSend($sSocket[$x] , @CRLF & GetListOfUsers() & @CRLF & @CRLF & ">")
                    Else
                        For $j = 0 To UBound($sNickname)-1
                            If $j <> $x Then
                                If $sNickname[$j] Then
                                    $sBackspaces = ""
                                    $sSpaces = ""
                                    For $k = 0 to 30
                                        $sBackspaces &= Chr(0x8)
                                        $sSpaces &= " "
                                    Next
                                    TCPSend($sSocket[$j],$sBackspaces & $sSpaces & $sBackspaces) ; clears line and goes to the beginning
                                    TCPSend($sSocket[$j],$sNickname[$x] & ">" & $sBuffer[$x] & @CRLF & $sNickname[$j] & ">" & $sBuffer[$j])
                                EndIf
                            Else
                                TCPSend($sSocket[$x],$sNickname[$x] & ">")
                            EndIf
                        Next
                    EndIf
                EndIf
                $sBuffer[$x] = ""
            EndIf
        EndIf
    Next
WEnd

Func GetNumberOfUsers()
    Local $sReturn = 0
    For $i = 0 to UBound($sSocket)-1
        If $sNickname[$i] Then
            $sReturn += 1
        EndIf
    Next
    Return $sReturn
EndFunc

Func GetListOfUsers()
    Local $sReturn = "|| Online users:" & @CRLF
    For $i = 0 to UBound($sSocket)-1
        If $sNickname[$i] Then
            $sReturn &= "|| " & $sNickname[$i] & @CRLF
        EndIf
    Next
    Return $sReturn
EndFunc

Share this post


Link to post
Share on other sites



works pretty good..

nice job!

Share this post


Link to post
Share on other sites

Thank you, very nice job! Really a good starting point.

Share this post


Link to post
Share on other sites

I've thought of putting two servers in the same script. You type messages in one client, and receive all the messages in the other client.

That would work perfectly, if you can get two servers in the same script. But I have no doubt about it, that is possible.

Share this post


Link to post
Share on other sites

Very nice. I've got a question, though. I'm trying to make a program that will allow one to connect to a telnet server, display what it has to show, and send commands to it. Although every time i try to open the connection, @error indicates that there is no connection. I ask you because you seem to know more about this sort of things. Any help would be appreciated.


~T3CHM4G3~PROjECTS: Telnet Client

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0