Jump to content

Problem sending image by TCP


Chapi
 Share

Recommended Posts

Hi, i am trying to send an image file over TCP and my code have some sort of problem or error...

If i test the server and the client in the same pc everything goes ok. But when I put the server in other pc of my lan the screen transfer doesn't work.

Here is the code for the transfer:

Server:

#include <GUIConstantsEx.au3>
#include <INet.au3>
#Include <ScreenCapture.au3>

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
    ; Set Some reusable info
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 3333
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv
    Local $CP, $HP
    Local $time = 0
    Local $iFileOp, $sFile, $sBuff, $iSendReturn
    Local $envio = 0

    ; Start The TCP Services
    ;==============================================
    TCPStartup()

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

    ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then 
        MsgBox(0, "Test", "Fallo")
        Exit
    EndIf

    ; Create a GUI for messages
    ;==============================================
    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180, 0x0800 + 0x00200000)
    GUISetState()


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


    ;Wait for and Accept a connection
    ;==============================================
    Do
        $msg = GUIGetMsg()
        $ConnectedSocket = TCPAccept($MainSocket)
    Until ($ConnectedSocket <> -1) Or ($msg = $GUI_EVENT_CLOSE)


    ; GUI Message Loop
    ;==============================================
    While 1
        $msg = GUIGetMsg()

        ; GUI Closed
        ;--------------------
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
            
        ; Try to receive (up to) 1024 bytes
        ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 1024)

        ; If the receive failed with @error then the socket has disconnected
        ;----------------------------------------------------------------
        If @error Then ExitLoop
            
        If $envio = 1 Then
            $envio = 0
            $sBuff = Binary(0)
            TCPSend($ConnectedSocket, "10001")
        EndIf

        ; Update the edit control with what we have received
        ;----------------------------------------------------------------
        If $recv <> "" Then
            GUICtrlSetData($edit, "- " & $recv & @CRLF & GUICtrlRead($edit))
            If $recv = "S328" Then
                TCPSend($ConnectedSocket, "File")
                _ScreenCapture_Capture(@MyDocumentsDir & "\Paulo.jpg", 0, 0, 348, 166)
                $sFile = @MyDocumentsDir & "\Paulo.jpg" 
                $iFileOp = FileOpen($sFile, 16)
                $sBuff = Binary(StringTrimLeft($sFile,StringInStr($sFile,"\",-1,-1))&",")&FileRead($iFileOp)
                FileClose($iFileOp)
                While BinaryLen($sBuff)
                    $iSendReturn = TCPSend($ConnectedSocket, $sBuff)
                    $sBuff = BinaryMid ($sBuff, $iSendReturn + 1, BinaryLen ($sBuff) - $iSendReturn)
                WEnd
                $envio = 1
            Else
                TCPSend($ConnectedSocket, $recv & " -Succes")
            EndIf
        EndIf
    WEnd


    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFuncoÝ÷ Ø)bz{ZºÚ"µÍÚ[ÛYH    ÑÕRPÛÛÝ[Ë]LÉÝÂ]]Ò]Ù]Ü[Û    ][ÝÓ]ÝXÛUÉ][ÝË
BÌÍÑÜLHHÕRPÜX]J   ][ÝÕÔHÛY[   ][ÝË
MÌJBÕRTÙ]ÐÛÛÜ
BÌÍÚ[XYÙHHÕRPÝÜX]TXÊØÜ    [È ][ÝÉÌLÜÚÚ[ËÉ][ÝËÌË
L

ÊBÌÍÚ[XYÙPÛÛHÕRPÝÜX]TXÊØÜ  [È ][ÝÉÌLØÛÛÛÛKÉ][ÝË

ÍMBÌÍÑY]HHÕRPÝÜX]QY]
    ][ÝÉ][ÝËÍK

KMË
È
BÌÍÒ[]HHÕRPÝÜX]R[]
    ][ÝÉ][ÝËÍKNMÎK
BÌÍÐ]ÛHHÕRPÝÜX]P]Û  ][ÝÔÙ[   ][ÝË
LÌNK
NJBÌÍØÛÛXÝYHÛØ[ ÌÍÜÛØÚÙ]ÛØ[    ÌÍÜÙ[ÛØ[  ÌÍÝ[YHHÛØ[ ÌÍÜÐY   ÌÍÜÕ    ÌÍÚQ[SÜ ÌÍØ]^ÛØ[   ÌÍÚQÝÚ[HHYBÛØ[   ÌÍÜÑ[S[YHHØÜ  [È ][ÝÉÌLØÛÛÛÛKÉ][ÝÂÝÙ^TÙ]
    ][ÝÊÞÑSTI][ÝË ][ÝÔÙ[]Ú]   ][ÝÊBÝÙ^TÙ]
    ][ÝÊÞÑ_I][ÝË  ][ÝÔÚÜÝ]ÌI][ÝÊBÝÙ^TÙ]
    ][ÝÊÞÑI][ÝË   ][ÝÔÚÜÝ]Ì][ÝÊHÝÙ^TÙ]
    ][ÝÊÞÑßI][ÝË ][ÝÔÚÜÝ]ÌÉ][ÝÊHÝÙ^TÙ]
    ][ÝÊÞÑI][ÝË   ][ÝÔÚÜÝ]Í ][ÝÊHÝÙ^TÙ]
    ][ÝÊÞÑ_I][ÝË  ][ÝÔÚÜÝ]ÍI][ÝÊHÝÙ^TÙ]
    ][ÝÊÞÑI][ÝË   ][ÝÔÚÜÝ]Í][ÝÊHÝÙ^TÙ]
    ][ÝÊÞÑßI][ÝË ][ÝÔÚÜÝ]ÍÉ][ÝÊHÝÙ^TÙ]
    ][ÝÊÞÑI][ÝË   ][ÝÔÚÜÝ]Î ][ÝÊHÝÙ^TÙ]
    ][ÝÊÞÑ_I][ÝË  ][ÝÔÚÜÝ]ÎI][ÝÊHÝÙ^TÙ]
    ][ÝÊÞÑLI][ÝË  ][ÝÔÚÜÝ]ÌL    ][ÝÊHÝÙ^TÙ]
    ][ÝÊÞÑL_I][ÝË ][ÝÔÚÜÝ]ÌLI][ÝÊHÝÙ^TÙ]
    ][ÝÊÉÌÌÎÞÑLI][ÝË  ][ÝÔÚÜÝ]ÌL][ÝÊHÝÙ^TÙ]
    ][ÝÉÌÌÎÜÉ][ÝË  ][ÝÔØÜY[][ÝÊHÝÙ^TÙ]
    ][ÝÉÌÌÎÞ][ÝË    ][ÝÕ[ ][ÝÊHÕRTÙ]Ý]JÕ×ÔÒÕÊBÕRPÝÙ]Ý]J    ÌÍÒ[]K   ÌÍÑÕRWÑÐÕTÊBÕRPÝÙ]]J ÌÍÚ[]K   ][ÝËØÛÛXÝNLMÌÌÌÉ][ÝÊBÔÝ

BÚ[HB  ÌÍÛÙÈHÕRQÙ]ÙÊ
B   ÌÍÜXÝHÔXÝ ÌÍÜÛØÚÙ]L
BÙ[XÝØÙH    ÌÍÛÙÈH ÌÍÑÕRWÑUSÐÓÔÑBY    ÌÍØÛÛXÝYHH[ÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÓYHZI][ÝÈ    [ÈÔBÔÛÜÙTÛØÚÙ]
    ÌÍÜÛØÚÙ]
BÔÚ]ÝÛ
B[Y^]ÛÜØÙH  ÌÍÛÙÈH ÌÍÐ]ÛBÙ[]Ú]

