Jump to content

File Transfer with TCP stops Script


Recommended Posts

Hello everyone,

This is my first post in the autoit forum. Usually i don't sign up on boards, because I'm not very active in writing posts and i would feel bad if i only post here if i have a question.

After reading the board for a while and learning about AutoIT i decided to sign up anyway, because i made AutoIT to some kind of hobby of mine, as it lets me program creative applications without knowing very much about complex programming. I hope that atleast for everything i ask, i can help out someone else on this forum.

Now to my problem:

I've been experimenting with File Transfer via TCP and even though there are many UDFs out there, i couldn't get it to work properply.

At the moment, im not even sure which of my approaches with the different UDFs was the right one, so I'm posting my last try:

SENDER

$test = FileOpen("D:\test.zip", 16)
$bin = FileRead($test)
;MsgBox("","",$bin)

Local $S_ip = "<onlineip>"
Local $S_Port = 21525

TCPStartup()
Local $socket = TCPConnect($S_ip, $S_Port)
If $socket <> -1 Then

    TCPSend($socket, $bin)
    TCPCloseSocket($socket)

EndIf

TCPShutdown()

RECEIVER

it uses the _TCPFileTransfer.au3 , by DELmE

Unluckily he hasn't been around for a long time now, so i decided to post a new thread. I don't necessarily want to use that UDF, I'm grateful for every help to make it work and to understand it and I don't understand his functions very well.

#include <_TCPFileTransfer.au3>
TCPStartup()
$sock = TCPListen("<onlineip>",21525)

Do
    $ssock = TCPAccept($sock)

Until $ssock <> -1
$rFileRecv = _TCPFileRecv($ssock, "test.zip")
MsgBox(0,"Server",$rFileRecv)
TCPShutdown()

NOTE: change <onlineip> to the ip of the server, before trying out

What happens when running?

When i start the receiver and then start the sender, the file gets transfered, but the scripts stops and the MsgBox doesnt show up.

If i try to open the archive, it tells me that another process is acessing the file and i can not open it. After forcing the Server to quit, i can open the archive and the data seems to be okay.

At first i thought that maybe the script is listening if more data is coming and doesn't close the file, so it is unaccessable.

But I don't understand that Functions and its winapihandles so i couldnt test around with it more. Also if i would have known how i can make the program stop listening after x bytes or a specific time, i could make the script still run and display an error, if filetransfer didnt work instead of freezing.

I have attached DELmE's work on File Transfering, as it is required by the script above.

Thank you for reading, i know it is a lot of text.

_TCPFileTransfer.au3

Edited by Clock
Link to comment
Share on other sites

Hello, thanks for your reply!

I tryed to confirm if tcpshutdown() is beeing done, but im not sure how i can check, when it freezes before.

ive changed the code to this:

#include <_TCPFileTransfer.au3>
TCPStartup()
$sock = TCPListen("<onlineip>",21525)

Do
    $ssock = TCPAccept($sock)

Until $ssock <> -1
$rFileRecv = _TCPFileRecv($ssock, "test.zip")
If @error Then Exit
;MsgBox(0,"Server",$rFileRecv)
TCPShutdown()
MsgBox("","",@error)

It doesn't show any of the error returns and also doesn't exit in the If.

So the problem is this line? $rFileRecv = _TCPFileRecv($ssock, "test.zip")

I also tryed to do it all by myself, because I'm not good enough with AutoIT to fix the UDF myself if that is the problem.

TCPStartup()
$sock = TCPListen("<onlineip>",21525)

Do
    $ssock = TCPAccept($sock)

Until $ssock <> -1

While 1

    $get = TCPRecv($ssock, 2048, 1)


If StringLen($get) > 100 Then ExitLoop
WEnd

$test = FileOpen("D:\test.zip", 16)
FileWrite($test, $get)
FileClose($test)

MsgBox("","Done","Sucess")

;$rFileRecv = _TCPFileRecv($ssock, "test.zip")
;MsgBox(0,"Server",$rFileRecv)
TCPShutdown()

All it does is displaying the last message box when i send data. it doesn't save anything to file. :huh2:

Edited by Clock
Link to comment
Share on other sites

I just realised that other people have the same problem, too. I didn't read the UDF Authors thread fully, at the end someone has the exact same problem with the filehandle..

It would be best for me to try finding out how it works without UDF.. If anyone has any ideas, i posted what i have so far in the second script in the post 2 above.

Help with that would really be appreciated

EDIT:

Comment to my script:

With If StringLen($get) > 100 Then ExitLoop i just wanted the script to quit after 100 bits have been sended, just to check if he writes something to the file

I found various example scripts and UDFs and such in this forum and on the internet and it should work like that:

1) Open a file in binarymode

2) Read the file (when i msgbox the variable, it seems to be binary now) and close the file afterwards

3) send the data to the server

4) server puts data in a variable

5) i open a new file with file open in binary mode

6) write the data with filewrite to the file

7) close the file

8) file should be okay?

Hope someone can put me on the right track here (do i miss a step??) or could anyone show me the correct way to accept that traffic and save it? So that i can learn from it :huh2:

Edited by Clock
Link to comment
Share on other sites

I finally made it :huh2: If anyone else is interested in learning about file transfer over TCP, then just use this thread for good example: Also thank you for the help Tripredacus

The mistake that i made was that my script didn't read and send the file fully on the sender side.. and then on the receiver end it didn't write the file fully..

I had to read/transmit/receive/write the data in WHILE

Link to comment
Share on other sites

Hello, this script works perfectly with no changes.

The only problem with it is in the "RECEIVER" it has a icon in the task bar, is there anyway to take this icon off so it will run unnoticed?

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