Jump to content

Screanshoot transfer via TCP


Recommended Posts

Main problem. Almost everyting working now. Neead some lines to add.

But main problem is...

When transfering img from client to server it dosent send all data and image is not full.....

Server side

;SERVER!! Start Me First !!!!!!!!!!!!!!!
#include <GUIConstants.au3>

Func SocketToIP($SHOCKET)
    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

    Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
            "ptr",DLLStructGetPtr($sockaddr),"int_ptr",DLLStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc



; Set Some reusable info
; Set your Public IP address (@IPAddress1) here.
Dim $szIPADDRESS = @IPAddress1
Dim $nPORT = 33891
Dim $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")",300,200)
Dim $edit = GUICtrlCreateEdit("",10,10,280,180)


while 1
         $msg = GUIGetMsg()
; Start The TCP Services
;==============================================
TCPStartUp()

; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)

; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit


; Create a GUI for messages
;==============================================

GUISetState()


; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1


;Wait for and Accept a connection
;==============================================
GUICtrlSetData($edit, "Server: "& "waiting for connection....")
Do
    $ConnectedSocket = TCPAccept($MainSocket)
    
    $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then exit
Until $ConnectedSocket <> -1


; Get IP of client connecting
Dim $szIP_Accepted = SocketToIP($ConnectedSocket)
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"Server: "& $szIP_Accepted & " connecting")
Dim $msg, $recv, $no, $dataf 
; GUI Message Loop
;==============================================
$filed = $szIP_Accepted &" " & @YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &"_"& @MIN &"_"& @SEC&".jpg"




$file = FileOpen($filed  , 2)
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF & "Server: Requesting img from "& $szIP_Accepted)
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"Server: Recyving img from "& $szIP_Accepted)
While 1
   $msg = GUIGetMsg()

; GUI Closed
;--------------------
    If $msg = $GUI_EVENT_CLOSE Then exit

; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------

$recv = TCPRecv( $ConnectedSocket, 2048 )
    
; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
    If @error Then ExitLoop

; Update the edit control with what we have received
;----------------------------------------------------------------

If $recv <> "" Then
FileWrite($filed, $recv)

    
    EndIf       
WEnd
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"Server: Recyving img from "& $szIP_Accepted & " done!")
If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket )


FileClose($file)
TCPShutDown()
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"Server: Closing TCP conn and saving image to "& $filed)

$msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then exit
    WEnd

Clent side

;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
; see TCPRecv example
#include <GUIConstants.au3>
#include <A3LScreenCap.au3>
; Start The TCP Services
;==============================================



while 1
    Sleep ( 5000)
TCPStartUp()
; Set Some reusable info
;--------------------------
Dim $szServerPC = @ComputerName
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
Dim $szIPADDRESS = TCPNameToIP($szServerPC)
Dim $nPORT = 33891


; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1

;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)


Dim $szData = -1

; If there is an error... show it
If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
;   to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
_ScreenCap_SetJPGQuality(50)
$file = "c:\" & Random(0,9999, 1) & ".jpg"
_ScreenCap_Capture($file)
FileOpen($file, 0)
$sizee = FileGetSize($file)
$filedata = FileRead($file)
FileClose($file)


   ; InputBox for data to transmit
    



        $szData = $filedata
        Sleep(3000);
   ; If they cancel the InputBox or leave it blank we exit our forever loop

        
   ; We should have data in $szData... lets attempt to send it through our connected socket.
        TCPSend($ConnectedSocket,$szData)
        
   ; If the send failed with @error then the socket has disconnected
   ;----------------------------------------------------------------


EndIf
TCPShutDown()
wend

why its send not full images?

post-3048-1191330929_thumb.jpg

... :/ Client or server side problem?

Link to comment
Share on other sites

Does that clipped image represent 2K (2,048) bytes of data?

The TCPRecv() looks like it loops to receive additional blocks of 2KB, but the TCPSend() attempts to send all in one block. Should TCPSend() be sending multiple 2KB blocks?

:)

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 that clipped image represent 2K (2,048) bytes of data?

The TCPRecv() looks like it loops to receive additional blocks of 2KB, but the TCPSend() attempts to send all in one block. Should TCPSend() be sending multiple 2KB blocks?

:)

Server:

