JavaScript_Freek Posted February 5, 2009 Share Posted February 5, 2009 (edited) Simple LAN ChatHello everyone, recently, I embarked upon a cool task. Creating a SIMPLE LAN Chat. Users launch this application and chat with others who are on the same LAN.This is very basic and can be easy to understand. I have made several comments within this chat so you know whats happening.One thing I hope to add to this is ONLINE USERS.You, yourself, can add to this as well. This is more of a learning tool to see how LAN chats work. One last thing, pressing ENTER also sends messages.Enjoy. (PS, I may have left some not needed includes. )expandcollapse popup; ********************* ; SIMPLE LAN Chat ; By Javascript_Freek ; version 1.4b ; ********************* #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, $usersonline, $lanchat_gui, $username ; CREATE THE GUI $lanchat_gui = GUICreate("LAN CHAT v1.4b", 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, 365, 257, $LVS_LIST, $WS_EX_CLIENTEDGE) _GUICtrlListView_SetColumnWidth($chatarea, 0, 100) _GUICtrlListView_SetExtendedListViewStyle($chatarea, $LVS_EX_FULLROWSELECT) _GUICtrlListView_SetView($chatarea, 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 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)) Endif GUICtrlSetData($input, "") _GUICtrlListView_Scroll($chatarea, 0, 150) EndFunc ; QUITTING PROGRAM FUNCTION Func quit() 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 Edited February 5, 2009 by JavaScript_Freek xixautoeat 1 [center]Cookyx.com :: Simple LAN Chat[/center] Link to comment Share on other sites More sharing options...
ghetek Posted February 6, 2009 Share Posted February 6, 2009 Oh man im loving the TCP in the back on this. Perhaps the results should be displayed in a single column? When the script is running it sucks in every single {enter} command. in fact thats why this post is entirely in one line! ... every time i press {enter} i get a dialogue box that says you did not send anything". the funniest thing happens when i respond to that dialogue box by pressing... {enter} Link to comment Share on other sites More sharing options...
ghetek Posted February 6, 2009 Share Posted February 6, 2009 commented out this: ; Msgbox(64, "Error", "You did not send anything.") one more thing that i cant figure out. when i press enter and the gui has focus, i send nothing. when i click on send it works perfectly Link to comment Share on other sites More sharing options...
JavaScript_Freek Posted February 6, 2009 Author Share Posted February 6, 2009 (edited) Yeah that's still a bug I am working. Just comment the "hotkey" for {ENTER}. Trying to find a way around it. One, thing I am trying to find out how to do is online users. That will be a challenge. Edited February 6, 2009 by JavaScript_Freek [center]Cookyx.com :: Simple LAN Chat[/center] Link to comment Share on other sites More sharing options...
Prab Posted February 7, 2009 Share Posted February 7, 2009 (edited) A work around I have used in the past for your "enter" bug is to wrap everything inside the Func sendmsg() with: If WinActive(THE GUI) YOUR CODE Else HotKeySet("{ENTER}");temp disable hotkey Send("{ENTER}") HotKeySet("{ENTER}", "sendmsg");re-enable hotkey EndIf Warning: Code untested, just use the concept. Edit: Clarified where to put the code. Edited February 7, 2009 by Prab FolderLog GuiSpeech Assist Link to comment Share on other sites More sharing options...
BrettF Posted February 7, 2009 Share Posted February 7, 2009 GUISetAccelerators should do the trick... Cheers, Brett Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
JavaScript_Freek Posted February 7, 2009 Author Share Posted February 7, 2009 Ok thanks, so... how is the code? Cool? Needs improving? Etc? [center]Cookyx.com :: Simple LAN Chat[/center] Link to comment Share on other sites More sharing options...
RichardS Posted February 8, 2009 Share Posted February 8, 2009 Very nice script. I am learning AutoIT and I was thinking about the same thing. Your script will surely be useful. Link to comment Share on other sites More sharing options...
ghetek Posted February 8, 2009 Share Posted February 8, 2009 hey can somebody give me some input on my array cleanup? update() doesnt seem to update the $users array. I grabbed that function from another thread. expandcollapse popup#include <Constants.au3> #Include <Array.au3> #Include <Timers.au3> #NoTrayIcon Opt("TrayOnEventMode", 1) Opt("TrayMenuMode", 1) Global $sockserver, $sockclient, $recv, $users[2], $time $codemsg = "#01" $codeupdate = "#02" $updateinterval = 5000 $port = 65335 $iplocal = @IPAddress1 $users[0] = 1 $users[1] = $iplocal $aDivIP = StringSplit($iplocal, ".") $ipremote = $aDivIP[1] & "." & $aDivIP[2] & "." & $aDivIP[3] & "." & "255" $sendmsg = TrayCreateItem("Broadcast") TrayItemSetOnEvent(-1, "sendmsg") $display = TrayCreateItem("Display Hosts") TrayItemSetOnEvent(-1, "display") $quit = TrayCreateItem("Quit") TrayItemSetOnEvent(-1, "quit") TraySetState() UDPStartup() $sockserver = UDPBind($iplocal, $port) Sleep(1000) $sockclient = UDPOpen($ipremote, $port) $time = TimerInit() While 1 $recv=UDPRecv($sockserver, 50) Select Case $recv <> "" $code=StringLeft ($recv,3) $data=StringTrimLeft($recv, 3) Select Case $code = $codemsg; message recieved show message box MsgBox(0,"",$data,3) Case $code = $codeupdate Not $iplocal; update revieved, add to and sort in array update($users, $data) EndSelect EndSelect Switch _Timer_Diff($time) Case $updateinterval UDPSend($sockclient,$codeupdate & $iplocal) EndSwitch WEnd Func sendmsg() UDPSend($sockclient,$codemsg & InputBox("What to broadcast?", "text:", "")) EndFunc ;==>sendmsg Func update($str,$newuser) Local $count, $final = "", $split = StringSplit($str, ",") For $X = 1 To UBound($split) - 1 $count = "" For $i = $X To UBound($split) - 1 If StringInStr($final, $split[$i]) Then ContinueLoop If StringInStr($split[$X], $split[$i]) Then $count += 1 Next If $count >= 1 Then $final &= ",[" & $count & "] " & $split[$X] Next $updatedarray = _ArraySort (StringTrimLeft($final, 1)) $updatedarray[0] = UBound ($updatedarray) Return _ArraySort (StringTrimLeft($updatedarray, 1)) EndFunc Func display() _ArrayDisplay($users) EndFunc Func quit() UDPCloseSocket($sockserver);works ok? UDPShutdown() Exit EndFunc ;==>quit Link to comment Share on other sites More sharing options...
JavaScript_Freek Posted April 4, 2009 Author Share Posted April 4, 2009 Sorry, I wouldn't know. [center]Cookyx.com :: Simple LAN Chat[/center] Link to comment Share on other sites More sharing options...
Nezzo Posted May 7, 2009 Share Posted May 7, 2009 Hello. Indeed, very nice chat! I'm currently implementing some of my own stuff, that I plan to share with you guys in here when it's ready! But, I would like to change the way the gui reacts to messages - it should do so that a new message appears on a new line - how do I script that? Nezzo. Link to comment Share on other sites More sharing options...
CodyBarrett Posted May 8, 2009 Share Posted May 8, 2009 nice script! im working on a LAN of my own.. but not sure how lan operates... ima sift thru your code! [size="1"][font="Tahoma"][COMPLETED]-----[FAILED]-----[ONGOING]VolumeControl|Binary Converter|CPU Usage| Mouse Wrap |WinHide|Word Scrammbler|LOCKER|SCREEN FREEZE|Decisions Decisions|Version UDF|Recast Desktop Mask|TCP Multiclient EXAMPLE|BTCP|LANCR|UDP serverless|AIOCR|OECR|Recast Messenger|AU3C|Tik-Tak-Toe|Snakes & Ladders|BattleShips|TRON|SNAKE_____________________[u]I love the Helpfile it is my best friend.[/u][/font][/size] Link to comment Share on other sites More sharing options...
Drakthor Posted June 18, 2009 Share Posted June 18, 2009 Ran into a little "oopsie" if you will. Copied script and ran from two different places (one my documents and one the desktop). This was the result (see attachment) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now