madmorgan Posted December 15, 2009 Posted December 15, 2009 (edited) hello all, im trying to build a very simple chat program were to users can use it of course. im still a binggner but im trying my best with this program and enjoy it. But any way downto the program. you can run my code to see what im doing, the program has a GUI with a inputbox for the ipaddress to send to. and a edit box for the message but i also want the same program to recive messages as well in a sepreate input box on the same program, i know there will be a problem with the TCP Port numbers any way how can make lision on all ports as well so i dont have to give it a fixed port??? please i need help with this. here is my code. expandcollapse popup#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Global $Form1_1 = GUICreate("", 475, 256) Global $Edit1 = GUICtrlCreateEdit("", 8, 112, 185, 89) GUICtrlSetData(-1, "") Global $IPAddress = GUICtrlCreateInput("", 8, 56, 121, 21) Global $Label1 = GUICtrlCreateLabel("Enter The IP Address Of", 8, 16, 123, 17) Global $Send = GUICtrlCreateButton("Send", 8, 216, 75, 25, $WS_GROUP) Global $Exit = GUICtrlCreateButton("Exit", 120, 216, 75, 25, $WS_GROUP) Global $Label2 = GUICtrlCreateLabel("E.G 192.168.1.65", 144, 64, 90, 17) Global $Label3 = GUICtrlCreateLabel("You Message...", 8, 88, 78, 17) Global $Label5 = GUICtrlCreateLabel("The Person You Want To Chat To.", 8, 32, 171, 17) Global $edit2 = GUICtrlCreateEdit("", 272, 32, 185, 89) GUICtrlSetData(-1, "edit2") Global $Label4 = GUICtrlCreateLabel("I Want This Edit Dox To Show The", 280, 128, 170, 17) Global $Label6 = GUICtrlCreateLabel("Recived Messages", 312, 152, 95, 17) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Exit Exit Case $Send ; want this to keep sending new messages when the user enters them. TCPStartup() $TCPCONNECT = TCPConnect(GUICtrlRead ($IPAddress), 403) if $TCPCONNECT = -1 Then Exit TCPSend($TCPCONNECT, GUICtrlRead ($Edit1)) TCPShutdown() EndSwitch WEnd ; i also want this script to recive messages as well so here is my ; recive code. and this is a sparet bit of code ive been testing it with to make sure im sending data. but i would like to have this in the same program as well. so it recives messages as well. BY THE WAY YOU NEED TO HAVE THIS RUNNING FIRST TO SEE IF DATA IS BEEN SENT. TCPStartup() $TCPLISTEN = TCPListen(@IPAddress1, 403) Do $TCPACCEPT = TCPAccept($TCPLISTEN) Until $TCPACCEPT <> -1 Do $TCPRECV = TCPRecv($TCPACCEPT, 1000000) Until $TCPRECV <> "" MsgBox(0,"Incomming Data", $TCPRECV) Thanks Edited December 15, 2009 by madmorgan
ichigo325 Posted December 15, 2009 Posted December 15, 2009 (edited) Hi, i have create this script based on AutoIt Help. You should try to explore and read the AutoIt help file, there are many things in it that should solve your problems.. This is just a simple script, but i added some functions such as SoundPlay and WinFlash when you received messages.. expandcollapse popup;AutoIt Messanger by Ichigo325 #include <GUIConstantsEx.au3> #include <GuiEdit.au3> Opt('MustDeclareVars', 1) Example() Func Example() Local $szIPADDRESS = @IPAddress1 Local $nPORT = 33891 Local $MainSocket, $Gui, $edit, $ConnectedSocket, $szIP_Accepted, $ConnectedSocket2 Local $msg, $recv, $btn, $input, $input2, $nick TCPStartup() $MainSocket = TCPListen($szIPADDRESS, $nPORT) If $MainSocket = -1 Then Exit $Gui = GUICreate("AutoIt Messenger [" & $szIPADDRESS & "]", 300, 323) GUICtrlCreateLabel("Incoming messages:", 10, 5, 150, 15) $edit = _GUICtrlEdit_Create($Gui, "", 10, 20, 280, 180) GUICtrlSetFont(-1, 9, -1, -1, "Tahoma") GUICtrlCreateLabel("Nickname:", 10, 205, 100, 15) $nick = GUICtrlCreateInput("", 10, 220, 280, 22) GUICtrlCreateLabel("Text to send: ", 10, 245, 100, 15) $input = GUICtrlCreateInput("", 10, 260, 280, 22) GUICtrlSetFont(-1, 9, -1, -1, "Tahoma") GUICtrlSetState(-1, $GUI_FOCUS) $btn = GUICtrlCreateButton("Send...", 10, 290, 100, 25, 0x0001) GUICtrlCreateLabel("IP: ", 175, 295, 12, 25) $input2 = GUICtrlCreateInput("", 190, 290, 100, 25, 0x0001) GUICtrlSetData(-1, $szIPADDRESS) GUICtrlSetFont(-1, 9, -1, -1, "Tahoma") GUISetState() $ConnectedSocket = -1 While 1 $msg = GUIGetMsg() $ConnectedSocket = TCPAccept($MainSocket) If $msg = $GUI_EVENT_CLOSE Then ExitLoop $recv = TCPRecv($ConnectedSocket, 2048) If $recv <> "" Then If _GUICtrlEdit_GetText ($edit) <> "" Then _GUICtrlEdit_SetText($edit, _GUICtrlEdit_GetText($edit) & @CRLF & $recv) Else _GUICtrlEdit_SetText($edit, _GUICtrlEdit_GetText($edit) & $recv) EndIf If Not WinActive($Gui) Then WinFlash($Gui) SoundPlay(@WindowsDir & "\Media\Ding.wav") _GUICtrlEdit_LineScroll ($edit,_GUICtrlEdit_GetLineCount ($edit)+1,_GUICtrlEdit_GetLineCount ($edit)+1) EndIf If $msg = $btn Then If GUICtrlRead($nick) = "" Or GUICtrlRead($input) = "" Then MsgBox(48, "Error", "Please type something in ""Nickname"" and ""Text to send""!") ContinueLoop EndIf $szIPADDRESS = GUICtrlRead($input2) If StringRegExp($szIPADDRESS, "\d.\d.\d.\d") Then $ConnectedSocket2 = TCPConnect($szIPADDRESS, $nPORT) If $ConnectedSocket2 <> -1 Then TCPSend($ConnectedSocket2, GUICtrlRead($nick) & ": " & GUICtrlRead($input)) If _GUICtrlEdit_GetText ($edit) <> "" Then _GUICtrlEdit_SetText($edit, _GUICtrlEdit_GetText($edit) & @CRLF & GUICtrlRead($nick) & ": " & GUICtrlRead($input)) Else _GUICtrlEdit_SetText($edit, _GUICtrlEdit_GetText($edit) & GUICtrlRead($nick) & ": " & GUICtrlRead($input)) EndIf GUICtrlSetData($input, "") _GUICtrlEdit_LineScroll ($edit,_GUICtrlEdit_GetLineCount ($edit)+1,_GUICtrlEdit_GetLineCount ($edit)+1) GUICtrlSetState($input, $GUI_FOCUS) Else MsgBox(0, "Error!", "Couldn't connect " & $szIPADDRESS & "!" & @CRLF & "Make sure IP is exist!") GUICtrlSetState($input2, $GUI_FOCUS) EndIf EndIf EndIf WEnd If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket) TCPShutdown() Exit EndFunc ;==>Example Tested, works with Windows 7 and XP.. Hope this script help you.. Edited December 15, 2009 by ichigo325 [size="2"][font="Lucida Sans Unicode"][b][/b][/font][/size]
madmorgan Posted December 15, 2009 Author Posted December 15, 2009 Hello ICHIGO 325, thanks for your code and it works grate but i would like some help to change my code so then i know were i have gone wrong and how to do it better next time. this is the thurd program i have made with auto it and i want to learn from my errors so if you could use my code and show me what things to change or add and how to add them i would be grate fll. oooo i hvae use the help file by im still puzzeld on how to add the tcp send and recv functions in to one script. thanks
ichigo325 Posted December 16, 2009 Posted December 16, 2009 (edited) Hi, madmorgan.. For your information, i have tried your code. But things changed, i already mess up the script and now its unusable.. I don't know what is wrong with your code but when i write my own code, its working! I have no idea about your script.. You can compare my code with yours, maybe you can find something.. Good luck! Edited December 16, 2009 by ichigo325 [size="2"][font="Lucida Sans Unicode"][b][/b][/font][/size]
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