Jump to content

send binary file with TCP.


Recommended Posts

hi all!

so, i want to send a file with TCP.

i read some posts and i knew i have to use File properties.

here's my code:

$binaryfile = FileOpen("dati.txt",16) ; open the file binary mode
TCPSend($socket,$binaryfile) ; send the binary data to client

; the client instead

$file = TCPRecv($socket, 2048) ; receive the file 
FileWrite($file, "prova.txt") ; write the data into a file

what's my error? thank you for all, see you! ;-)

ehi ehi ehi, what is your name?

Link to comment
Share on other sites

file is in workdirectory, isn't right?

anyway here there is all my code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <INet.au3>

; genera il form con tutti i suoi controlli;

$Form1 = GUICreate("Accesso remoto!", 370, 450, 192, 124)
$pdc = GUICtrlCreateLabel("Pannello di Controllo - Connesso a: NESSUNO [IP INS'T DEFINED].", 24, 16, 326, 17)
$attdisatt = GUICtrlCreateButton("Apri il lancia comandi (on)", 24, 56, 150, 33)
$pdm = GUICtrlCreateList("", 24, 112, 321, 273)
GUICtrlSetData(-1, "Disattivo!")
GUICtrlSetState(-1, $GUI_DISABLE)
$chiusura = GUICtrlCreateButton("Termina la sessione.", 184, 400, 161, 33)
GUISetState(@SW_SHOW)

    TCPStartup()    ; attivo il servizio TCP
    $ascolto = TCPListen(@IPAddress1, 666)  ; mi metto in ascolto sulla porta 666 dell'ip locale

While 666
    
    $socket = TCPAccept($ascolto)   ; attendo che il client si connetta
    
    If $socket <> -1 Then   ; quando il client è connesso
            $ricvo = TCPRecv($socket, 32)   ; ricevo la stringa (nome utente e indirizzo ip)
            $ricv = StringStripWS($ricvo, 1+2)
            $arrayricv = StringSplit($ricv, "-")
            $nomeutente = $arrayricv[1]
            $indirizzoip = $arrayricv[2]
            $indirizzopubblico = _GetIP()
            GUICtrlSetData($pdc, "Pannello di Controllo - Connesso a: " & $nomeutente & " [" & $indirizzopubblico & "].") ; ...e imposto i dati ricevuti
            $binaryfile = FileOpen("dati.txt",16)
            MsgBox(0, "", $binaryfile)
            TCPSend($socket,$binaryfile)
    EndIf
    
    $nMsg = GUIGetMsg()
    
    Switch $nMsg
        
        Case $GUI_EVENT_CLOSE
            Exit
        Case $chiusura
            Exit
        Case $attdisatt
            GUICtrlSetState($pdm, $GUI_ENABLE)
            GUICtrlSetData($attdisatt, "Chiudi il lancia comandi (off)")

    EndSwitch
WEnd

this's for the client:

#include <_TCPFileTransfer.au3>
#include <File.au3>

TCPStartup()
$socket = TCPConnect(@IPAddress1, 666)
If $socket <> -1 Then
    $identify = @UserName & "-" & @IPAddress1
    TCPSend($socket, $identify)
Else
    MsgBox(0, "Errore nr: ", @error)
EndIf

$file = TCPRecv($socket, 2048)
MsgBox(0, "ecco il relativo codice:", $file)
FileWrite("prova.txt", $file)

ehi ehi ehi, what is your name?

Link to comment
Share on other sites

TCP functions are non-blocking. It means they will return (almost) instantly. Even if there is no data available. Your server code can take a while to execute, during which time the client has already decided there won't be any data.

Do something like:

While 1
$file = TCPRecv($socket, 2048)
MsgBox(0, "ecco il relativo codice:", $file)
FileWrite("prova.txt", $file)
WEnd
Link to comment
Share on other sites

TCP does not combine easily with GUI. Especially if this is your first time. Remove bits from your script until you have found the problem. It's called debugging and it's a necessary skill for programming.

Another mistake:

$binaryfile = FileOpen("dati.txt",16)
            MsgBox(0, "", $binaryfile)
            TCPSend($socket,$binaryfile

What does the MsgBox print? Right ...

Link to comment
Share on other sites

Maybe these Function will shed some light too.

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

I take it you read the Forum Post, because there is an Example of how to use the Functions in your Script >>

I am a little confused by this "do it need a "include" istruction with a macro? what's this? how can i use this?" The Functions have to be copied to your Script and then "Called" when required. As I said have a look at the "Working Example" above to get an idea of how to use :)

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

u italian? :)

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Link to comment
Share on other sites

pure io :)

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Link to comment
Share on other sites

Would you be kind and make that picture in your signature smaller.

Sure.

Before, it was like 3 times bigger, i reduced it to make less bothering, and yet is too big lol. Gimme 5 minutes

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

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