Jump to content

Loop help


Recommended Posts

Hi all

i have been working on this script that will Connect to a irc server my problem is that i have to keep clicking the send button to get it to TCPRecv it's a loop problem but i have no clue how to fix it if you can help i would be very thankfull been working on this script for couple weeks on and off and i can not fig it out

thank you

Michael

#include <GuiConstants.au3>
dim $socket 
$titl=GuiCreate("MyGUI", 548, 410,(@DesktopWidth-548)/2, (@DesktopHeight-410)/2)

$Input_1 = GuiCtrlCreateInput("69.31.71.61", 60, 10, 140, 20)
$Label_2 = GuiCtrlCreateLabel("Server", 10, 10, 40, 20)
$Label_3 = GuiCtrlCreateLabel("Port", 210, 10, 30, 20)
$Input_4 = GuiCtrlCreateInput("6667", 260, 10, 70, 20)
$Label_5 = GuiCtrlCreateLabel("Name", 10, 40, 40, 20)
$Input_6 = GuiCtrlCreateInput("name", 60, 40, 120, 20)
$Label_7 = GuiCtrlCreateLabel("Nick", 210, 40, 30, 20)
$Input_8 = GuiCtrlCreateInput("Nick", 260, 40, 120, 20)
$connect= GuiCtrlCreateButton("Connect", 410, 30, 100, 20)
$Edit_10 = GuiCtrlCreateEdit("", 0, 70, 550, 320)
$Input_11 = GuiCtrlCreateInput("Input11", 0, 390, 450, 20)
$Button_12 = GuiCtrlCreateButton("Send", 450, 390, 70, 20)
GuiSetState()
While 1
    $msg = GuiGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    If $msg = $connect Then
       $g_IP = GUICtrlRead ($Input_1  )
      TCPStartUp()
$socket = TCPConnect( $g_IP, GUICtrlRead ($Input_4))
endif
If $socket = 1 Then Exit
 WinSetTitle($titl,"","connected")

$recv = TCPRecv($socket, 512 )
    If $recv  <> "" Then
        GUICtrlSetData($Edit_10 , GUICtrlRead($Edit_10 ) & $recv  & @CRLF)
    EndIf

If $msg = $Button_12  Then
TCPSend( $socket, GUICtrlRead($Input_11 ))
endif
WEnd
Link to comment
Share on other sites

Uh... Your TCPRecv() really shouldn't be in that spot -- It's going to be called every time the GUI message processing loop loops. If you call TCPRecv() when no data is expected to be sent then your script will hang while it waits for that non-existent data to come through.

How do you know when the TCP transmission is complete? You need to program this logic into your script as well. It depends on what you're communicating with and how it signifies the end of a transmission.

Link to comment
Share on other sites

Uh... Your TCPRecv() really shouldn't be in that spot -- It's going to be called every time the GUI message processing loop loops. If you call TCPRecv() when no data is expected to be sent then your script will hang while it waits for that non-existent data to come through.

How do you know when the TCP transmission is complete? You need to program this logic into your script as well. It depends on what you're communicating with and how it signifies the end of a transmission.

hmmmm i don't when the TCP transmission is complete well when i connect to the irc server

i get some transmission data then iwould have to click send to get the rest of the data from the server i'm not sure where to put the TCPRecv() i have try'd a few places no luck where would you put it ?

thank you

Michael

Link to comment
Share on other sites

Something like this should get you started -- basically the idea is to keep the GUI processing loop to the bare essentials. Any code to be fired when an event occurs should go into a separate function:

While 1

$Msg = GuiGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $Connect Then Connect($Edit_10)
    If $msg = $Button_12 Then TCPSend( $socket, GUICtrlRead($Input_11 ))

WEnd

Func Connect($Edit_10)

    $g_IP = GUICtrlRead ($Input_1  )
    TCPStartUp()
    $socket = TCPConnect( $g_IP, GUICtrlRead ($Input_4))
    If $socket = 1 Then Exit
    WinSetTitle($titl,"","connected")
    $recv = TCPRecv($socket, 512 )
    If $recv  <> "" Then GUICtrlSetData($Edit_10 , GUICtrlRead($Edit_10 ) & $recv  & @CRLF)

