Jump to content
Sign in to follow this  
JavaScript_Freek

UDPSend

Recommended Posts

JavaScript_Freek

I am trying to make it so when someone quits the application it says "user has logged off". But then I get this error:

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$linedata = $tmp[2]

$linedata = ^ ERROR

(Line 114)

Help, why does this happen? :)

; *********************
; SIMPLE LAN Chat
; By Javascript_Freek
; version 1.5a
; *********************

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

Opt("GUIOnEventMode", 1)

Global $reachserver, $reachdata, $linedata, $chatarea, $userlist, $lanchat_gui, $username
Global $sel, $rename, $menu1, $ms_, $reachcast

; CREATE THE GUI
$lanchat_gui = GUICreate("LAN CHAT v1.5a", 380, 345, -1, -1)

; WHERE YOUR USERNAME IS STORED
Dim $inifile = "username.ini"

; LOADING USERNAME
loadname()
Global $username

; LOGGED IN AS
If $username = "" Then
GUICtrlCreateLabel("Welcome!" & $username, 8, 7, 200)
Else
GUICtrlCreateLabel("Logged in as: " & $username, 8, 7, 200)
Endif

; CHAT AREA
$chatarea = GUICtrlCreateListView("", 8, 32, 250, 257,  $LVS_LIST, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetColumnWidth($chatarea, 0, 100)
_GUICtrlListView_SetExtendedListViewStyle($chatarea, $LVS_EX_FULLROWSELECT)
_GUICtrlListView_SetView($chatarea, 3)

; USERS ONLINE LIST
$userlist = GUICtrlCreateListView("", 270, 32, 105, 257,  $LVS_LIST, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetColumnWidth($userlist, 0, 100)
_GUICtrlListView_SetView($userlist, 3)

; THE INPUT OF YOUR MSGS
$input = GUICtrlCreateInput("", 8, 296, 250, 21)

; SEND BUTTON
$Button1 = GUICtrlCreateButton("send", 270, 296, 105, 21, 0)
GUICtrlSetOnEvent(-1, "sendmsg")

; MENU
$nFileMenu = GUICtrlCreateMenu("File")
    $nExititem2 = GUICtrlCreateMenuItem("New Name", $nFileMenu)
        GUICtrlSetOnEvent(-1, "renameyou")
    $nExititem = GUICtrlCreateMenuItem("Exit", $nFileMenu)
        GUICtrlSetOnEvent(-2, "quit")
$aFileMenu = GUICtrlCreateMenu("About")
    $aAboutitem = GUICtrlCreateMenuItem("About", $aFileMenu)
        GUICtrlSetOnEvent(-3, "about")

; ALLOWS THE PROGRAM TO CLOSE
GUISetOnEvent($GUI_EVENT_CLOSE, "quit")

GUISetState()

;LOADS USERNAME
$username = ""
loadname()

; NO USERNAME FOUND, LOAD THIS
If $username = "" Then
    $username = InputBox("Welcome to the LAN Chat", "Create a username", "")
    Global $username
    If $username = "" Then
        MsgBox(48,"No namer!","You didn't put anything down for your new name. See ya!")
        Exit
        Else
    IniWrite($inifile, "me", 0, $username)
    Endif
Endif

; GETS IP ADDRESS AND DECLARES A VARIABLE
$varIP = @IPAddress1

; STARTS UP UDP
UDPStartup()
; CREATES A SOCKET BOUND TO  AN INCOMING CONNECTION
$reachserver = UDPBind($varIP, 65335)

Sleep(1000)

; SPLITS IP ADDRESS AND DECARES VARIABLES
$divideIP = StringSplit($varIP, ".")
$newaddress = $divideIP[1] & "." & $divideIP[2] & "." & $divideIP[3] & "." & "255"

; OPENS THE SOCKET CONNECTED TO AN EXITISING SERVER USING YOUR SPLIT UP IP ADDRESS
$reachcast = UDPOpen($newaddress, 65335)


While 1
    ;RECIEVING THE DATA FROM AN OPEN SOCKET
    $reachdata = UDPRecv($reachserver, 50)

   ; YOU RECEIVED SUCCESSFULL, SO RUN THIS
    If $reachdata <> "" Then

        $tmp = StringSplit($reachdata, "|")
       
        $linedata = $tmp[2]

        ; If the user who typed the message is NOT you... call this function!
        If StringInStr($linedata, $username) = 0 And StringInStr($tmp[1], ".") <> 4  Then
            
            $str = StringSplit($linedata, "|")
            $last = $str[0]
            _GUICtrlListView_AddItem($chatarea, $str[$last] & " says: " & $tmp[1])
            _FileWriteLog("logs.txt", $str[$last] & " says: " & $tmp[1])
        EndIf
    EndIf
   
   ; SETTING THE HOTKEY OF SENDING MSGS BY PRESSING ENTER
    HotKeySet("{ENTER}", "sendmsg")
    Sleep(50)
   
WEnd

; FUNCTION LOADS FILE FROM CREATED INI
Func loadname()
    $username = IniRead($inifile, "me", 0, "")
EndFunc 


;FUNCTION TO SEND MESSAGES
Func sendmsg()
    If GUICtrlRead($input) = "" Then
        Msgbox(64, "Error", "You did not send anything.")
    Else
        
    UDPSend($reachcast, GUICtrlRead($input) & "|" & $username)  
        
    _GUICtrlListView_AddItem($chatarea, $username & " says: " & GUICtrlRead($input))
    _FileWriteLog("logs.txt", $username & " says: " & GUICtrlRead($input))
    Endif
    GUICtrlSetData($input, "")
    
    _GUICtrlListView_Scroll($chatarea, 0, 150)
EndFunc

; QUITTING PROGRAM FUNCTION
Func quit()
    UDPSend($reachcast,$username &" has logged off.")
    Sleep(1000)
    UDPCloseSocket($varIP)
    UDPShutdown()
    Exit
EndFunc 

;RENAMING YOURSELF MESSAGE BOX INSTRUCTIONS
Func renameyou()
    MsgBox(64,"To change your name","Where you have launched this application, find username.ini."& @LF &"Inside you can edit your username."& @LF &"Before you edit your username, please exit this program.")
EndFunc

; ABOUT THIS PROGRAM MESSAGE BOX
Func about()
    MsgBox(64,"About LAN Chat","Who can connect here? Whoever is on the same LOCAL AREA NETWORK as you. You can connect and chat to others who are usually in the same building or near area.")
EndFunc

[center]Cookyx.com :: Simple LAN Chat[/center]

Share this post


Link to post
Share on other sites
Inverted

Why do you keep declaring $username ?

I see it at least 3 times !

Share this post


Link to post
Share on other sites
Inverted

It's not the answer, but it's something to improve ...

Share this post


Link to post
Share on other sites
Inverted

Share it with us, lol. That's what the forum is about.

Edited by Inverted

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.