Jump to content

TCP UDF, Event driven!


Kip
 Share

Recommended Posts

  • 6 months later...

Hello,

im useing this tcp udf in one of my main "projects", im currently working towards a new version and i never had problems with this udf, but now the data i want to send has changed in size, so i have changed:

Local $sDataBuff = TCPRecv( $hSocket, 1024)

in TCP.udf -> line ~200 (server) and -> line ~400 (client)

from 1024 to 102400 to recvieve more data (lol how clever)

but the data is not increasing, on the same amount of data in sample 8000 characters are send from the server randomly only 1297 to 2601 characters are recieved on the client.

hope you can help me to solve this

.

and sorry for bumping a old thread

regards

Edited by Vision
Link to comment
Share on other sites

  • 1 month later...

Hello

I managed to use this with local network but i dont know how to make it work global, so that i can send client to my friends while server works at my home.

Is there any way i can to that?

Link to comment
Share on other sites

Hello

I managed to use this with local network but i dont know how to make it work global, so that i can send client to my friends while server works at my home.

Is there any way i can to that?

Yes, within the router, forward the port you are using to your computer.

Google how to do it for your specific router.

Link to comment
Share on other sites

  • 3 weeks later...

Hello, Kip. Nice functions.

I have a little question.$TCP_SEND event occures once and if no data sended and no other activity on socket event never posted again, right? It's mean that i need some flag variable in my script to store sending condition if i does not send data at the moment of $TCP_SEND occurance. Or event will be posted again and again even no activities on socket?

Link to comment
Share on other sites

After some experiments i have found the answer. $TCP_SEND wil be posted right after connect to the peer and after disapearance of some error. In most case i have 10035 error(send buffer of asocket is full) when sending data and then have $TCP_SEND.

I wrote wrapper function and put it in tcp.au3 so i don't need $TCP_SEND in my main scripts anymore. I just fill the buffer and TCP.au3 make all work for me.

#region Sending data
Func _TCP_Send_to_Buffer($hSocket, $Data)
If Not BinaryLen($Data) Then Return 1;we did it :)
Local $iSocket = __TCP_SocketHandle2SocketID($hSocket)
if @error then Return Seterror(1,0,0)
If BinaryLen($__TCP_SEND_Buffers[$iSocket]) > $__TCP_BUFFERSIZE * 1024 Then;buffer is full $__TCP_BUFFERSIZE is 10% of free memory.
  Return SetError(1, BinaryLen($__TCP_SEND_Buffers[$iSocket]), 0)
EndIf
Local $DataType = IsBinary($Data)
$Data = __TCP_Cry($iSocket, $Data, $DataType) ;i crypt and zlib compress my data
;in this part i make my own packet structure - 5bytes header with datasize and datatype info
Local $DataSize = BinaryLen($Data)
If $DataType = 1 Then
  Local $Header = Binary(Binary(True) & Binary($DataSize))
Else
  Local $Header = Binary(Binary(False) & Binary($DataSize))
EndIf
$__TCP_SEND_Buffers[$iSocket] &= $Header
$__TCP_SEND_Buffers[$iSocket] &= $Data
$__TCP_SEND_Buffers[$iSocket] = Binary($__TCP_SEND_Buffers[$iSocket])
If $__TCP_SOCKETS[$iSocket][$iSendFlag] Then ;i need flag to take $TCP_SEND state
Local $iSendres = __TCP_SendBuffer($hSocket)
Return 1
EndFunc   ;==>_TCP_Send_to_Buffer

Func __TCP_SEND($hSocket, $iError) ;$TCP_SEND event calls this func
$iSocket = __TCP_SocketHandle2SocketID($hSocket)
If Not $iError Then $__TCP_SOCKETS[$iSocket][$iSendFlag] = True
__TCP_SendBuffer($hSocket)
EndFunc   ;==>__TCP_SEND

Func __TCP_SendBuffer($hSocket)
Local $iSocket = __TCP_SocketHandle2SocketID($hSocket)
If Not $__TCP_SOCKETS[$iSocket][$iSendFlag] Then Return SetError(0, 0, 0)
Local $bytesent = 0
While BinaryLen($__TCP_SEND_Buffers[$iSocket])
    $bytesent = TCPSend($hSocket, $__TCP_SEND_Buffers[$iSocket])
    If @error = 10035 Then;WS buffer is full
             $__TCP_SEND_Buffers[$iSocket] = BinaryMid($__TCP_SEND_Buffers[$iSocket], $bytesent + 1)
             $__TCP_SOCKETS[$iSocket][$iSendFlag] = 0
              Return SetError(1, 10035, 0);error when sending winsock send window is full
  ElseIf @error Then ; some other error maybe disconnect
              $__TCP_SEND_Buffers[$iSocket] = BinaryMid($__TCP_SEND_Buffers[$iSocket], $bytesent + 1)
              $__TCP_SOCKETS[$iSocket][$iSendFlag] = 0
              Return SetError(2, @error, 0);error when sending
  EndIf
  $__TCP_SEND_Buffers[$iSocket] = BinaryMid($__TCP_SEND_Buffers[$iSocket], $bytesent + 1)
WEnd
Return 1
EndFunc   ;==>__TCP_SendBuffer
#endregion Sending data

code is not ideal but it works for me. In main script i send data in chunks.

Link to comment
Share on other sites

  • 3 weeks later...

Aloha.

I'm really lost to this TCP with the example.

I have read this post from the beginning, and there were different TCP / Client / server.

I tried to setup in my office but it shows that the "Could not connect...."

When I do download the 3 different .au3 files.... I only edit the TCP.au3 File and change the IP to the server's IP.... right???

Awwww.. please Help.

Thank You Sooo much

Link to comment
Share on other sites

  • 4 weeks later...