EndFunc

Here's the thing with TCP transmissions: whatever you're communicating with will be following some sort of protocol. The protocol defines how a server will indicate where its transmission ends. It may be a certain character in the stream or perhaps it will start of by sending a number to indicate how many more characters to read from the stream. The way this is communicated varies greatly and depends on who designs the protocol. (For an assignment I had to write a client/server in Java. The protocol I designed used '---@@@---' to indicate an end of transmission.)

If you want to communicate with an IRC server then you'll need to know this kind of information. It's not a trivial task but it's by no means too tricky. There's bound to be documents out there on the internet that will tell you all the gory details of successfully communicating with an IRC server via raw TCP.

Good luck.

Edit: Code fixed to pass text field handle to Connect().

Edited by LxP
Link to comment
Share on other sites

thank you this will help big time i see how you did that you are calling

Func outside the loop i understand what you are saying about bare essentials

i can not seem to get data in the edit now but i will fig this out

thank you so much

michael

Link to comment
Share on other sites

Hi All

still working on this script to connect to irc i took a brake from it but back on it

LxP gave me nice tips on how to do this .

i know how to connect i just can't seem to do it in autoit i think i'm doing it all Wrong my problem is the tcp send

i need to send the nick command nick something then the user command USER Guest 0 0 : michael

but i have to wait till the recv so after the server looks up my host and stuff then i need to send the nick and user command and not sure how to do that any idea on this ?

thank you michael

#include <GuiConstants.au3>


dim $Button_12 , $socket,$recv,$err

$titl=GuiCreate("MyGUI", 548, 410,(@DesktopWidth-548)/2, (@DesktopHeight-410)/2)

$Input_1 = GuiCtrlCreateInput("64.125.158.24", 60, 10, 140, 20)
$Label_2 = GuiCtrlCreateLabel("Server", 10, 10, 40, 20)
$Label_3 = GuiCtrlCreateLabel("Port", 210, 10, 30, 20)
$Input_4 = GuiCtrlCreateInput("6667", 260, 10, 70, 20)
$Label_5 = GuiCtrlCreateLabel("Name", 10, 40, 40, 20)
$Input_6 = GuiCtrlCreateInput("name", 60, 40, 120, 20)
$Label_7 = GuiCtrlCreateLabel("Nick", 210, 40, 30, 20)
$Input_8 = GuiCtrlCreateInput("Nick", 260, 40, 120, 20)
$connect= GuiCtrlCreateButton("Connect", 410, 30, 100, 20)
$Edit_10 = GuiCtrlCreateEdit("", 0, 70, 550, 320)
$Input_11 = GuiCtrlCreateInput("Input11", 0, 390, 450, 20)
$Button_12 = GuiCtrlCreateButton("Send", 450, 390, 70, 20)
GuiSetState()

While 1
$Msg = GuiGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $Connect Then Connect($Edit_10)
;--------------------------------------------------------

 $recv = TCPRecv($socket, 512 )
If $msg = $Button_12 Then TCPSend( $socket, GUICtrlRead($Input_11 ))
If $recv <> "" Then recv ($Edit_10)
If $recv <>"" Then sen($Input_8)
WEnd

;----------------------FUNc- connect-----------------------------------------
Func Connect($Edit_10)
    $g_IP = GUICtrlRead ($Input_1  )
    TCPStartUp()
    $socket = TCPConnect( $g_IP, GUICtrlRead ($Input_4))
    If $socket = 1 Then Exit
    WinSetTitle($titl,"","connected")
EndFunc
;----func recv--------------------------------
Func recv ($Edit_10)
 GUICtrlSetData($Edit_10, GUICtrlRead($Edit_10) & ">" & $recv & @CRLF)
EndFunc
;---------------------------------------------
Func sen($Input_8)
TCPSend( $socket, GUICtrlRead($Input_8 ))
EndFunc
Link to comment
Share on other sites

