Jump to content

Having Problem with sending *.jpg files per TCP


Recommended Posts

I started with programming a kind of DesktopRemote. For that, i need a live Desktop View. I want to realize that, with sending jpg screenshots per TCP.

;Server (Controlled) : 
#include <TCP.au3>
#include <ScreenCapture.au3>
$iClient_PIP = @IPAddress1
$iPort = 1234
_ScreenCapture_Capture(@DesktopDir&"\DRServer.jpg")
$hScreenFile = FileOpen(@DesktopDir&"\DRServer.jpg")
$sScreenToSend = FileRead($hScreenFile)
SendIP($iClient_PIP,$iPort,$sScreenToSend)

;Client (Controller) :
#include <TCP.au3>
$iPort = 1234
$iScreenReceived = ReceiveIP(@IPAddress1,$iPort)
FileWrite(@DesktopDir&"\Picture Received.jpg",$iScreenReceived)

My #Include <TCP.au3> File:

#include <IE.au3>
#include <String.au3>
Func IP ()
    $IE = _IECreate("wieistmeineip.de",0,0)
    $IP = _StringBetween(_IEBodyReadHTML($IE),'<div class="title"><strong>','</strong></div>')[0]
    Return $IP
EndFunc
$OwnIP = IP ()
$Main_Socket = 0
Func ChangeSocket ($IP_Address,$IP_Port)
    TCPShutdown ()
    $Main_Socket = 0
    TCPCloseSocket($Main_Socket)
    TCPStartup ()
    $Main_Socket = TCPListen($IP_Address,$IP_Port)
EndFunc

Func SendIP ($IP_Addre,$IP_Por,$Data)
    ChangeSocket ($IP_Addre,$IP_Por)
    $New_Socket = TCPConnect($IP_Addre,$IP_Por)
    $TCP_SEND = TCPSend($New_Socket,$Data)
    If $TCP_SEND < 1 Then
        MsgBox(0,"Error","Data couldn't be sent."&@CRLF&"Maybe the Receiver is offline."&@CRLF&"Maybe the IPAddress does not exist.")
        Return -1
    Else
        Return 1
    EndIf
    ChangeSocket($OwnIP,$IP_Por)
EndFunc

Func ReceiveIP ($IP_Add,$IP_Po)
    ChangeSocket($IP_Add,$IP_Po)
    Do
    $Accept = TCPAccept($Main_Socket)
        Until $Accept <> -1
            Do
                $Received = TCPRecv($Accept,10000000000000000000000000000)
            Until $Received <> ""
                Return $Received

EndFunc 

It works, but the Picture that arrives isn't completed.

How can i fix that?

(For first i just want to send 1 Screenshot.)

Edited by Programs4All
Link to comment
Share on other sites

3 issues:

- TCPCloseSocket comes first than TCPShutDown, anyway you need TCPShutDown only when you finish to work with sockets

- Use FileOpen in binary mode (16) and TCPRecv too (1)

 

; Notes: If you don't know how much length will be the data,

; use e.g: 2048 for maxlen parameter and call the function until the it returns nothing/error.

 

so modify this code:

Do
    $Accept = TCPAccept($Main_Socket)
Until $Accept <> -1
Do
    $Received = TCPRecv($Accept, 10000000000000000000000000000)
Until $Received <> ""
Return $Received

 with:

Do
    $Accept = TCPAccept($Main_Socket)
Until $Accept <> -1
Local $text
While 1
    $Received = TCPRecv($Accept, 2048, 1)
    If @error And @error <> -1 Then ExitLoop
    If $Received <> "" Then $text &= $Received
WEnd
Return $text

Anyhow there are millions of scripts that do exatly what you want, try to give them a look..

Edited by j0kky
Link to comment
Share on other sites

Doesn't work...

I receive a File, that gets an error message from Windows: "Photo Gallery can't open this photo or video. The file may be unsupportet, damaged or corrupted."

Where is my problem?