BØÙH[ÙBY ÌÍÜXÝ   ÉÝÈ  ][ÝÉ][ÝÈ[BBBRY
    ÌÍÜXÝH  ][ÝÐYÚ]I][ÝÊH[
[YY ÌÍÝ[YJH  ÝÈÌ
H[BBBBTÛÝ[^H
ØÜ    [È ][ÝÉÌLÐYÚ]KÉ][ÝÊBBBBBIÌÍÝ[YHH[Y[]

BBBBQ[ÙRY
    ÌÍÜXÝH  ][ÝÑ[I][ÝÊH[BBBBPÚ]Ê
BBBBQ[ÙRY
    ÌÍÜXÝ   ÉÝÈ  ][ÝÐYÚ]I][ÝÊH[
    ÌÍÜXÝ   ÉÝÈ  ][ÝÑ[I][ÝÊH[BBBBQÕRPÝÙ]]J    ÌÍÑY]K   ][ÝËH ][ÝÈ  [È ÌÍÜXÝ   [ÈÔ   [ÈÕRPÝXY
    ÌÍÑY]JJBBBBQ[Y[YBQ[Ù[XÝÑ[^][ÈÚ]Ê
BQÕRPÝÙ]]J   ÌÍÑY]K   ][ÝËH ][ÝÈ  [È ÌÍÜXÝ   [ÈÔ   [ÈÕRPÝXY
    ÌÍÑY]JJBIÌÍÜÐYH[H
    ÌÍÜÐYBUÚ[H ÌÍÜXÝ   ÉÝÈ  ][ÝÌLI][ÝÂBIÌÍØ]^HÔXÝ  ÌÍÜÛØÚÙ]

BBIÌÍÜXÝH[UÔÝ[È
    ÌÍØ]^
BBRYÝ[Ò[Ý    ÌÍÜXÝ   ÌÎNË ÌÎNÊH[   ÌÍÚQÝÚ[H[BBIÌÍÜÕHÝ[ÓY
    ÌÍÜXÝÝ[Ò[Ý   ÌÍÜXÝ   ][ÝË  ][ÝÊHHJBBBIÌÍÜXÝHÝ[Õ[SY
    ÌÍÜXÝÝ[Ó[ ÌÍÜÕ
H
ÈJBBBRYÝ[Ó[  ÌÍÜÑ[S[YJH  ÈH[    ÌÍÜÑ[S[YHH  ÌÍÜÕBBIÌÍÚQÝÚ[HH[ÙBBQ[YBIÌÍÜÐY    [ÏH    ÌÍÜXÝBRYÜ[^]ÛÜUÑ[QÕRPÝÙ]]J   ÌÍÑY]K   ][ÝËH[H[  ][ÝÈ  [ÈÔ   [ÈÕRPÝXY
    ÌÍÑY]JJBIÌÍØ]^H   ][ÝÌ  ][ÝÂBBIÌÍÚQÝÚ[HHYBBQØÜXÚ]Ê
BIÌÍÜÐYH    ][ÝÉ][ÝÂ[[Â[ÈØÜXÚ]Ê
BZY[Q^ÝÊ  ÌÍÜÑ[[[YJH[BQ[Q[]J  ÌÍÜÑ[[[YJBBZY   ÌÍÜÑ[[[YHHØÜ  [È ][ÝÉÌLØÛÛÛÛP[É][ÝÈ[BBIÌÍÜÑ[[[YHHØÜ [È ][ÝÉÌLØÛÛÛÛKÉ][ÝÂBY[ÙHBBIÌÍÜÑ[[[YHHØÜ [È ][ÝÉÌLØÛÛÛÛP[É][ÝÂBQ[YQ[YIÌÍÚQ[SÜH[SÜ[  ÌÍÜÑ[S[YKM
ÈBQ[UÜ]J  ÌÍÚQ[SÜ ÌÍÜÐYBQ[PÛÜÙJ    ÌÍÚQ[SÜ
BQÕRPÝÙ][XYÙJ   ÌÍÚ[XYÙPÛÛ    ÌÍÜÑ[S[YJB[[Â[ÈÙ[]Ú]

BYÚ[XÝ]J  ][ÝÕÔHÛY[   ][ÝÊH[    ÌÍÜÙ[HÕRPÝXY
    ÌÍÒ[]JBBRY
Ý[ÓY
    ÌÍÜÙ[JHH    ÌÎNÉ][ÝÉÌÎNÊH[BBIÌÍÜÙ[H ÌÎNÉ][ÝÉÌÎNÈ    [È ][ÝÞÑÕÓI][ÝÈ [ÈÝ[ÓZY
    ÌÍÜÙ[BBQ[YÕRPÝÙ]]J   ÌÍÒ[]K   ][ÝÉ][ÝÊBY  ÌÍÜÙ[   ÉÝÈ  ][ÝÉ][ÝÈ[Y  ÌÍØÛÛXÝYHH[BBBUÔÙ[
    ÌÍÜÛØÚÙ] ÌÍÜÙ[   [È ][ÝÞÑ[I][ÝÊB[ÙBYÝ[Ò[Ý  ÌÍÜÙ[   ][ÝËØÛÛXÝ ][ÝÊH[    ÌÍÜÙHÝ[ÔÜ]
    ÌÍÜÙ[   ][ÝÈ  ][ÝÊB ÌÍÜÙÜ]HÝ[ÔÜ]
    ÌÍÜÙÌK ][ÝÎ][ÝÊB   ÌÍÚHÔ[YUÒT
    ÌÍÜÙÜ]ÌWJB    ÌÍÜÜH   ÌÍÜÙÜ]ÌB  ÌÍÜÛØÚÙ]HÔÛÛXÝ
    ÌÍÚ  ÌÍÜÜ
BY  ÌÍÜÛØÚÙ]HLH[ÕRPÝÙ]]J  ÌÍÑY]K   ][ÝÓÈÙHYÈÝXXÙHÛÛ^[Û   ][ÝÈ  [ÈÔB  ÌÍØÛÛXÝYH[ÙBÕRPÝÙ]]J  ÌÍÑY]K   ][ÝÐÛÛ^[ÛÝXXÚYH  ][ÝÈ  [ÈÕT  [È ][ÝÎ][ÝÈ    [ÈRS   [ÈÔB  ÌÍØÛÛXÝYHBßÙ[]ÛÛXÝ

B[Y[ÙBÙÐÞ
    ][ÝÕÔHÛY[   ][ÝË  ][ÝÓÝÛÛXÝY][ÝÊB[Y[Y[Y[ÙB[Y[[ÈÏOIÝÔÙ[]Ú][ÈÙ[]ÛÛXÝ

BÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÔÝÙÛ][ÝÊB[[ÈÏOIÝÔÙ[]ÛÛXÝ[ÈÚÜÝ]ÌJ
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑ_I][ÝÊB[[Â[ÈÚÜÝ]Ì
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑI][ÝÊB[[Â[ÈÚÜÝ]ÌÊ
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑßI][ÝÊB[[Â[ÈÚÜÝ]Í

BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑI][ÝÊB[[Â[ÈÚÜÝ]ÍJ
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑ_I][ÝÊB[[Â[ÈÚÜÝ]Í
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑI][ÝÊB[[Â[ÈÚÜÝ]ÍÊ
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑßI][ÝÊB[[Â[ÈÚÜÝ]Î

BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑI][ÝÊB[[Â[ÈÚÜÝ]ÎJ
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑ_I][ÝÊB[[Â[ÈÚÜÝ]ÌL

BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑLI][ÝÊB[[Â[ÈÚÜÝ]ÌLJ
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑL_I][ÝÊB[[Â[ÈÚÜÝ]ÌL
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÞÑLI][ÝÊB[[Â[ÈØÜY[
BUÔÙ[
    ÌÍÜÛØÚÙ] ][ÝÔÌÌ  ][ÝÊB[[Â[È[

BQÕRPÝÙ][XYÙJ   ÌÍÚ[XYÙPÛÛÚÝÜ  [È ][ÝÉÌLØÛÛÛÛLKÉ][ÝÊB[[

When server receiv "S328" init the file transfer.

The main function of receive the file is in the clientnt "Archivo()" function...

Any help is welcome...

Sorry for my bad english

Edited by Chapi
Link to comment
Share on other sites

Are you sure the port is open/unblocked on the PC and that it's allowed through your firewall?

The conection goes fine...

I can stablish comunication and send text betwen server and client.

The problem is whit the file transfer...

Link to comment
Share on other sites

Perhaps the image is getting some kind of connection timeout? I haven't looked very closely at the script so my guesses are just general problems that could happen with anyone.

Link to comment
Share on other sites

Perhaps the image is getting some kind of connection timeout? I haven't looked very closely at the script so my guesses are just general problems that could happen with anyone.

Look, this is the important part in the client...

Func Archivo()
    GUICtrlSetData($Edit1, "- " & $recv & @CRLF & GUICtrlRead($Edit1))
    $sBuff = Binary ($sBuff)
    While $recv <> "10001"
        $aux = TCPRecv($socket, 2048)
        $recv = BinaryToString ($aux)
        If StringInStr($recv, ',') And $iFirstWhile Then
            $sTmp = StringLeft($recv, StringInStr($recv, ",") - 1)
            $recv = StringTrimLeft($recv, StringLen($sTmp) + 1)
            If StringLen($sFileName) < 1 Then $sFileName = $sTmp
            $iFirstWhile = False
        EndIf
        $sBuff &= $recv
        If @error Then ExitLoop
    WEnd
    GUICtrlSetData($Edit1, "- File End" & @CRLF & GUICtrlRead($Edit1))
    $aux = "0"   
    $iFirstWhile = True 
    EscribirArchivo()
    $sBuff = ""
EndFunc


Func EscribirArchivo()
    if FileExists($sFilename) Then
        FileDelete($sFilename)
        if $sFilename = @ScriptDir & "\consolaAlt.jpg" Then
            $sFilename = @ScriptDir & "\consola.jpg"
        else
            $sFilename = @ScriptDir & "\consolaAlt.jpg"
        EndIf
    EndIf
    $iFileOp = FileOpen($sFileName, 16 + 2)
    FileWrite($iFileOp, $sBuff)
    FileClose($iFileOp)
    GUICtrlSetImage($imageCon, $sFileName)
EndFunc

When client receive the string "File" call the function "Archivo()"

When server finish of sending the Screen send the string "10001" to thell the client that the screen transfer is finished.

It seems that the string "10001" is never returned by the server, beacause if i print the info in a EditControl its look ok...

I hava a hotkey to activate the screen transfer, if i pushit several times once in a while the image is transfered...

Onece again, sorry for my poor english...

Link to comment
Share on other sites

I initially tried what you are doing by sending a string of "file transfer complete" but that would often fail hence why in the functions I posted I send the filesize in the beginning and let each of the programs do the math to figure out when the file transfer is over themselves. I also think you should add a sleep in your server when sending the pictures.

Edited by SoulA
Link to comment
Share on other sites

I initially tried what you are doing by sending a string of "file transfer complete" but that would often fail hence why in the functions I posted I send the filesize in the beginning and let each of the programs do the math to figure out when the file transfer is over themselves. I also think you should add a sleep in your server when sending the pictures.

Thx SoulA, your code works really fine...

Edited by Chapi
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...