It's important that you don't have the TCPRecv() calls happening in your loop. Basically a TCPRecv() call translates to 'pause the script until either I receive X many bytes from the socket, or the socket no longer exists'. So if you leave this call in the loop. at some point your script will hang indefinitely because it will be waiting for some server response when the server has no plans to send one.

Your While..WEnd loop should look more like this:

While 1
    $Msg = GuiGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $Connect Then Connect($Edit_10)
WEnd

and you should find another more suitable location for this code (possibly within your Connect() function?):

$recv = TCPRecv($socket, 512 )
If $msg = $Button_12 Then TCPSend( $socket, GUICtrlRead($Input_11 ))
If $recv <> "" Then recv ($Edit_10)
If $recv <> "" Then sen($Input_8)

I know enough about TCP communication to get by, but I know nothing about the ins and outs of the IRC protocol so I'm afraid I can't help you much further.

Link to comment
Share on other sites

ok thank you guys i will fig it out

i think the hole prob is i can't fig out where to put the tcp send to send the nick and user command to the server .

hey LxP thank you for all your help you show me how to place func in my script and that help me out big time .

Michael

Link to comment
Share on other sites

  • 1 month later...

TCPRecv() does not pause! At least it didn't back when I wrote an all AutoIt SMTP email function.

I put all my TCPRecv()'s into loops (until they get what's expected) or they almost always miss something.

Here's a function I wrote that illustrates the idea of waiting for a response from a server. Once I started using this all my TCP stuff started working.

;
; Does a TCPSend() and waits (default 20 seconds) for a response with TCPRecv()
;
Func _TCPQuery ( $Sckt, $data, $TO = 20000)
    local $rc, $begin
    $rc = TCPSend($Sckt, $data)
    if $rc == 0 then
        SetError(@error)
        return ""
    EndIf
    $begin = TimerInit()
    Do
        Sleep(50)
        $rc = TCPRecv($Sckt, 1024)
        SetError(@error)
    Until ($rc <> "") OR (TimerDiff($begin) > $TO)
    Return $rc
EndFunc
Link to comment
Share on other sites

$password="password"

;original script - IRC client in Autoit v3 beta by Michael Mcstay AKA ( ^sleepy^s-sin scripting) http://sleepys-sin.freeserverhost.net/sleepys__sin.htm 

$chatto="#autoit"
$joinf="#autoit"

#include <GuiConstants.au3> 
#Include <GuiEdit.au3>

dim $sendb , $socket,$recv,$err, $g_IP 

$regplace="HKEY_LOCAL_MACHINE\SOFTWARE\Adam1213 Software\IRC"

; ------------- Nick ---------------------
$nick = RegRead($regplace, "1 Nick")
if "t"&$nick=="t" then 
$nick=@UserName & Random (1 , 99,1)
RegWrite($regplace, "1 Nick", "REG_SZ", $nick)
endif
; ------------- Name ---------------------
$name = RegRead($regplace, "2 Name")
if "t"&$name=="t" then 
$name=@UserName & Random (1 , 99,1)
RegWrite($regplace, "2 Name", "REG_SZ", $name)
endif
; ------------- server ---------------------
$server = RegRead($regplace, "3 Server")
if "t"&$server=="t" then
$server="irc.freenode.net"
RegWrite($regplace, "3 Server", "REG_SZ", $server)
endif
; ------------- Port ---------------------
$port = RegRead($regplace, "4 Port")
if "t"&$port=="t" then 
$port="6667"
RegWrite($regplace, "4 Port", "REG_SZ", $port)
endif

; ------------------------- GUI ---------------------------------
$titl=GuiCreate("MyGUI", 548, 410,(@DesktopWidth-548)/2, (@DesktopHeight-410)/2 ) 

