Jump to content

TCPRecv and TCPSend


Recommended Posts

So i have currently tested the the Sql Functions and TCP functionality in autoit and came accross some problems.on TCPRecv.

The string being passed on by the client is approximately 30304 long "according to the return value of StringLen"

i checked the length of the string before being passed and after it was received. When it was received it was only 1460 long. My TCPRecv Max character limit is set to 100000000  "see code :)"

 

Server

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>

TCPStartup()
Dim $Socket_Data[1]
$Socket_Data[0] = 0
$Listen = TCPListen("192.168.72.8", 4444, 500)
If @error Then
    ConsoleWrite('!--> TCPListen error number ( ' & @error & ' ), look in the help file (on MSDN link) on func TCPListen to get more info about this error.' & @CRLF)
    Exit
 EndIf



While 1
    For $x = $Socket_Data[0] To 1 Step -1
        $Recv = TCPRecv($Socket_Data[$x], 100000000)
        If $Recv Then
          $Converted = StringLen($Recv)
            MsgBox($MB_SYSTEMMODAL, "Received string is", $Converted)
            MsgBox($MB_SYSTEMMODAL, "Received string is", $Recv)
           ;    Local $Converted = StringSplit($Recv, "*")
            ;   _ArrayDisplay($Converted)

            TCPCloseSocket($Socket_Data[$x])
            _ArrayDelete($Socket_Data, $x)
            $Socket_Data[0] -= 1
        EndIf
    Next
    _Accept()
 WEnd



Func _Accept()
    Local $Accept = TCPAccept($Listen)
    If $Accept <> -1 Then
        _ArrayAdd($Socket_Data, $Accept)
        $Socket_Data[0] += 1
    EndIf
EndFunc   ;==>_Accept

Client

#include <File.au3>
#include <MsgBoxConstants.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>


TCPStartup()
$Socket = TCPConnect("192.168.72.8", 4444)

If $socket = -1 Then ;if $socket = -1 then error
   MsgBox(16, "Error:", "Can't connect to server")
EndIf



Local $Dir = @ScriptDir&"\"&"History"
Local $aResult, $iRows, $iColumns, $iRval
Local $sLocalSQLiteDll =  @ScriptDir&"\"&"sqlite3_302700200.dll" ; to be change to an existing .dll to have no error

Local $sSQliteDll = _SQLite_Startup($sLocalSQLiteDll, False, 1)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "SQLite Error", "'SQLite3.dll' Can't be Loaded!")
    Exit -1
EndIf

Local $myDB = _SQLite_Open($Dir) ; Creates a temporary disk database
If @error Then
    MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Can't create a temporary Database!")
    Exit -1
 Else
;   MsgBox($MB_SYSTEMMODAL, "SQLite Loaded", "Success boys")
EndIf


$iRval = _SQLite_GetTable2d(-1, "SELECT * FROM urls;", $aResult, $iRows, $iColumns)
If $iRval = $SQLITE_OK Then

    Local $count = UBound($aResult,$UBOUND_ROWS) ; Count the number of rows including the Index
    Local $iEnd_Row =$count-1
    Local $Converted = _ArrayToString($aResult, "*", 1,$iEnd_Row,"*", 1,1)

   MsgBox($MB_SYSTEMMODAL, "Sent string is", $Converted)

;  _ArrayDisplay($aResult)
   $sendedBytes = TCPSend($socket, $Converted) ;send message to connected socket
   If $sendedBytes = 0 Then ;if receiving data TCPSend(...) = 0 then error
      MsgBox(16, "Error", "Can't send message")
   EndIf
   TCPCloseSocket($socket)
   TCPShutdown()

Else
    MsgBox($MB_SYSTEMMODAL, "SQLite Error: " & $iRval, _SQLite_ErrMsg())
EndIf


_SQLite_Close($myDB)
_SQLite_Shutdown()

If my code is messy im sorry i just started learning this Hope u guys help me

Link to comment
Share on other sites

Receiver should have a loop that resembles to this :

Local $sBuffer = Binary (""), $sReceived = Binary ("")
    Do
      $sReceived &= $sBuffer
      $sBuffer = TCPRecv($iSocket, 4096, $TCP_DATA_BINARY)
    Until $sBuffer = "" or @error

Instead of trying to use a huge buffer...

Link to comment
Share on other sites

@Nine the receiver won't receive it - it's getting truncated by the transmission. From the link:

When setting up a TCP connection, a Maximum Segment Size (MSS) is agreed upon. This could be considered an MTU at layer 4, but it is not fixed. It is often set to the largest payload that can be sent in a TCP segment without causing fragmentation, thus reflecting the lowest layer 2 MTU on the path. With an ethernet MTU of 1500, this MSS would be 1460 after subtracting 20 bytes for the IPv4 and TCP header.

The OP needs to establish the MSS between the sender and receiver and then use that to control the size of the transmitted TCP packets. Note, whilst the poster of the above comment uses the term "agreed upon" this should not be considered a negotiation by the TCP, it is merely the supported value for the connection.

 

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

10 minutes ago, SlackerAl said:

@Nine the receiver won't receive it - it's getting truncated by the transmission.

Nah, it is working fine.  I have used my receiver many times, and it works perfectly with small and large files, txt and binay files, etc. 

Take also a look at TCPRecv () function in help file, they are also using 4096 bytes transfert.

Edited by Nine
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...