Sign in to follow this  
Followers 0
Andreik

Client error

1 post in this topic

#1 ·  Posted (edited)

I started to create a chatroom and server seems to work fine but I don't know why client doesn't receive any data from server.

Server.au3

#include <TCP.au3>
#include <GUIListView.au3>
#include <GuiTab.au3>
#include <Array.au3>
Global Const $CHAT_NAME = "Andreik Chat"
Global Const $CHAT_X = 500
Global Const $CHAT_Y = 400
Global Const $CHAT_STYLE = BitOR(0x00040000,0x00080000,0x00C00000)
Global Const $TAB_STYLE = BitOR(0x8000,0x0008,0x0400)
Global Const $EDIT_STYLE = BitOR(0x00200000,0x0040,0x0800)
Global Const $INPUT_STYLE = BitOR(0x00200000,0x0040)
Global Const $INI_FILE = @ScriptDir & "\cfg.ini"
Global Const $BLACK_LIST = @ScriptDir & "\blacklist.dat"
Global $MAIN, $CHAT_LIST, $CHAT_CONTAINER, $CHAT_INPUT
Global $WELCOME_INPUT, $MAX_USER_INPUT, $IP_INPUT, $PORT_INPUT, $MAX_MSG_LEN_INPUT
Global $USER_RESTRICT_INPUT
Global $ADMIN_NAME = "Andreik"
Global $SERVER
Global $SERVER_RUN = False
Global $CLIENT_NUM = 0
Global $CLIENT_NICK[1]
Global $CLIENT_SOCKET[1]

$MAIN = GUICreate($CHAT_NAME,$CHAT_X,$CHAT_Y,-1,-1,$CHAT_STYLE)
$TAB = GUICtrlCreateTab(5,5,$CHAT_X-10,$CHAT_Y-10,$TAB_STYLE)
_GUICtrlTab_SetBkColor($MAIN,$TAB,0xE0DFE3)
$TAB_CHAT = GUICtrlCreateTabItem("Chat")
$CHAT_CONTAINER = GUICtrlCreateEdit("",10,30,285,$CHAT_Y-100,$EDIT_STYLE)
$CHAT_LIST = GUICtrlCreateListView("Username",300,30,190,$CHAT_Y-40)
$CHAT_INPUT = GUICtrlCreateEdit("",10,335,285,55,$INPUT_STYLE)
_GUICtrlListView_SetColumnWidth($CHAT_LIST,0,170)
$TAB_CONFIG = GUICtrlCreateTabItem("Config")
$WELCOME_LABEL = GUICtrlCreateLabel("Welcome message",10,42,95,20,0x01)
$WELCOME_INPUT = GUICtrlCreateInput("Welcome to " & $CHAT_NAME & "!",110,40,150,20)
$MAX_USER_LABEL = GUICtrlCreateLabel("Max users",10,67,95,20,0x01)
$MAX_USER_INPUT = GUICtrlCreateInput("50",110,65,50,20,BitOR(0x2000,0x01))
$IP_LABEL = GUICtrlCreateLabel("Machine IP",10,92,100,20,0x01)
$IP_INPUT = GUICtrlCreateInput(@IPAddress1,110,90,87,20,0x01)
$PORT_LABEL = GUICtrlCreateLabel("Port",10,117,100,20,0x01)
$PORT_INPUT = GUICtrlCreateInput("6107",110,115,40,20,0x01)
$MAX_MSG_LEN_LABEL = GUICtrlCreateLabel("Max message length",10,142,100,20,0x01)
$MAX_MSG_LEN_INPUT = GUICtrlCreateInput("250",110,140,50,20,0x01)
$USER_RESTRICT_LABEL = GUICtrlCreateLabel("User restrict IP",10,167,100,20,0x01)
$USER_RESTRICT_INPUT = GUICtrlCreateInput("",110,165,87,20,0x01)
$USER_RESTRICT_BUTTON = GUICtrlCreateButton("Restrict",200,165,50,20)
$SAVE_CONFIG_BUTTON = GUICtrlCreateButton("Save config",30,200,80,20)
$START_SERVER_BUTTON = GUICtrlCreateButton("Start server",120,200,80,20)
GUISetState(@SW_SHOW,$MAIN)