Hi.

I think this is exactly what I have been looking for. I'm stuck just now though, can't seem to get my server to notice a message from the client.

Here's the client code:

#include <GUIConstantsEx.au3>

#include <TCP.AU3>

Func Connected($connection, $iError)

If not $iError Then

TrayTip("Client", "Connected...", 5)

Else

TrayTip("Client", "Failed to connect!", 5, 3)

EndIf

EndFunc

func disconnected($connection, $iError)

TrayTip("Client", "Connection closed or dropped", 5, 2)

endFunc

func receive($session, $iError)

TrayTip("Client", "received", 5); not sure if this works, the server is where the problem is that I know of

endFunc

func disconnect()

_TCP_Client_Stop($connection)

endFunc

func process(); my gui has a button to click that calls this

msgbox (0, "", "processing "&guiCtrlRead($input));This works, but the msgbox on the server doesn't pop up after I ok this

TCPSend($connection, guiCtrlRead($input)); input is an edit field in the client to type something to try sending to the server

endFunc

Func terminate()

_TCP_Client_Stop($connection)

Exit

endFunc

while 1

if not $connected and isIPV4Address($serverIP) then;$connected is a variable that's assigned properly in code that's stripped from this snippet, same as isIPV4Address

$connection = _TCP_Client_Create($ServerIP, 25999)

_TCP_RegisterEvent($connection, $TCP_RECEIVE, "Received")

_TCP_RegisterEvent($connection, $TCP_CONNECT, "Connected")

_TCP_RegisterEvent($connection, $TCP_DISCONNECT, "Disconnected")

endIf

sleep(500)

wend

That's the end of the client code, here's the server.

global $connection = _TCP_Server_Create(25999)

_TCP_RegisterEvent($connection, $TCP_NEWCLIENT, "NewClient")

_TCP_RegisterEvent($connection, $TCP_DISCONNECT, "Disconnect")

_TCP_RegisterEvent($connection, $TCP_RECEIVE, "Receive")

Func NewClient($session, $iError)

_TCP_Send($session, "connected"); Haven't set up the receive function on the client so don't know if it works that way.

EndFunc

Func Disconnect($session, $iError)

;I have a notification coded in here

EndFunc

func getIP($IP)

return _TCP_Server_ClientIP($IP);works great till I try to use it to get the IP of a disconnected client. Any suggestions on that while we're at it?

endFunc

func receive($receivedData, $iError)

msgbox(0, "server", "received "&$receivedData); doesn't seem to be working though the process function in the client does pop up it's own msgbox

endFunc

func terminate()

_TCP_Server_Broadcast("shutdown")

_TCP_Server_Stop()

sleep(1000)

exit

endFunc

while 1

sleep(100)

wend

Link to comment
Share on other sites

  • 4 months later...
  • Developers

help me please i want send file with the udf

couple of questions/remarks:

1. Why do you post in this thread?

2. What have you done/researched yet?

3. You haven't really asked a specific question so what is it yo need help with?

Anyway start your own thread with your support request.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 1 month later...

A very well designed UDF, but I do have a couple of improvements that could be made perhaps.

1. Run the UDF through Jos' Tidy.

2. Use #forceref to bypass Au3Check (advanced parameters) errors.

3. Remove explicitly declaring loop only variables e.g. Line 230 in _TCP_Server_Stop.

4. You're missing a DllClose for $hWs2_32 and TCPShutdown.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 2 months later...

Aloha.

I'm really lost to this TCP with the example.

I have read this post from the beginning, and there were different TCP / Client / server.

I tried to setup in my office but it shows that the "Could not connect...."

When I do download the 3 different .au3 files.... I only edit the TCP.au3 File and change the IP to the server's IP.... right???

Awwww.. please Help.

Thank You Sooo much

No one can help you unless you post your code, because you've obviously got something a little whack. Let us help!

@Valiant8086 - You did not set an IP address in your client script to send to the server, unless you haven't posted all your code..

Hey, how do I use _TCP_Server_ClientList()?

gonna need a little more than that..

Edited by JonBMN
Link to comment
Share on other sites

  • 3 weeks later...

We found the problem. It kind of relates back to guinness's suggestion a couple posts up. It relates to using the Obfuscator. The script runs fine until you compile it with #AutoIt3Wrapper_Run_Obfuscator=y at which point compiling generates a lot of warnings and errors and the resultant exe does not work properly.

By changing the directive to #AutoIt3Wrapper_Run_Obfuscator=n everything works as an exe but it just isn't clean......

Link to comment
Share on other sites

  • 2 months later...

Hello,

im useing this tcp udf in one of my main "projects", im currently working towards a new version and i never had problems with this udf, but now the data i want to send has changed in size, so i have changed:

 

Local $sDataBuff = TCPRecv( $hSocket, 1024)
in TCP.udf -> line ~200 (server) and -> line ~400 (client)

from 1024 to 102400 to recvieve more data (lol how clever)

but the data is not increasing, on the same amount of data in sample 8000 characters are send from the server randomly only 1297 to 2601 characters are recieved on the client.

hope you can help me to solve this

.

and sorry for bumping a old thread

regards

 

I am having this same problem with the Event Driven P2P script based off this script, it's not sendign the data as packets, it's simply cutting off the string when it's send over a network connection, locally there is no limit for TCPSend, but it seems that over a network connection, only so many bytes can be transferred at once.

Link to comment
Share on other sites

  • 5 years later...

Even if Kip doesn't recommend his UDF, I will still use it, because it is great :-)

I use $TCP_DISCONNECT and can get a message if the connection is lost. But I don't get a message, when the used WLAN connection is broken. Am I doing something wrong or is there a simple way to implement this?

Otherwise I could separately ping... I would favour a way with minimal load.

 

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