Jump to content

Sending picture over tcp


Recommended Posts

Ok so i have a client and a server when the client sends the command "desktop" to the server the server takes a screenshot, and sends it to the client, the client then recives the information and assembles the picture out of it...there is a problem though when i go to view the screenshot that the client assembled it doesn't show. i'm trying to be as clear as i can but it's kind of hard for me to explain here is my source.

Client:

#include <GUIConstants.au3>

dim $sock

#region GUI
$frmmain = GUICreate("Send picture", 795, 577, 179, 116)
$picdesktop = GUICtrlCreatePic("Capture.jpg", 8, 8, 777, 529, BitOR($SS_NOTIFY,$WS_GROUP))
$txtip = GUICtrlCreateInput("127.0.0.1", 520, 544, 121, 21)
$txtport = GUICtrlCreateInput("1337", 648, 544, 57, 21)
$btnconnect = GUICtrlCreateButton("Connect", 712, 544, 75, 25, 0)
$txtsend = GUICtrlCreateInput("", 8, 544, 161, 21)
$btnsend = GUICtrlCreateButton("Send", 176, 544, 75, 25, 0)
$lblstatus = GUICtrlCreateLabel("Status: Disconnected", 312, 544, 156, 20)
GUISetState(@SW_SHOW)
#endregion

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
            Disconnect()
            Exit
            
        case $btnconnect
            Connect()
            
        case $btnsend
            SendToServer()
        EndSwitch
        
        Recive()
    WEnd
    
    func Recive();Recive routine
        $recv = TCPRecv($sock,100000000)
        
            if StringInStr($recv,"desktop") Then
                $temp = StringSplit($recv,":")
                FileWrite("Screenie.jpg",$temp[2])
            EndIf
            
            if $recv = "ac" Then
                GUICtrlSetData($lblstatus,"Status: Connected")
            EndIf
    EndFunc
        
    func SendToServer();Send routine
        TCPSend($sock,guictrlread($txtsend))
    EndFunc
            
    func Connect();Connect routine
        TCPStartup()
        $sock = TCPConnect(guictrlread($txtip),guictrlread($txtport))
    EndFunc
    
    func Disconnect();Disconnect routine
        TCPSend($sock,"disconnect")
        TCPCloseSocket($sock)
        TCPShutdown()
    EndFunc

Server:

#include <A3LScreenCap.au3>

Global $Connectedsock = -1
Global $sock = -1
Local $ip = "127.0.0.1"
Local $port = 1337

TCPStartup()
$sock = TCPListen($ip,$port);Wait for connection

while 1
    $recv = TCPRecv($connectedsock,100000000);Recive the data from the client
    
    if $recv = "disconnect" Then
        $connectedsock = -1
    EndIf
    
    if $recv = "desktop" Then
        _ScreenCap_Capture("pic.jpg")
        $file = FileOpen("pic.jpg",16)
        $fileread = FileRead($file)
        FileClose($file)
        TCPSend($connectedsock,"desktop:" & $fileread)
    EndIf
    
    if $connectedsock = -1 Then;Accept the connection
        $connectedsock = TCPAccept($sock)
        if $connectedsock <> - 1 Then
            TCPSend($connectedsock,"ac")
        EndIf
    EndIf
WEnd
Edited by deathkillspt
Link to comment
Share on other sites

Where, exactly, does it fail? Does the server generate the screenshot file to disk correctly, and that file is usable? Does the file get transferred correctly to the client, and is usable from disk? Does the graphical code on the client correctly present the file if you manually copy it over?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Does the server generate the screenshot file to disk correctly, and that file is usable?

Yes

Does the file get transferred correctly to the client, and is usable from disk?

No, I think it's failing here.

Does the graphical code on the client correctly present the file if you manually copy it over?

Actuallly it doesn't for some reason?

But for instance if i do this:

#include <A3LScreenCap.au3>

