Sign in to follow this  
Followers 0
binarydigit0101

send binary file with TCP.

20 posts in this topic

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?

Share this post


Link to post
Share on other sites



; the client instead

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

Should be:

; the client instead

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

:)

Share this post


Link to post
Share on other sites

hahaha! you're right;

now the client creates the file.

anyway there's another problem: server sends nothing to client.

two programs are already connected.

what is happening? :-P


ehi ehi ehi, what is your name?

Share this post


Link to post
Share on other sites

Make sure that "dati.txt" exists on the server. Otherwise, we will have to see more of your code to determine the problem.

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

thank you!

sorry but client doesn't receive anything.


ehi ehi ehi, what is your name?

Share this post


Link to post
Share on other sites

thank you!

sorry but client doesn't receive anything.

Do i put "while" also in server?


ehi ehi ehi, what is your name?

Share this post


Link to post
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 ...

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

so the MSGBOX was a way to see what client received.

what do i remove for the first times?

p.s.: for debug i asked because i don't know if the syntax is correct.

Edited by binarydigit0101

ehi ehi ehi, what is your name?

Share this post


Link to post
Share on other sites

Maybe these Function will shed some light too.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

i read something about it, but i do anything.

do it need a "include" istruction with a macro? what's this? how can i use this?

thank you very much, have a good day! :-)


ehi ehi ehi, what is your name?

Share this post


Link to post
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 :)


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

Maybe these Function will shed some light too.

LOL! i don't see this was a link... :-D

sorry for this... thank you for all! ;-)


ehi ehi ehi, what is your name?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

oui! :)


ehi ehi ehi, what is your name?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

pure io :)

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

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
:) maybe that signature is too big... :)

ehi ehi ehi, what is your name?

Share this post


Link to post
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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0