$vserver = GuiCtrlCreateInput($server, 60, 10, 140, 20) ; Input server
$Label_2 = GuiCtrlCreateLabel("Server", 10, 10, 40, 20) ;  Display server
$vPort   = GuiCtrlCreateLabel("Port", 210, 10, 30, 20)   ;  Display Port
$vPorte  = GuiCtrlCreateInput($port, 260, 10, 70, 20)     ; Input port
$vName   = GuiCtrlCreateLabel("Name", 10, 40, 40, 20)     ;  Display Name
$vnamei  = GuiCtrlCreateInput($name, 60, 40, 120, 20,)   ; Input Name
$vnick1  = GuiCtrlCreateLabel("Nick", 210, 40, 30, 20)   ;  Display nick
$vnick1e = GuiCtrlCreateInput($nick, 260, 40, 120, 20)   ; Input nick
$connect = GuiCtrlCreateButton("Connect", 410, 30, 100, 20);   Button connect
$Edit_10 = GuiCtrlCreateEdit("", 0, 70, 550, 320)         ; Edit chat
$chat12  = GuiCtrlCreateInput("", 0, 390, 450, 20)       ; Input send
$sendb   = GuiCtrlCreateButton("Send", 450, 390, 70, 20)   ;   Button send
GuiSetState() 

GUICtrlSetState ($connect, $GUI_FOCUS)


While 1 
_GUICtrlEditLineScroll($Edit_10,0, 1000)
$Msg = GuiGetMsg() 
If $Msg = $GUI_EVENT_CLOSE Then ExitLoop 
;------------button connect--func call--------------------------- 
If $msg = $Connect Then Connect($Edit_10) 
;--------------the socket Recv ------------------------------ 
$recv = TCPRecv($socket, 512 ) 
If $msg = $sendb Then call ("onsend")
;------------------call socket recv func------------------------- 
If $recv <> "" Then recv ($Edit_10) 
;--------------end socket Recv f---------------------------------- 
WEnd 

;---------------------- Connect ------------------------------ 
Func Connect($Edit_10) 
$g_IP = $g_IP 
TCPStartUp() 
GUICtrlSetState(-1,$GUI_FOCUS) 
;----name to number isp---au3 can only use number isp--------------------- 
$g_IP=TCPNameToIP(GUICtrlRead($vserver)) 
$socket = TCPConnect( $g_IP, GUICtrlRead ($vPorte)) 
If $socket = 1 Then Exit 
;--------winsettitle sends connect to the gui title-when connected------------------------------ 
WinSetTitle($titl,"","connected") 
;------------tcp send the user and nick commands on conenct---------------- 
$nick=GUICtrlRead($vnick1e)
TCPSend( $socket,"NICK "&$nick&@LF) 
$name=GUICtrlRead($vnamei)
TCPSend( $socket,"USER Guest 0 0 :"& $name& @LF) 
sleep(1000)
TCPSend( $socket,"privmsg nickserv identify " & $password &@LF)
TCPSend( $socket,"join "& $joinf&@LF)
EndFunc 
;----func recv-------------------------------- 
Func recv ($Edit_10) 
GUICtrlSetData($Edit_10, GUICtrlRead($Edit_10) & ">" & $recv & @CRLF) 
EndFunc 

; -------------- On send --------------------

func onsend()

$monly="yes"
$text=GUICtrlRead($chat12 )
if Stringleft ($text,2)=="//"  then
$test=stringmid($text,2)
$monly="no"
endif

if Stringleft ($text,1)=="/"  then
call ("commandlist")
endif

if $monly=="yes" then $text="privmsg " & $chatto & " :" & $text

$monly="no"

TCPSend( $socket, $text &@LF) 
GUICtrlSetData($Edit_10, GUICtrlRead($Edit_10) & ">>>" & $text & @CRLF) 
endfunc

; ------------- Commands -------------------------------
 func commandlist() 

if Stringleft ($text, 6)=="/nick" then
$text="privmsg nickserv :nick " & stringmid ($text,6)
endif
;/me
;/whois
;/part
endfunc


; ------------- Save info ---------------------
$nick=GUICtrlRead($vnick1e)
$name=GUICtrlRead($vnamei)
$server=GUICtrlRead($vserver)
$port=GUICtrlRead ($vPorte)
RegWrite($regplace, "1 Nick", "REG_SZ", $nick)
RegWrite($regplace, "2 Name", "REG_SZ", $name)
RegWrite($regplace, "3 Server", "REG_SZ", $server)
RegWrite($regplace, "4 Port", "REG_SZ", $port)

I found a program like this one taht worked and edited it a lot. I am still working on it

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...