Sign in to follow this  
Followers 0
vikingnor

Chat Client and server

1 post in this topic

long time ago i worked on some scripts to make a chat server/client, but cant seem to make it work again, and i guess anyone here might know whats wrong.

Client:

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Images\icon.ico
#AutoIt3Wrapper_outfile=IK-Chat Client.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Run_Tidy=y
#Tidy_Parameters=/rel /sf /sfc
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#include <guiconstants.au3>
Global $connect_port = 34000
$ip = ("Server ip here.")
TCPStartup()
$socket = TCPConnect($ip, $connect_port)
If $socket = -1 Then
    Exit
EndIf
Do
    Do
        $user = InputBox("Connected", "Connection established please type in your desired Username")
        If @error = 1 Then
            $end = MsgBox(4, "End client", "Sure you want to exit?")
            If $end = 6 Then
                Exit
            EndIf
        EndIf
    Until StringLen($user) > 3 And StringLen($user) < 12
    TCPSend($socket, "us:" & $user)
    Do
        $recv = TCPRecv($socket, 512)
        If $recv = "password"  Then
            $pw = InputBox("Password required", "Please type in the password for " & $user, "", "*")
            TCPSend($socket, "pw:" & $pw)
        EndIf
    Until $recv = "accepted"  Or $recv = "rejected"  Or @error = 1
Until $recv = "accepted"  Or $recv = "rejected" 
If $recv = "rejected"  Then
    MsgBox(0, "Failure", "Maximum number of users reached or Server currently not availible, please try again later")
    Exit