_ScreenCap_Capture("Picture.jpg")
$file = FileOpen("Picture.jpg",16)
$fileread = FileRead($file)
FileWrite("Picture2.jpg",$fileread)

It works like a charm.

But if i try to send $fileread over the connection and assemble it on the client side it doesnt work right, It just shows a whole bunch of numbers and it doesnt look anything like the graphical code of the picture taken on the server side.

Edited by deathkillspt
Link to comment
Share on other sites

W00t W00t i thought about what larry said and i've finally got atleast something that works

Client:

#include <GUIConstants.au3>

dim $sock

#region GUI
$frmmain = GUICreate("Send picture", 795, 577, 179, 116)
$picdesktop = GUICtrlCreatePic("Capture.jpg", 8, 8, 777, 529, BitOR($SS_NOTIFY,$WS_GROUP))
$txtip = GUICtrlCreateInput("127.0.0.1", 520, 544, 121, 21)
$txtport = GUICtrlCreateInput("1337", 648, 544, 57, 21)
$btnconnect = GUICtrlCreateButton("Connect", 712, 544, 75, 25, 0)
$txtsend = GUICtrlCreateInput("", 8, 544, 161, 21)
$btnsend = GUICtrlCreateButton("Send", 176, 544, 75, 25, 0)
$lblstatus = GUICtrlCreateLabel("Status: Disconnected", 312, 544, 156, 20)
GUISetState(@SW_SHOW)
#endregion

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
            Disconnect()
            Exit
            
        case $btnconnect
            Connect()
            
        case $btnsend
            SendToServer()
        EndSwitch
        
        Recive()
    WEnd
    
    func Recive();Recive routine
        $recv = TCPRecv($sock,100000000)
        
            if StringInStr($recv,"[start]") Then
                do
                    $chars = TCPRecv($sock,1)
                    FileWrite("Screenie.jpg",$chars)
                until $recv = "[end]"
            EndIf
            
            if $recv = "ac" Then
                GUICtrlSetData($lblstatus,"Status: Connected")
            EndIf
    EndFunc
        
    func SendToServer();Send routine
        TCPSend($sock,guictrlread($txtsend))
    EndFunc
            
    func Connect();Connect routine
        TCPStartup()
        $sock = TCPConnect(guictrlread($txtip),guictrlread($txtport))
    EndFunc
    
    func Disconnect();Disconnect routine
        TCPSend($sock,"disconnect")
        TCPCloseSocket($sock)
        TCPShutdown()
    EndFunc

Server:

#include <A3LScreenCap.au3>

Global $Connectedsock = -1
Global $sock = -1
Local $ip = @IPAddress1
Local $port = 1337

TCPStartup()
$sock = TCPListen($ip,$port);Wait for connection

while 1
    $recv = TCPRecv($connectedsock,100000000);Recive the data from the client
    
    if $recv = "disconnect" Then
        $connectedsock = -1
    EndIf
    
    if StringInStr($recv,"run") Then
        $temp = StringSplit($recv,":")
        Run($temp[2])
    EndIf
    
    if $recv = "desktop" Then
        _ScreenCap_Capture("pic.jpg")
        $file = FileOpen("pic.jpg",0)
        $size = FileGetSize("pic.jpg")
        TCPSend($connectedsock,"[start]")
        while $size > 0
            $fileread = FileRead($file,1)
            TCPSend($connectedsock,$fileread)
            $size = $size - 1
        WEnd
        TCPSend($connectedsock,"[end]")
        FileDelete("pic.jpg")
    EndIf
    
    if $connectedsock = -1 Then;Accept the connection
        $connectedsock = TCPAccept($sock)
        if $connectedsock <> - 1 Then
            TCPSend($connectedsock,"ac")
        EndIf
    EndIf
WEnd

Now time to refine it and make it faster.

Edited by deathkillspt
Link to comment
Share on other sites

  • 7 years later...

Pretty sure that whoever was needing that information won't need it 7 years later.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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