Jump to content

while loop question


Recommended Posts

 

Hi all

the program is only doing the fist loop how can i make it do both of them ?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 437, 192, 124)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd



While 1
$TCPListen = TCPListen(@IPAddress1, 403)
Do
$TCPAccept = TCPAccept ($TCPListen)
Until $TCPAccept <> -1

Do
$TCPRecive = TCPRecv ($TCPAccept, 1000000)
Until $TCPRecive <> ""
MsgBox(0, "hi", "recived")

WEnd
Edited by alexander95

Link to comment
Share on other sites

i don't see a tcp startup, neither a tcp connect, and you need a second script that communicates (sends) if you want to receive. it is a complex procedure, your loops are correct, but you missed most of the other important things. please do a forum search for exact information.

E.

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

from helpfile TCPRecv:

.

#include <GUIConstantsEx.au3>

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
    ; Set Some reusable info
    ; Set your Public IP address (@IPAddress1) here.
    ;   Local $szServerPC = @ComputerName
    ;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891
    Local $MainSocket, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv

    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Create a Listening "SOCKET".
    ;   Using your IP Address and Port 33891.
    ;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

    ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit


    ; Create a GUI for messages
    ;==============================================
    GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200, 100, 100)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
    GUISetState()


    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1


    ;Wait for and Accept a connection
    ;==============================================
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1


    ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

    ; GUI Message Loop
    ;==============================================
    While 1
        $msg = GUIGetMsg()

        ; GUI Closed
        ;--------------------
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop

        ; Try to receive (up to) 2048 bytes
        ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 2048)

        ; If the receive failed with @error then the socket has disconnected
        ;----------------------------------------------------------------
        If @error Then ExitLoop

        ; convert from UTF-8 to AutoIt native UTF-16
        $recv = BinaryToString($recv, 4)

        ; Update the edit control with what we have received
        ;----------------------------------------------------------------
        If $recv <> "" Then GUICtrlSetData($edit, _
                $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
    WEnd


    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc   ;==>Example

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP

.

from helpfile TCPSend:

.

;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Set Some reusable info
    ;--------------------------
    Local $ConnectedSocket, $szData
    ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
    ;   Local $szServerPC = @ComputerName
    ;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891

    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1

    ;Attempt to connect to SERVER at its IP and PORT 33891
    ;=======================================================
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

    ; If there is an error... show it
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
        ; If there is no error loop an inputbox for data
        ;   to send to the SERVER.
    Else
        ;Loop forever asking for data to send to the SERVER
        While 1
            ; InputBox for data to transmit
            $szData = InputBox("Data for Server", @LF & @LF & "Enter data to transmit to the SERVER:")

            ; If they cancel the InputBox or leave it blank we exit our forever loop
            If @error Or $szData = "" Then ExitLoop

            ; We should have data in $szData... lets attempt to send it through our connected socket.
            ; convert AutoIt native UTF-16 to UTF-8
            TCPSend($ConnectedSocket, StringToBinary($szData, 4))

            ; If the send failed with @error then the socket has disconnected
            ;----------------------------------------------------------------
            If @error Then ExitLoop
        WEnd
    EndIf
EndFunc   ;==>Example

.

this shows what all is missing in your script

E.

Edit: in short, your second loop will work when the first loop has ended, but it can only end when you do the right coding before

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

You have actually 4 loops in the script you showed since in the second while loop there are 2 do/until loops. Each of those loops could never get the condition to leave the loop so the script might never work (apart from the fact that, as Edano pointed out, there is code missing.)

If you want to test for a number of things then one way is to include all the tests in one loop like this maybe

while 1
  if datadetected then
     do
       readdata
    until noMoreData

  $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
wend

 

Another way is to use a timer or Adlibregister

AdlibRegister("keepChecking")
 
 $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch

wend
 
Func keepChecking()
 
 if test1 then do1thing
 
 if test2 then doOtherthing
 
endfunc
 
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

hello alexander95
 

ok what Edano & martin says, and also

you're trying to send and receive tcp packets from the same script,

this is nonsense, it is as if you are trying to call yourself using the same telephone :blink:.
To send and receive tcp packets on the same computer, you must use at least 2 separate scripts, one that sends and another that receives .
I suggest you read again and try >the example that I have already posted in a previous post.
;) 
bye

Edited by Pincopanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

hello alexander95

 

ok what Edano & martin says, and also

you're trying to send and receive tcp packets from the same script,

this is nonsense, it is as if you are trying to call yourself using the same telephone :blink:.

To send and receive tcp packets on the same computer, you must use at least 2 separate scripts, one that sends and another that receives .

I suggest you read again and try >the example that I have already posted in a previous post. ;) 

bye

.

sorry to contradict ;), but you can of course send and receive in one script. it is a tricky task and confusing work to switch between client and server part, but undoubtedly possible. done that with success before :) it is not nonsense.

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

Ok,Edano, it is probably also technically feasible, but I can not see what useful purpose can serve this ploy :huh2:

.

for a chat program, of course, or a network game. they combine receiving and sending.

i made a very nice network game some time ago, will see if it still works and maybe put it in the example scripts. it is one script that can be copied and executed on two or more network computers and they will communicate (if you wish, but you can also play it standalone).

if i find time ...

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

 . . . . will see if it still works and maybe put it in the example scripts. it is one script that can be copied and executed on two or more network computers and they will communicate (if you wish, but you can also play it standalone).

if i find time ...

 

I'll keep an eye on ...

bye

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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...