While True
    $MSG = GUIGetMsg()
    Switch $MSG
        Case $START_SERVER_BUTTON
            If Not $SERVER_RUN Then
                $SERVER_IP = GUICtrlRead($IP_INPUT)
                $SERVER_PORT = GUICtrlRead($PORT_INPUT)
                If IsValidIp($SERVER_IP) Then
                    $SERVER = _TCP_Server_Create($SERVER_PORT,$SERVER_IP)
                    If @error Then ChatError(1,True)
                    _TCP_RegisterEvent($SERVER,$TCP_NEWCLIENT,"AddClient")
                    _TCP_RegisterEvent($SERVER,$TCP_DISCONNECT,"Disconnect")
                    _TCP_RegisterEvent($SERVER,$TCP_RECEIVE,"DataReceive")
                    GUICtrlSetData($START_SERVER_BUTTON,"Stop Server")
                    $SERVER_RUN = True
                    $ADMIN = GUICtrlCreateListViewItem($ADMIN_NAME,$CHAT_LIST)
;~                  GUICtrlSetImage($ADMIN,"admin.ico","",2)
                    Dim $CLIENT_NICK[1]
                    Dim $CLIENT_SOCKET[1]
                    AdlibEnable("SendUserList",1500)
                Else
                    ChatError(2,False)
                EndIf
            Else
                AdlibDisable()
                _TCP_Server_Broadcast("Server was stopped.")
                _TCP_Server_Stop()
                _GUICtrlListView_DeleteAllItems($CHAT_LIST)
                $CLIENT_NICK = 0
                $CLIENT_SOCKET = 0
                GUICtrlSetData($START_SERVER_BUTTON,"Start Server")
                $SERVER_RUN = False
            EndIf
        Case $SAVE_CONFIG_BUTTON
            IniWrite($INI_FILE,"CONFIG","WELCOME",GUICtrlRead($WELCOME_INPUT))
            IniWrite($INI_FILE,"CONFIG","MAX_USERS",GUICtrlRead($MAX_USER_INPUT))
            IniWrite($INI_FILE,"CONFIG","MAX_MSG_LEN",GUICtrlRead($MAX_MSG_LEN_INPUT))
            IniWrite($INI_FILE,"CONFIG","IP",GUICtrlRead($IP_INPUT))
            IniWrite($INI_FILE,"CONFIG","PORT",GUICtrlRead($PORT_INPUT))
        Case $USER_RESTRICT_BUTTON
            $RS_USER = GUICtrlRead($USER_RESTRICT_INPUT)
            If IsValidIp($RS_USER) Then
                Local $FILE = FileOpen($BLACK_LIST,1)
                FileWriteLine($FILE,$RS_USER)
                FileChangeDir($FILE)
                GUICtrlSetData($USER_RESTRICT_INPUT,"")
            Else
                ChatError(2,"False")
            EndIf
        Case -3
            Quit()
    EndSwitch
    If WinActive($CHAT_NAME) Then
        HotKeySet("{ENTER}","SendMsg")
    Else
        HotKeySet("{ENTER}")
    EndIf
    Sleep(10)
WEnd

Func AddClient($SOCKET,$ERROR)
    Local $CLIENT_IP = _TCP_Server_ClientIP($SOCKET)
    If IsRestricted($CLIENT_IP) Then
        _TCP_Send($SOCKET,"Your IP is banned.")
        _TCP_Server_DisconnectClient($SOCKET)
    ElseIf $CLIENT_NUM > GUICtrlRead($MAX_USER_INPUT) Then
        _TCP_Send($SOCKET,"Chatroom is full.")
        _TCP_Server_DisconnectClient($SOCKET)
    Else
        _TCP_Send($SOCKET,"%user%")
        $CLIENT_NUM += 1
    EndIf
EndFunc

Func Disconnect($SOCKET,$ERROR)
    $INDEX = _ArraySearch($CLIENT_SOCKET,$SOCKET,Default,Default,True)
    If Not @error Then
        _ArrayDelete($CLIENT_SOCKET,$INDEX)
        _ArrayDelete($CLIENT_NICK,$INDEX)
        $CLIENT_NUM -= 1
    EndIf