;Server:
#include <TCP.au3>
#include <ScreenCapture.au3>
TCPStartup ()
$iClient_PIP = @IPAddress1
$iPort = 1234
$iSocket = TCPConnect($iClient_PIP,$iPort)
_ScreenCapture_Capture(@DesktopDir&"\DRServer.jpg")
$hScreenFile = FileOpen(@DesktopDir&"\DRServer.jpg")
$sScreenToSend = FileRead($hScreenFile)
TCPSend($iSocket,$sScreenToSend)

;Client:
#include <TCP.au3>
#include <File.au3>
$iPort = 1234
TCPStartup()
$Main_Socket = TCPListen(@IPAddress1,$iPort)
Do
    $Accept = TCPAccept($Main_Socket)
Until $Accept <> -1
Local $Data
While 1
    $Received = TCPRecv($Accept, 2048, 1)
    If @error and @Error <> -1 Then ExitLoop
    $Data &= $Received
WEnd
_FileCreate(@DesktopDir&"\Picture Received.jpg")
FileWrite(@DesktopDir&"\Picture Received.jpg",$Data)
Link to comment
Share on other sites

 

Doesn't work...

I receive a File, that gets an error message from Windows: "Photo Gallery can't open this photo or video. The file may be unsupportet, damaged or corrupted."

Where is my problem?

;Server:
#include <TCP.au3>
#include <ScreenCapture.au3>
TCPStartup ()
$iClient_PIP = @IPAddress1
$iPort = 1234
$iSocket = TCPConnect($iClient_PIP,$iPort)
_ScreenCapture_Capture(@DesktopDir&"\DRServer.jpg")
$hScreenFile = FileOpen(@DesktopDir&"\DRServer.jpg")
$sScreenToSend = FileRead($hScreenFile)
TCPSend($iSocket,$sScreenToSend)

;Client:
#include <TCP.au3>
#include <File.au3>
$iPort = 1234
TCPStartup()
$Main_Socket = TCPListen(@IPAddress1,$iPort)
Do
    $Accept = TCPAccept($Main_Socket)
Until $Accept <> -1
Local $Data
While 1
    $Received = TCPRecv($Accept, 2048, 1)
    If @error and @Error <> -1 Then ExitLoop
    $Data &= $Received
WEnd
_FileCreate(@DesktopDir&"\Picture Received.jpg")
FileWrite(@DesktopDir&"\Picture Received.jpg",$Data)

And I just see, that AutoIt gets the wrong data from the File. So at Fileread, it gets a mess. The wrong mess. xD

Link to comment
Share on other sites

One more thing to add that I saw.. You can't create a client and server in the same script.

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Glad to hear @Programs4All. I know it's possible :) doesn't mean you should though :ermm:

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Quick question for understanding.. Why do you use:

FileOpen

and

FileRead

before you send your picture ?

I'm thinking you more or less mean to use the:

FileOpenDialog

function. I would think when you are sending the:

$sScreenToSend

you are sending yourself an error code from not being able to open and read a picture.

Try using this code for your server:

;Server:

#include <TCP.au3>
#include <ScreenCapture.au3>

TCPStartup ()

$iClient_PIP = @IPAddress1
$iPort = 1234
$iSocket = TCPConnect($iClient_PIP,$iPort)
_ScreenCapture_Capture(@DesktopDir & "\DRServer.jpg")
$hScreenFile = FileOpenDialog("Select Picture", @WorkingDir, "Images (*.jpg)")
If @error <> 0 Then
  If @error = 1 Then
    MsgBox(0, "error", "File selection failed")
  else
    MsgBox(0, "error", "Bad file filter")
EndIf
$sScreenToSend = $hScreenFile
TCPSend($iSocket,$sScreenToSend)

EDIT: @j0kky you are right, silly me. UEZ's client server looks to be a nice actual way of doing this in @Chimp 's post below.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

@Mikahs: he doesn't want to send the jpg path, he want to send the content of the file

 

@programs4all: have you tried the modifies I suggested?

Edited by j0kky
Link to comment
Share on other sites

I've done that. Now I'm standing infront of a new Problem  :nuke:. How can i receive TCP Messages with the Client? I Dont get it right now, because i dont want to use my old ChageSocket Function, that you can see on the top of the page... Is it able to Receive with TCPConnet (,) ?

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