Jump to content

Send a file with TCP/IP


Recommended Posts

Hi Everyone,

I have some code that I'm trying to get working. My concept is to have an "update" client on my client machines. When that client runs, it checks in with a server. If that server is currently hosting something, it will download the file and run it. If the server isn't running, the client displays a MsgBox stating that there are no updates currently.

My problem occurs when only 2k of my executable are being transferred over.

Any help would be greatly appreciated.

SERVER

$filetosend = FileOpenDialog("File To Send", "C:\", "All (*.*)", 3)
$filetoread = FileOpen($filetosend, 16)
$datatosend = FileRead($filetoread)
FileClose($filetosend)

$g_IP = "192.168.2.2"
$recv = ""
MsgBox(0, "", StringLen($datatosend))
TCPStartup()

$MainSocket = TCPListen($g_IP, 12000)
If $MainSocket = -1 Then Exit

While 1
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
    $sent = TCPSend($ConnectedSocket, String($datatosend))
    TCPCloseSocket($ConnectedSocket)
WEnd

and

CLIENT

$IPADDRESS = "[SERVER IP HERE]"
$PORT = 12000
$recv = ""

TCPStartup()
$ConnectedSocket = TCPConnect($IPADDRESS, $PORT)
If $ConnectedSocket = -1 Then
    MsgBox(64, "No Updates", "No Updates Are Available At This Time")
    Exit
EndIf
While $recv = ""
    $recv = TCPRecv($ConnectedSocket, 743048)
WEnd
MsgBox(0, "Received:", $recv)
TCPSend($ConnectedSocket, 1)

MsgBox(0, "", StringLen($recv))

If $recv = "" Then
    MsgBox(64, "No Updates", "No Updates Are Available At This Time")
    Exit
EndIf

MsgBox(0, "Update Received", "")

MsgBox(0, "Received", $recv)

$tempfile = FileOpen(@TempDir&"\update.exe", 18)
FileWrite($tempfile, $recv)
FileClose($tempfile)

MsgBox(0, "Update Written", "")

Run(@TempDir&"\update.exe")

Exit
Link to comment
Share on other sites

TCPSend returns the number of bytes sent... If the return does not match the file length, trim off the number of bytes that were sent and send again... until all bytes are sent.

When TCPSending you should really send a "header" before the data... telling the reciever what is coming and how much of it to expect.

Lar.

f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Link to comment
Share on other sites

TCPSend returns the number of bytes sent... If the return does not match the file length, trim off the number of bytes that were sent and send again... until all bytes are sent.

When TCPSending you should really send a "header" before the data... telling the reciever what is coming and how much of it to expect.

Lar.

The listening value for maximum characters with the TCPRecv command is larger than the data I'm sending, which is why I'm being thrown for a loop.

Thanks,

~Felanor

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