Sign in to follow this  
Followers 0
Aceguy

Serverless Lan chat

8 posts in this topic

Hopes you like it

Not all my work, thanks to contributor (forgot name).

#include<Array.au3>
#include<GuiListView.au3>
#include<Misc.au3>
#include<GUIConstantsEx.au3>
#include<ListViewConstants.au3>
#include<WindowsConstants.au3>
#include<StructureConstants.au3>
#include <ButtonConstants.au3>
#include <Sound.au3>


Opt("GUIOnEventMode", 1)
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=chat.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Global $contactserver, $buddy, $contactdata, $msgdata, $sel, $rename, $menu1, $ms_
Global $hlistview, $c_list, $chat_gui, $buttoncontext, $buttonitem, $msg, $me
Dim $buddy[1]
Dim $inifile = @MyDocumentsDir & "\chat.ini"

$chat_gui = GUICreate("chat 1.1", 212, 390, 526, 146, -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_ACCEPTFILES, $WS_EX_TOPMOST))
GUISetBkColor(0xC8C8FF, $chat_gui)

$c_list = GUICtrlCreateListView("", 8, 278, 193, 103, $LVS_LIST, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetColumnWidth($c_list, 0, 100)
_GUICtrlListView_SetExtendedListViewStyle($c_list, $LVS_EX_FULLROWSELECT)
_GUICtrlListView_SetView($c_list, 3)

$input = GUICtrlCreateInput("", 5, 5, 150, 25)
GUICtrlSetOnEvent(-1, "sendit")

$hlistview = GUICtrlCreateListView("", 8, 45, 193, 230, $LVS_LIST, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetColumnWidth($hlistview, 0, 100)
_GUICtrlListView_SetExtendedListViewStyle($hlistview, $LVS_EX_FULLROWSELECT)
_GUICtrlListView_SetView($hlistview, 3)

GUISetOnEvent($GUI_EVENT_CLOSE, "quit")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()
$me = ""
load()
If $me = "" Then
    $me = InputBox("Who Are you", "Your name", "")
    Global $me
    IniWrite($inifile, "me", 0, $me)
    _GUICtrlListView_AddItem($c_list, $me)
    _ArrayAdd($buddy, $me)
EndIf

$PrivateIP = @IPAddress1
; Start The UDP Services
;==============================================
UDPStartup()

; Bind to a SOCKET
;==============================================

$contactserver = UDPBind($PrivateIP, 65335)

Sleep(1000)
;create multicast address
$splitaddress = StringSplit($PrivateIP, ".")
$changeaddress = $splitaddress[1] & "." & $splitaddress[2] & "." & $splitaddress[3] & "." & "255"

;send packet
$contactcast = UDPOpen($changeaddress, 65335)

AdlibEnable("check", 1000)

While 1
    $contactdata = UDPRecv($contactserver, 50)
    If StringInStr($contactdata, "|") > 0 Then
        $str = StringSplit($contactdata, "|")
        
        If $contactdata <> "" And StringInStr($str[1], ".") = 4 And StringInStr($str[1], ".", 0, 2) = 8 Then
            $ar = _ArraySearch($buddy, $str[2])
            If @error Then
                ConsoleWrite("Adding via Buddy")
                _ArrayAdd($buddy, $str[2])
                _GUICtrlListView_AddItem($c_list, $str[2])
            Else
                $c = _GUICtrlListView_GetItemText($c_list, $ar - 1)
                If StringInStr($c, "On-line") = 0 Then
                    _GUICtrlListView_SetItemText($c_list, $ar - 1, $str[2] & " On-line")

                    If $str[2] <> $me Then _GUICtrlListView_AddItem($hlistview, $str[2] & " Has signed on")
                    _SoundPlay("C:\Windows\Media\Speech on.wav",0)
                EndIf
            EndIf
            
        EndIf
    EndIf
    
    If $contactdata <> "" Then

        $tmp = StringSplit($contactdata, "|")
        
        $msgdata = $tmp[2]

        If StringInStr($msgdata, $me) = 0 And StringInStr($tmp[1], ".") <> 4 And StringInStr($tmp[2], "Has signed") = 0 Then
            $str = StringSplit($msgdata, "|")
            $last = $str[0]
            _SoundPlay("Windows Shutdown.wav", 0)
            _GUICtrlListView_AddItem($hlistview, $str[$last] & " Says: " & $tmp[1])
            _GUICtrlListView_Scroll($hlistview, 0, 50)
        ElseIf StringInStr($msgdata, $me) = 0 And StringInStr($tmp[1], ".") <> 4 And StringInStr($tmp[2], "Has signed") > 0 Then
            $str = StringSplit($msgdata, "|")
            $last = $str[0]
            If StringInStr($tmp[2], "Has signed off") > 0 Then
                _GUICtrlListView_AddItem($hlistview, $tmp[1] & " " & $str[$last])
                _GUICtrlListView_Scroll($hlistview, 0, 50)
                $ar = _ArraySearch($buddy, $tmp[1])
                _GUICtrlListView_SetItemText($c_list, $ar - 1, $tmp[1] & " Off-line")
                
            EndIf
            
        EndIf
    EndIf
    
    If _IsPressed("OD") Then sendit()
    Sleep(50)
    
WEnd

Func check()
    If $me <> "" Then UDPSend($contactcast, @IPAddress1 & "|" & $me)
EndFunc   ;==>check

Func quit()
    _SoundPlay("C:\Windows\Media\Speech Off.wav",0)
    AdlibDisable()
    Sleep(1000)
    UDPSend($contactcast, $me & "|Has signed off")
    UDPCloseSocket($PrivateIP)
    UDPShutdown()
    For $i = 1 To UBound($buddy) - 1
        IniWrite($inifile, "contacts", $i - 1, $buddy[$i])
    Next
    
    Exit
EndFunc   ;==>quit

Func load()
    $me = IniRead($inifile, "me", 0, "")
    $var = IniReadSection($inifile, "contacts")
    
    If Not @error Then
        For $x = 1 To $var[0][0]
            If $var[$x][1] <> @IPAddress1 Then
                _GUICtrlListView_AddItem($c_list, $var[$x][1] & " Off-line")
                _ArrayAdd($buddy, $var[$x][1])
            Else
                If $me <> "" Then
                    _GUICtrlListView_AddItem($c_list, $me & " Off-line")
                    _ArrayAdd($buddy, $me)
                EndIf
            EndIf
        Next
    EndIf
    
    
EndFunc   ;==>load

Func sendit()
    ConsoleWrite("SEND" & @LF)
    UDPSend($contactcast, GUICtrlRead($input) & "|" & $me)
    _GUICtrlListView_AddItem($hlistview, $me & " Sends: " & GUICtrlRead($input))
    GUICtrlSetData($input, "")
    _GUICtrlListView_Scroll($hlistview, 0, 50)
EndFunc   ;==>sendit

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $iCode, $tInfo, $iItem
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $iCode
        Case $NM_RCLICK
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
            $sel = DllStructGetData($tInfo, "Index")
            ConsoleWrite("Right click")
    
            Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
            If $iItem = _GUICtrlListView_GetColumnCount($hListView) - 1 Then Return 1
            ConsoleWrite("double click")
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func rename()
    ConsoleWrite("func rename")
EndFunc   ;==>rename

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

It works if I use 1.. but when I use 2 I cant talk to eachother

Edited by ludocus

Share this post


Link to post
Share on other sites

how do i use this?

Share this post


Link to post
Share on other sites

ace...

$changeaddress = $splitaddress[1] & "." & $splitaddress[2] & "." & $splitaddress[3] & "." & "255"
won't work for everyone... you should calculate the broadcast address using the ip address and the subnet mask... so that it will work even for thoose without /24 ip classes


Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Share this post


Link to post
Share on other sites

ace...

$changeaddress = $splitaddress[1] & "." & $splitaddress[2] & "." & $splitaddress[3] & "." & "255"
won't work for everyone... you should calculate the broadcast address using the ip address and the subnet mask... so that it will work even for thoose without /24 ip classes
Pretty sure this will raise some eyebrows but you are fully correct that the Broadcast address depends on the subnet mask used. :)

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

although i'm sure it won't work for everybody... for more than one reason... here's a start ace :)

#include<array.au3>
$out=Run("ipconfig" , "" ,@SW_SHOW,2)
ProcessWaitClose($out)
$data=StdoutRead($out)
$mask=StringRegExp($data,"   Subnet Mask . . . . . . . . . . . : ((([0-9]){1,3}\.){3}[0-9]{1,3})",3)
$ip=StringSplit(@IPAddress1,".")
$mask=StringSplit($mask[0],".")
Dim $net[5]
Dim $wc[5]
Dim $bc[5]
For $i=1 To 4
    $net[$i]=BitAND($ip[$i],$mask[$i])
    $wc[$i]=BitXOR($mask[$i],255)
    $bc[$i]=lif($wc[$i],$ip[$i])
Next
_ArrayDisplay($wc)
$BROADCAST_ADDRESS=_ArrayToString($bc,".",1)
MsgBox(0 , "" , $BROADCAST_ADDRESS)

Func lif($a,$B)
    If $a=0 Then Return $b
    If $a<>255 Then Return BitOR($a,$B)
    Return $a
EndFunc

edit: added lif instead of just bitor

Edited by Xand3r

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

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