Jump to content
Sign in to follow this  
dburner

Sending JPEG using TCP

Recommended Posts

Why don't you use ftp protocol dburner?

If you have opened a socket and you are sending and receiving strings on it from client to server, and you want to send binary data, create a simple protocol, for example like this:

CLIENT: Send string "<SNDIMAGE:12345>" where 12345 is the size of the file you want to send

SERVER: When the server receives the string "<SNDIMAGE:" it captures the file size that will receive in binary data format.

CLIENT: Open file for reading and send binary data of the image file

SERVER: Stop parsing client strings received. Open file for writing, receive binary information from client and write that information to the file openend, controlling number of bytes received from the client.

SERVER: when number of bytes received from the client are the total indicated in the expresion <SNDIMAGE:XXXXX>, write pending bytes to the file opened fro writing, close that file and continue parsing client strings.

You can implement a simple protocol like this:

<STR:XXX>text

<SNDIMAGE:XXXXXX>binarydata

where XXX and XXXXXX is the size of the data sended.

See you!

Share this post


Link to post
Share on other sites

Well im trying to find the fastest solution to get the image to the server thats why im not using ftp controls.

One more question. Do you know any way to reduce the image size?(reduceing the image quality, or by resizing)

Share this post


Link to post
Share on other sites

I don't know how to reduce image quality or resize using autoit, but i think that there are utilities to do it.

I use batch options of InfanView (http://www.irfanview.com/) for doing that things. You can call that batch functions from autoit and convert/resize/resample an image. It's fast and you have very much options.

Share this post


Link to post
Share on other sites

I don't know how to reduce image quality or resize using autoit, but i think that there are utilities to do it.

I use batch options of InfanView (http://www.irfanview.com/) for doing that things. You can call that batch functions from autoit and convert/resize/resample an image. It's fast and you have very much options.

Try GDIPlus functions. Not sure if any functions are in there to do that.

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

It looks like i cant get this done :) . Can somone explain me what am i doing wrong?

File Send function

Func FileSend($sFile, $IP, $PORT = 4324, $iWaitWhileConnected = 1)
    Local $conection = -1, $sBuff, $iFileOp,$sRecv
    
    
    TCPStartup()
    
    If $iWaitWhileConnected = 1 Then
        While $conection  = -1
            $conection  = TCPConnect($IP, $PORT)
        WEnd
    Else
        $conection = TCPConnect($IP, $PORT)
        If @error Then Return SetError(2, 0, -1)
    EndIf
;TrayTip("connected", "now sending file", 5)
    $iFileOp = FileOpen($sFile, 16)
    $sBuff =FileRead($iFileOp)
    FileClose($iFileOp)
    TCPSend($conection, "FILESIZE:" & BinaryLen($sBuff))
    Sleep(20)
    While BinaryLen($sBuff)
        $iSendReturn = TCPSend($iMainSocket, $sBuff)
        $sBuff = BinaryMid ($sBuff, $iSendReturn + 1, BinaryLen ($sBuff) - $iSendReturn)
    WEnd
    Sleep(20)
    TCPSend($conection, "DONE")
    Sleep(20)
    $filename=StringSplit($sFile, "\")
    $filen=$filename[$filename[0]]
    TCPSend($conection, "FILENAME:" & $filen)
;TrayTip("file Sent", "your file was sent" , 5)
    TCPCloseSocket($conection)
    TCPShutdown()
EndFunc

File Receive function

Func ReceiveFile($IP = @IPAddress1, $PORT = 4324)
    Local $conection=-1
    Local $buffer=""
    Local $recv=""
    
    TCPStartup()
    $MainSocket = TCPListen($IP, $PORT)
        
    Do
        $conection = TCPAccept($MainSocket); Waiting for connection
    Until $conection <> -1
    
    While $recv = ""
        $recv=TCPRecv($conection, 1024)
    WEnd
;Recive file size in formtat FILESIZE:
    If StringLeft($recv, 9)="FILESIZE:" Then
        $fSize=StringTrimLeft($recv, 9)     
    EndIf
    
    TrayTip("Receving", "Now Receving file", 5)
    $buffer=Binary($buffer)
    
    TrayTip("Client connected", "Receving file", 5)
    
    While $fSize<>BinaryLen($buffer)
        $recv=TCPRecv($conection, 2048, 1)
;If BinaryToString($recv)="DONE" Then ExitLoop
        $buffer=$buffer & $recv
    WEnd
    TrayTip("" ,"File send. Writing...", 5)
    While $recv = ""
        $recv=TCPRecv($conection, 1024)
    WEnd
;Recive file name in formtat FILENAME:
    $filename=@YDAY & "-" & @MON & "-" & @YEAR & "-" & @HOUR & ":" & @MIN & ".jpg"
    If StringLeft($recv, 9)="FILENAME:" Then $filename=StringTrimLeft($recv, 9)
    If FileExists(@ScriptDir & "\Imagini Primite\" & $filename) Then
        $k=0
        $tmp=$filename
        While FileExists(@ScriptDir & "\Imagini Primite\" & $tmp)
            $k=$k+1
            $tmp=$filename & "(" & $k & ")"
        WEnd
        $filename=$tmp
    EndIf
    TrayTip("Saving", "Saving file", 5)
    $fHandle=FileOpen(@ScriptDir & "\Imagini Primite\" & $filename, 16+2)
    FileWrite($fHandle, $buffer)
    FileClose($fHandle)
    TCPCloseSocket($conection)
    TCPShutdown()
EndFunc
Edited by dburner

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  

×
×
  • Create New...