Jump to content

[SOLVED]TCP Send/Rec Debug.


kudrow
 Share

Recommended Posts

I am not sure whats going on but attached is my script. It calls a function that generates a text file then reads in the text file and sends it over TCP. It is reading all of the data from the text file and storing it in the variable $chars just fine. The problem is that not all of the data is being sent. It gets cut off. I have set tcpRead to accept 999999999999 chars and it still cuts off at the same spot. I also tried forcing it to send in binary but made no difference. Any help would be appreciated.

TCPStartup()
Global $Socket_Data[1], $Recv
Global $Listen = TCPListen(@IPAddress1, 6543, 500)
If @error Then
MsgBox(0,'',@error)
Exit
EndIf
$Socket_Data[0] = 0
While 1
For $x = $Socket_Data[0] To 1 Step -1;front to bck loop cos of _ArrayDelete
$Recv = TCPRecv($Socket_Data[$x], 1000000)
If $Recv == "report" Then
go()
Local $file = FileOpen("C:\apptemp\file0.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
; Read in 1 character at a time until the EOF is reached
Global $chars = FileRead($file)
$chars2 = StringToBinary($chars, 4)
$chars3 = BinaryLen($chars2)

FileClose($file)

TCPSend($Socket_Data[$x], $chars2 );send some response if needed or additional code before closing that socket
TCPCloseSocket($Socket_Data[$x])
_ArrayDelete($Socket_Data,$x)
$Socket_Data[0] = $Socket_Data[0] - 1
EndIf
Next
_Accept()
WEnd
Func _Accept()
$Accept = TCPAccept($Listen)
If $Accept <> -1 Then
MsgBox(0,'Connected',$Accept,1)
_ArrayAdd($Socket_Data,$Accept)
$Socket_Data[0] = $Socket_Data[0] + 1
EndIf
EndFunc
Edited by kudrow
Link to comment
Share on other sites

If it cuts off you'll have to divide your string into small parts and then send/recv in for/while loop.

Yeah I adjusted my client side script to just troubleshoot and found that it is not waiting long enough to reveive all the data. I simply added a sleep(100) to test this and it worked but I am affraid it is not the best solution. Any thoughts? I know that the server is sending all of the data now. The client is just not waiting for all of it.

TCPStartup()
$Socket = TCPConnect("XXX.XXX.XXX.XXX", 6543)
If $socket = -1 Then Exit
TCPSend($Socket, 'report')
;gona try if needed to recive last msg from server (named 'haha' in server script) before exiting
Do
$Recv = TCPRecv($Socket, 90000, 1)
Sleep(100)
Until $Recv

$file = FileOpen ("C:apptempfile0.txt", 2)
FileWrite($file, $Recv)
FileClose($file)
ConsoleWrite($Recv)

Exit
Edited by kudrow
Link to comment
Share on other sites

Hi,

The data length depends on your connection, don't use big numbers, it's dirty and It won't make it work better.

So, according to this length you have to split your file in order to send each part at time (use an offset for that).

And, for the receiver, you need to wait until the end of the file, because the whole data won't be sent at once. I suggest you to put a "code" like for beginning the transfer.

Br, FireFox.

Link to comment
Share on other sites

Hi,

The data length depends on your connection, don't use big numbers, it's dirty and It won't make it work better.

So, according to this length you have to split your file in order to send each part at time (use an offset for that).

And, for the receiver, you need to wait until the end of the file, because the whole data won't be sent at once. I suggest you to put a "code" like for beginning the transfer.

Br, FireFox.

I think I am going to try and prefix the data with the number of bytes and do a check on the receiving end to make sure I get the total amount. I did a little research and the biggest TCP packet size will be based on the MTU size which is 1500. So it looks like the client side receives 1500 bytes and goes on when there is actually about 2900. Thanks for the insight everyone.

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