EndIf
$main = GUICreate("Chronos Chat @ " & $user, 450, 200, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
$input = GUICtrlCreateInput("", 20, 170, 280, 20)
$edit = GUICtrlCreateEdit("", 20, 30, 270, 120, $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL + $ES_AUTOVSCROLL)
$userlist = GUICtrlCreateEdit("", 330, 30, 100, 120, $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL + $ES_AUTOVSCROLL)
$send = GUICtrlCreateButton("Send", 320, 170, 120, 20, $BS_DEFPUSHBUTTON)
$chatgroup = GUICtrlCreateGroup("Chat", 10, 10, 290, 150)
$usergroup = GUICtrlCreateGroup("Users", 320, 10, 120, 150)
GUICtrlSetState($input, $GUI_FOCUS)
GUISetState()
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        TCPSend($socket, "bye")
        Sleep(5000)
        TCPShutdown()
        Exit
    EndIf
    If $msg = $send Then
        If GUICtrlRead($input) <> "" Then
            TCPSend($socket, GUICtrlRead($input))
            GUICtrlSetData($input, "")
        EndIf
    EndIf
    $recv = TCPRecv($socket, 512)
    $err = @error
    If $recv = "bye"  Then
        GUICtrlSetData($edit, "Connection Lost" & @CRLF & GUICtrlRead($edit))
        TCPShutdown()
        ExitLoop
    EndIf
    If $recv <> "" And $err = 0 And Not StringInStr($recv, "users:") Then
        GUICtrlSetData($edit, $recv & @CRLF & GUICtrlRead($edit))
    EndIf
    If StringInStr($recv, "users:") Then
        $users = StringTrimLeft($recv, 8)
        $users = StringReplace($users, "|", @CRLF)
        GUICtrlSetData($userlist, $users)
    EndIf
WEnd
Func OnAutoItExit()
    TCPSend($socket, "bye")
    TCPCloseSocket($socket)
    TCPShutdown()
EndFunc  ;==>OnAutoItExit

and here is the code for Server

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Images\icon.ico
#AutoIt3Wrapper_outfile=IK-Chat Server.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Run_Tidy=y
#Tidy_Parameters=/rel /sf /sfc
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#include <guiconstants.au3>
Global $g_port = 34000
Global $g_IP = @IPAddress1
Global $MainSocket
Dim $ConnectedSocket[101]
Dim $recv[101]
Dim $user[101]
Dim $pw[101]
For $i = 1 To 100 Step 1
    $ConnectedSocket[$i] = -1
Next
TCPStartup()
$MainSocket = TCPListen($g_IP, $g_port, 100)
If $MainSocket = -1 Then Exit
$RogueSocket = -1
$input = GUICtrlCreateInput("", 10, 10, 280)
$main = GUICreate("Chat Server", 600, 400, -1, -1)
Opt("GuiOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "GUI_Close")
$edit = GUICtrlCreateEdit("", 20, 30, 270, 350, $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL + $ES_AUTOVSCROLL)
$userlist = GUICtrlCreateEdit("Server", 440, 30, 140, 350, $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL + $ES_AUTOVSCROLL + $ES_WANTRETURN)
$Group_3 = GUICtrlCreateGroup("Users Connected", 430, 10, 160, 380)
$Group_4 = GUICtrlCreateGroup("Chat Log", 10, 10, 290, 380)
$Button_5 = GUICtrlCreateButton("test", 320, 30, 90, 30)
$Group_6 = GUICtrlCreateGroup("Menu", 310, 10, 110, 380)
$Button_7 = GUICtrlCreateButton("test", 320, 70, 90, 30)
$Button_8 = GUICtrlCreateButton("test", 320, 110, 90, 30)
$Button_9 = GUICtrlCreateButton("test", 320, 150, 90, 30)
$Button_10 = GUICtrlCreateButton("test", 320, 190, 90, 30)
$Button_11 = GUICtrlCreateButton("Stop Server", 320, 350, 90, 30)
GUICtrlSetOnEvent(-1, "GUI_Close")
$send = GUICtrlCreateButton("Refresh", 320, 230, 90, 30, $BS_DEFPUSHBUTTON)
GUICtrlSetState($input, $GUI_FOCUS)
GUISetState()
$users = "Server" & @CRLF
$users1 = $users
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        For $i = 1 To 100 Step 1
            If $ConnectedSocket[$i] <> -1 Then
                TCPSend($ConnectedSocket[$i], "~~bye")
            EndIf
        Next
        TCPShutdown()
        Exit
    EndIf
    If $msg = $send Then
        If GUICtrlRead($input) <> "" Then
            For $i = 1 To 100 Step 1
                If $ConnectedSocket[$i] <> -1 Then
                    TCPSend($ConnectedSocket[$i], "Server: " & GUICtrlRead($input))
                EndIf
            Next
            GUICtrlSetData($edit, GUICtrlRead($input) & @CRLF & GUICtrlRead($edit))
            GUICtrlSetData($input, "")
        EndIf
    EndIf
    For $i = 1 To 100 Step 1
        If $ConnectedSocket[$i] <> -1 Then
            $recv[$i] = TCPRecv($ConnectedSocket[$i], 512)
            $err = @error
            If $recv[$i] = "~~bye"  Then
                $ConnectedSocket[$i] = -1
                TCPCloseSocket($ConnectedSocket[$i])
                $test = $user[$i] & "|" 
                $users = StringReplace($users, $test, "")
                GUICtrlSetData($userlist, $users)
                If StringReplace($users1, "|", @CRLF) <> $users Then
                    For $j = 1 To 100
                        If $ConnectedSocket[$i] <> -1 Then
                            TCPSend($ConnectedSocket[$i], "~~users:" & $users1)
                        EndIf
                    Next
                EndIf
            EndIf
            If StringInStr($recv[$i], "~~us:") Then
                $user[$i] = StringTrimLeft($recv[$i], 5)
                $test = $user[$i] & @CRLF
                If Not StringInStr($users, $test) Then
                    If IniRead("chat.ini", "Users", $user[$i], "") <> "" Then
                        TCPSend($ConnectedSocket[$i], "~~password")
                    Else
                        TCPSend($ConnectedSocket[$i], "~~accepted")
                        USERLIST()
                    EndIf
                EndIf
            EndIf
            If StringInStr($recv[$i], "~~pw:") Then
                $pw[$i] = StringTrimLeft($recv[$i], 5)
                If IniRead("chat.ini", "Users", $user[$i], "") = $pw[$i] Then
                    TCPSend($ConnectedSocket[$i], "~~accepted")
                    USERLIST()
                Else
                    TCPSend($ConnectedSocket[$i], "~~password")
                EndIf
            EndIf
            If $recv[$i] <> "" And $err = 0 And $recv[$i] <> "~~bye"  And Not StringInStr($recv[$i], "~~us:") And Not StringInStr($recv[$i], "~~pw:") Then
                For $j = 1 To 100 Step 1
                    If $ConnectedSocket[$j] <> -1 Then
                        TCPSend($ConnectedSocket[$j], $user[$i] & ": " & $recv[$i])
                    EndIf
                Next
                GUICtrlSetData($edit, $user[$i] & ": " & $recv[$i] & @CRLF & GUICtrlRead($edit))
            EndIf
        Else
            $ConnectedSocket[$i] = TCPAccept($MainSocket)
        EndIf
    Next
WEnd
Func GUI_Close()
    Exit
EndFunc  ;==>GUI_Close
Func USERLIST()
    For $i = 1 To 100 Step 1
        If $ConnectedSocket[$i] <> -1 Then
            $test = $user[$i] & @CRLF
            If Not StringInStr($users, $test) Then
                $users = $users & $user[$i] & @CRLF
            EndIf
        ElseIf $ConnectedSocket[$i] = -1 Then
        ;$users = stringreplace ($users, $user[$i], "")
        EndIf
    Next
    $users1 = StringReplace($users, @CRLF, "|")
    For $i = 1 To 100 Step 1
        If $ConnectedSocket[$i] <> -1 Then
            TCPSend($ConnectedSocket[$i], "~~users:" & $users1)
        EndIf
    Next
    GUICtrlSetData($userlist, $users)
EndFunc  ;==>USERLIST

The problem is.. that i can compile it with no error output, but still it wont run.

thanks for any help, i also attached the files.

IK_Chat_Client.au3

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