Thanks for response ;P working!!!

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=Beta
#AutoIt3Wrapper_UseAnsi=y
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
;SERVER!! Start Me First !!!!!!!!!!!!!!!
#include <GUIConstants.au3>

Func SocketToIP($SHOCKET)
    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

    Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
            "ptr",DLLStructGetPtr($sockaddr),"int_ptr",DLLStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc



; Set Some reusable info
; Set your Public IP address (@IPAddress1) here.
Dim $szIPADDRESS = TCPNameToIP("localhost")
Dim $nPORT = 33891
Dim $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")",300,200)
Dim $edit = GUICtrlCreateEdit("",10,10,280,180)

GUISetState()
while 1
         $msg = GUIGetMsg()
; Start The TCP Services
;==============================================
TCPStartUp()

; Create a Listening "SOCKET".
;   Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)

; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit


; Create a GUI for messages
;==============================================




; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1


;Wait for and Accept a connection
;==============================================
GUICtrlSetData($edit, "Server: "& "waiting for connection....")
Do
    $ConnectedSocket = TCPAccept($MainSocket)
    
    $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then exit
Until $ConnectedSocket <> -1


; Get IP of client connecting
Dim $szIP_Accepted = SocketToIP($ConnectedSocket)
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF &"Server: "& $szIP_Accepted & " connecting")
Dim $msg, $recv, $no, $dataf 
; GUI Message Loop
;==============================================
$filed = $szIP_Accepted &" " & @YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &"_"& @MIN &"_"& @SEC&".jpg"




$file = FileOpen($filed  , 2)
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF & "[" & @YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &"_"& @MIN &"_"& @SEC&"] Server: Requesting img from "& $szIP_Accepted)
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF & "[" & @YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &"_"& @MIN &"_"& @SEC&"] Server: Recyving img from "& $szIP_Accepted)
While 1
   $msg = GUIGetMsg()

; GUI Closed
;--------------------
    If $msg = $GUI_EVENT_CLOSE Then exit

; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------

$recv = TCPRecv( $ConnectedSocket, 2048 )
    
; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
    If @error Then ExitLoop

; Update the edit control with what we have received
;----------------------------------------------------------------

If $recv <> "" Then
FileWrite($filed, $recv)

    
    EndIf       
WEnd
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF & "[" & @YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &"_"& @MIN &"_"& @SEC&"] Server: Recyving img from "& $szIP_Accepted & " done!")
If $ConnectedSocket <> -1 Then TCPCloseSocket( $ConnectedSocket )


FileClose($file)
TCPShutDown()
GUICtrlSetData($edit, GUICtrlRead($edit) & @CRLF & "[" & @YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &"_"& @MIN &"_"& @SEC&"] Server: Closing TCP conn and saving image to "& $filed)

$msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then exit
    WEnd

Cliend side

;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
; see TCPRecv example
#include <GUIConstants.au3>
#include <A3LScreenCap.au3>
; Start The TCP Services
;==============================================



while 1
    Sleep ( 5000)
TCPStartUp()
; Set Some reusable info
;--------------------------
Dim $szServerPC = "localhost"
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
Dim $szIPADDRESS = TCPNameToIP($szServerPC)
Dim $nPORT = 33891


; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1

;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)


Dim $szData = -1

; If there is an error... show it
If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
;   to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
_ScreenCap_SetJPGQuality(50)
$file = "c:\" & Random(0,9999, 1) & ".jpg"
_ScreenCap_Capture($file)
$sizee = FileGetSize($file)


FileOpen($file, 1)
$chars = FileRead($file)
$lengt = StringLen($chars)
$n = 0
$byte_in_packet = 4*2*2*2*2*2*2*2*2 ;1024 bytes
While $lengt >= $n 
$n = $n + $byte_in_packet
$tosend = StringLeft($chars, $byte_in_packet)
$chars = StringTrimLeft($chars, $byte_in_packet)
TCPSend($ConnectedSocket,$tosend)
WEnd



FileClose($file)


EndIf
TCPShutDown()
Sleep(60000*3);
wend

Just fresh..... from SciTE workpad ;) Have a fun with this

Link to comment
Share on other sites

Thanks for response ;P working!!!

Glad it helped. I didn't run the code and wasn't sure that was the problem.

:)

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

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