Jump to content

TCPRecv() active or passive?


 Share

Recommended Posts

This feel like more of a dev question. If i run this will the computer continually actively check for data to be sent, or does it passively wait for something to come through? just wondering about using in terms of CPU useage/heat because i would really prefer not to burn up our K2 server (or slow it down any more than it already is!)

Server

Do
    $Recv = TCPRecv ($Accept, 1000000)
    FileOpen(@WorkingDir & "\License.txt", 1)
    FileWriteLine(@WorkingDir & "\License.txt")
Until $Recv <> ""

Client

TCPSend($Send, $License & " = " @ComputerName)

Just trying to make a log of who's computer is using what MSOffice so we have a record

Link to comment
Share on other sites

If i run this will the computer continually actively check for data to be sent

yes untill he hits first result, then the script will exit loop

or does it passively wait for something to come through?

No, it loops all the time and circeling to get that one pice of data needed to exit loop, adding some sleep can help to lower cpu usage if needed

just wondering about using in terms of CPU useage/heat because i would really prefer not to burn up our K2 server (or slow it down any more than it already is!)

Your consttantly opening and writing something so move unneded funcs outside loop for reciving or add on them to execute only if $Recv <> '' (btw if $Recv <> '' Then is equivalent to If $Recv Then ) Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

Have you tried it at all to see if there will be an issue with the CPU usage or overheating?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewManNHI have tried it on my laptop (i7-2720) and it cranks 2 of the threads up to max so im just afraid it will burn up our server as its running 2 dual core xeons. Pretty sure it will just burn one up.

@bogQ thanks for the help. My problem with setting sleeps in there, wont i probably miss some data being sent if im sleeping? i know Sleep(10) is only 10 mS, but 1mS check with 10mS sleep is pretty drastic. Will the TCPSend() from the client hold there long enough for the TCPRecv() to pick it up?

Link to comment
Share on other sites

@BrewManNHI have tried it on my laptop (i7-2720) and it cranks 2 of the threads up to max so im just afraid it will burn up our server as its running 2 dual core xeons. Pretty sure it will just burn one up.

If you tried it and saw what the result was, why did you ask if it would do that? You already had the answer, so asking the question, and bumping it, is rather pointless don't you think?

You, and everyone else with questions that can be answered without needing to post the question in the first place, should read the first link in my signature before ever posting another question.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

lets explain like this

when ever something try to send you some data its stored to something that we well call for now

$PortClientVariable

every time new data arives $PortClientVariable goes like this

$PortClientVariable = $PortClientVariable & $new_data

So our $PortClientVariable is allwayes refilling itself with new data with no deliting old data

when ever you call TCPRecv to collect ALL data from $PortClientVariable, $PortClientVariable do this

$PortClientVariable= ''

So it cleans all data and empty it but not before you recive that data from TCPRecv

So if you sleep it doesnt do any diffrance but if you recive 2 messages it do mean that youl need to split them up if you notice that they can mix up

or you can make that client dont send new data untill server respond that he recived something form start of data to end of data (witch is more safer way of dooing it)

Edit:

Small example

Server

#Include <GuiListBox.au3>
#include <GuiListView.au3>
#include <Math.au3>
Opt("TrayIconDebug", 1)
Dim $array[100][2],$f,$recv
TCPStartup()
$MainSocket = TCPListen(@IPAddress1, 33891)
If $MainSocket = -1 Then Exit
$gui = GUICreate("My STATS", 800, 600 ,0 ,0)
GUICtrlCreateTab(10, 5, 780, 100)
GUICtrlCreateTabItem("Connection Monitor")
$hlistview = GUICtrlCreateListView("", 10,50, 780, 540)
_GUICtrlListView_AddColumn($hListView, "Identification", 90)
_GUICtrlListView_AddColumn($hListView, "msg", 34)
GUISetState()
$loop = 0
While 1
    Sleep(1000)
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        ExitLoop
    EndIf
    For $r = 0 to UBound($array) - 1
        If $array[$r][0] <> "" Then
            $recv = $recv & TCPRecv($array[$r][0], 1000000)
            If $recv <> "" Then
                $data = StringSplit($recv,"$")
                $even_odd = _MathCheckDiv($data[0], 2)
                If $even_odd = 2 Then
                    For $x = 1 To $data[0] Step 2
                        _GUICtrlListView_InsertItem($hListView, $data[$x], 0)
                        _GUICtrlListView_AddSubItem($hListView, 0, $data[$x+1], 1, 1)
                        $loop += 1
                    Next
                    $recv = ''
                Else
                    For $x = 1 To $data[0]-1 Step 2
                        _GUICtrlListView_InsertItem($hListView, $data[$x], 0)
                        _GUICtrlListView_AddSubItem($hListView, 0, $data[$x+1], 1, 1)
                        $loop += 1
                    Next
                    $recv = $data[$data[0]]
                EndIf
            EndIf
            TCPSend($array[$r][0],String(Random(200,300,1)))
        EndIf
    Next
    WinSetTitle($gui,'',$loop &' msgs recived')
    newclient()
WEnd
TCPShutdown()
Func newclient()
    $ConnectedSocket = TCPAccept($MainSocket)
    If $ConnectedSocket <> -1 Then
        $array[$f][0] = $ConnectedSocket
        $f += 1
    EndIf
EndFunc

Client

TCPStartup ( )
$ConnectedSocket = TCPConnect ( @IPAddress1 , 33891 )
$x = 0
$t = TimerInit()
While 1
    $x += 1
;$reply = BinaryToString ( TCPRecv ( $ConnectedSocket , 10000000 , 1 ) )
;If $reply <> ""Then ToolTip( $reply )
    TCPSend ( $ConnectedSocket , DriveGetSerial("c:") & "$" & String(Random(1,10,1))& "$")
    ToolTip($x)
    If TimerDiff($t) > 10000 Then ExitLoop
Wend
MsgBox(0,'Client heare', 'Sends :' & $x &' Msgs')
Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

OK. thanks bogQ so basically the data is served to the socket and just kinda sits there in the memory until TCPRecv() picks it up. That's what i was hoping.

@BrewManNH This is a script that i copied and am now modifying from Here and there are a lot of loops that are currently beyond my understanding (even with looking them up) i was wondering if the Do...Until loop was the culprit. It appears now (after advice from both of you) that it was the main (not sole) culprit of the high cpu useage. Thank you for your help, and I agree with your last post entirely

Link to comment
Share on other sites

Just always keep in mind, any loop that you perform can max CPU threads unless there is some form of pause in each iteration. Some functions, such as GUIGetMsg() contain pauses built into them, so you don't have to specify them yourself.

It's always good to just test it out, see if it maxes out any of your CPU threads, and if it does, throw in a sleep(10) to force the CPU to rest.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

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