EndFunc

Func DataReceive($SOCKET,$RECV,$ERROR)
    If StringLeft($RECV,6) = "%user%" Then
        _ArrayAdd($CLIENT_NICK,StringTrimLeft($RECV,6))
        _ArrayAdd($CLIENT_SOCKET,$SOCKET)
        GUICtrlCreateListViewItem(StringTrimLeft($RECV,6),$CHAT_LIST)
    ElseIf $RECV = "%disconnect%" Then
        _TCP_Send($SOCKET,"Good bye.")
        _TCP_Server_DisconnectClient($SOCKET)
    ElseIf StringLeft($RECV,5) = "%msg%" Then
        $RECV = StringTrimLeft($RECV,5)
        If StringLen($RECV) > GUICtrlRead($MAX_MSG_LEN_INPUT) Then
            _TCP_Send($SOCKET,"Your message is too long. Limit is " & GUICtrlRead($MAX_MSG_LEN_INPUT) & " chars.")
        Else
            Local $INDEX = _ArraySearch($CLIENT_SOCKET,$SOCKET)
            GUICtrlSetData($CHAT_CONTAINER,"<" & $CLIENT_NICK[$INDEX] & "> " & $RECV & @CRLF,1)
            _TCP_Server_Broadcast($RECV)
        EndIf
    EndIf
EndFunc

Func SendMsg()
    Local $BROADCAST_MSG = GUICtrlRead($CHAT_INPUT)
    If $BROADCAST_MSG <> "" Then
        _TCP_Server_Broadcast($BROADCAST_MSG)
        GUICtrlSetData($CHAT_CONTAINER,"<" & $ADMIN_NAME & "> " & GUICtrlRead($CHAT_INPUT) & @CRLF,1)
        GUICtrlSetData($CHAT_INPUT,"")
        ControlFocus($CHAT_NAME,"",$CHAT_INPUT)
    EndIf
EndFunc

Func _GUICtrlTab_SetBkColor($hWnd, $hSysTab32, $sBkColor)
    Local $aTabPos = ControlGetPos($hWnd, "", $hSysTab32)
    Local $aTab_Rect = _GUICtrlTab_GetItemRect($hSysTab32, -1)
    GUICtrlCreateLabel("", $aTabPos[0]+2, $aTabPos[1]+$aTab_Rect[3]+4, $aTabPos[2]-4, $aTabPos[3]-$aTab_Rect[3]-7)
    GUICtrlSetBkColor(-1, $sBkColor)
    GUICtrlSetState(-1,128)
EndFunc

Func IsValidIp($IP)
    Local $SPLIT = StringSplit($IP,".")
    If IsArray($SPLIT) Then
        If $SPLIT[0] = 4 Then
            For $INDEX = 1 To 4
                If $SPLIT[$INDEX] > 255 Or $SPLIT[$INDEX] < 0 Then Return False
            Next
        Else
            Return False
        EndIf
    Else
        Return False
    EndIf
    Return True
EndFunc

Func ChatError($NUM,$CRITICAL=False,$FLAG=0x40)
    If $NUM = 1 Then
        MsgBox(0x40,"Error","Unable to start server")
    ElseIf $NUM = 2 Then
        TrayTip("Error","Invalid IP address.",3)
    EndIf
    If $CRITICAL Then Quit()
EndFunc

Func IsRestricted($IP)
    Local $FILE = FileOpen($BLACK_LIST,0)
    Local $COUNT = 1
    While True
        $LINE = FileReadLine($BLACK_LIST,$COUNT)
        If @error Then ExitLoop
        If $LINE = $IP Then
            FileClose($BLACK_LIST)
            Return True
        EndIf
        $COUNT += 1
    WEnd
    FileClose($FILE)
    Return False
EndFunc

Func SendUserList()
    Local $INDEX
    Local $BROADCAST_MSG = "%userlist%" & $ADMIN_NAME & "|"
    For $INDEX = 1 To UBound($CLIENT_NICK)-1
        $BROADCAST_MSG &= $CLIENT_NICK[$INDEX] & "|"
    Next
    _TCP_Server_Broadcast(StringTrimRight($BROADCAST_MSG,1))
