Sign in to follow this  
Followers 0
Dirk Diggler

TCPxxxx

8 posts in this topic

Sorry for studip question, but -- functions like TCPRecv -- does it waits for receive any data, or it checks for incoming data and instantly finish their work, so need to loop it?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Sorry for studip question, but -- functions like TCPRecv -- does it waits for receive any data, or it checks for incoming data and instantly finish their work, so need to loop it?

You loop it and check for received data with if <> "" so basically if there is data there then you react to it..

AutoIT Smiths server code.. Using this you can write an AutoIT command (Use single quotes ' not double) and it executes on the server machine

Opt("RunErrorsFatal", 0)
Global Const $Port = 8000
Global $MaxConc = 100
Global $MainSocket = TCPStartServer($Port, $MaxConc)
If @error <> 0 Then Exit MsgBox(16, "Error", "Server unable to initialize.")
Global Const $MaxLength = 512
Global $ConnectedSocket[$MaxConc]
Global $CurrentSocket = 0
Local $Track = 0
Global Const $MaxConnection = ($MaxConc - 1)
For $Track = 0 To $MaxConnection Step 1
    $ConnectedSocket[$Track] = -1
Next
While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($MainSocket)
    If $ConnectedSocket[$CurrentSocket] <> - 1 Then
        $CurrentSocket = SocketSearch()
    EndIf
    $Track = 0
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$Track] <> - 1 Then
            $Data = TCPRecv($ConnectedSocket[$Track], $MaxLength)
            If $Data = "~bye" Then
                TCPCloseSocket($ConnectedSocket[$Track])
                $ConnectedSocket[$Track] = -1
                $CurrentSocket = SocketSearch()
            ElseIf $Data <> "" Then
                AutoItExecute($Data)
                TCPSendMessageAll($MaxConnection, $Data & " operated.")
            EndIf
        EndIf
    Next
WEnd
Func AutoItExecute($Cmd)
    RunWait(@AutoItExe & ' /AutoIt3ExecuteLine "' & $Cmd & '"')
EndFunc
Func SocketSearch()
    Local $Track = 0
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$Track] = -1 Then
            Return $Track
        Else
      ; Socket In Use
        EndIf
    Next
EndFunc;==>SocketSearch
Func TCPSendMessageAll($ConnectionLimit, $Data)
    Local $Track = 0
    For $Track = 0 To $ConnectionLimit Step 1
        If $ConnectedSocket[$Track] <> - 1 Then TCPSend($ConnectedSocket[$Track], $Data)
    Next
EndFunc;==>TCPSendMessageAll
Func TCPStartServer($Port, $MaxConnect = 1)
    Local $Socket
    $Socket = TCPStartup()
    Select
        Case $Socket = 0
            SetError(@error)
            Return -1
    EndSelect
    $Socket = TCPListen(@IPAddress1, $Port, $MaxConnect)
    Select
        Case $Socket = -1
            SetError(@error)
            Return 0
    EndSelect
    SetError(0)
    Return $Socket
EndFunc;==>TCPStartServer

and Client Code

#include <GuiConstants.au3>
#include <GuiEdit.au3>
#include <Misc.au3>
Local $Server = InputBox("Server", "Please input the server you would like to connect to:")
Local $Port = InputBox("Server Port", "Please input the port or the server you would like to connect to:")
TCPStartup()
$MainSocket = TCPConnect(TCPNameToIP($Server), $Port)
If $MainSocket = -1 Then Exit MsgBox(0, "Error", "Could Not Connect or Bad Connection")
GUICreate("Server Client", 390, 210)
$Send = GUICtrlCreateEdit("", 10, 10, 175, 150, $WS_VSCROLL)
Local $History = GUICtrlCreateEdit("Server Messages:", 200, 10, 175, 150, BitOR($WS_VSCROLL, $ES_READONLY))
$SButton = GUICtrlCreateButton("Send", 145, 165, 100, 35)
GUISetState()
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        Exit
    ElseIf $msg = $SButton Or _IsPressed ("0D") = 1 Then
        $Text = GUICtrlRead($Send)
        $TCPSent = TCPSend($MainSocket, $Text)
        GUICtrlSetData($Send, "")
    EndIf
    $Recv = TCPRecv($MainSocket, 512)
    If $Recv <> "" Then
        GUICtrlSetData($History, GUICtrlRead($History) & @CRLF & $Recv)
        _GUICtrlEditLineScroll ($History, 0, _GUICtrlEditGetLineCount ($History))
    EndIf
WEnd
Func OnAutoItExit()
    TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc;==>OnAutoItExit
Edited by ChrisL

Share this post


Link to post
Share on other sites

what will happen if looped code very slow? i mean

While 1
  $Recv = TCPRecv($MainSocket, 512)

  #region VERY BIG AND SLOW CODE
....
  #endregion
WEnd

Will it miss data? Are incoming data storing in some buffer when other code is running? How long VERY BIG AND SLOW CODE can run before TCPRecv() again checks for receiving data(without missing it)?

Share this post


Link to post
Share on other sites

what will happen if looped code very slow? i mean

While 1
  $Recv = TCPRecv($MainSocket, 512)

  #region VERY BIG AND SLOW CODE
....
  #endregion
WEnd

Will it miss data? Are incoming data storing in some buffer when other code is running? How long VERY BIG AND SLOW CODE can run before TCPRecv() again checks for receiving data(without missing it)?

Might want to look at AdlibEnable() If your wanting to run a TCPRecv() function, and you need the data up to date often, and your code is "VERY BIG AND SLOW".

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

i have no problems yet with TCP-functionality, i'm just trying to understand how it works.

so, if code slow, f.e. 1 second per cycle, and i don't use AdlibEnable, will some data be lost or not? 10 seconds? 100?

What happens with incoming data when other code is executing?

Edited by Dirk Diggler

Share this post


Link to post
Share on other sites

i have no problems yet with TCP-functionality, i'm just trying to understand how it works.

so, if code slow, f.e. 1 second per cycle, and i don't use AdlibEnable, will some data be lost or not? 10 seconds? 100?

What happens with incoming data when other code is executing?

Try it, but I don't think it looses it.

I did an application where once I got data I opened up a message box, if I left that message box up and more data came in it still picked it up on the next loop as long as it wasn't bigger than the max data length

Share this post


Link to post
Share on other sites

Incoming data appears to be queued somewhere until it is TCPRecv'd. I just tried using TCPRecv in a loop with a five-second delay, and quickly sent it four separate blocks of data that were close to the maximum length. Each run through the loop, it picked up the next data block in chronological order. You'd have to set up a more extensive test to determine just how long data can be "in limbo" before it gets tossed, but it's definitely more than 15 seconds.

Share this post


Link to post
Share on other sites

Check my signature for more examples and quick tests. It would be best to receive the data using a global variable and checking for it once every 100 miliseconds, but the information cannot stay in limbo forever.

- AutoIt Smith

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