EndFunc

Func Quit()
    _TCP_Server_Broadcast("Server was stopped.")
    _TCP_Server_Stop()
    Exit
EndFunc

Client.au3

#include <TCP.au3>
#include <GUIListView.au3>

Global Const $CHAT_NAME = "__Andy__ Chat"
Global Const $CHAT_X = 500
Global Const $CHAT_Y = 400
Global Const $CHAT_STYLE = BitOR(0x00040000,0x00080000,0x00C00000)
Global Const $TAB_STYLE = BitOR(0x8000,0x0008,0x0400)
Global Const $EDIT_STYLE = BitOR(0x00200000,0x0040,0x0800)
Global Const $INPUT_STYLE = BitOR(0x00200000,0x0040)
Global $MAIN, $CHAT_LIST, $CHAT_CONTAINER, $CHAT_INPUT
Global $USER
Global $CLIENT

$SERVER_IP = InputBox($CHAT_CONTAINER,"Type server IP address",@IPAddress1)
$SERVER_PORT = InputBox($CHAT_CONTAINER,"Type server port","6107")
$USER = InputBox($CHAT_NAME,"Type a nickname")

$MAIN = GUICreate($CHAT_NAME,$CHAT_X,$CHAT_Y,-1,-1,$CHAT_STYLE)
$CHAT_CONTAINER = GUICtrlCreateEdit("",10,30,285,$CHAT_Y-100,$EDIT_STYLE)
$CHAT_LIST = GUICtrlCreateListView("Username",300,30,190,$CHAT_Y-40)
$CHAT_INPUT = GUICtrlCreateEdit("",10,335,285,55,$INPUT_STYLE)
_GUICtrlListView_SetColumnWidth($CHAT_LIST,0,170)
$CLIENT = _TCP_Client_Create($SERVER_PORT,$SERVER_IP)
_TCP_RegisterEvent($CLIENT,$TCP_CONNECT,"Connect")
_TCP_RegisterEvent($CLIENT,$TCP_DISCONNECT,"Disconnect")
_TCP_RegisterEvent($CLIENT,$TCP_RECEIVE,"DataReceive")
GUISetState(@SW_SHOW,$MAIN)

While True
    $MSG = GUIGetMsg()
    Switch $MSG
        Case -3
            Quit()
    EndSwitch
    If WinActive($CHAT_NAME) Then
        HotKeySet("{ENTER}","SendMsg")
    Else
        HotKeySet("{ENTER}")
    EndIf
    Sleep(10)
WEnd

Func DataReceive($SOCKET,$RECV,$ERROR)
    If $RECV = "%user%" Then
        _TCP_Send($CLIENT,"%user%" & $USER)
    ElseIf $RECV = "%userlist%" Then
        $RECV = StringTrimLeft($RECV,10)
        Local $SPLIT = StringSplit($RECV,"|")
        If IsArray($SPLIT) Then
            _GUICtrlListView_DeleteAllItems($CHAT_LIST)
            For $INDEX = 1 To $SPLIT[0]
                If $INDEX = 1 Then
                    $ADMIN = GUICtrlCreateListViewItem($SPLIT[$INDEX],$CHAT_LIST)
                    GUICtrlSetImage($ADMIN,"admin.ico","",2)
                Else
                    GUICtrlCreateListViewItem($SPLIT[$INDEX],$CHAT_LIST)
                EndIf
            Next
        EndIf
    EndIf
EndFunc

Func SendMsg()
    Local $BROADCAST_MSG = GUICtrlRead($CHAT_INPUT)
    If $BROADCAST_MSG <> "" Then
        _TCP_Send($CLIENT,$BROADCAST_MSG)
        GUICtrlSetData($CHAT_CONTAINER,"<" & $USER & "> " & @CRLF,1)
        GUICtrlSetData($CHAT_INPUT,"")
        ControlFocus($CHAT_NAME,"",$CHAT_INPUT)
    EndIf
EndFunc

Func Quit()
    _TCP_Send($CLIENT,"%disconnect%")
    _TCP_Client_Stop($CLIENT)
    Exit
EndFunc
Edited by Andreik

When the words fail... music speaks

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