Sign in to follow this  
Followers 0
andyvl

tcp client server model

4 posts in this topic

#1 ·  Posted (edited)

OK, a transfer over data over LAN now.

A computer is keeping a log (encrypted ini file) and screenshots locally.

But at a randomly period (aprox. every minute) these files need to be send to a location on the LAN

(to a place were the user itself doesn't have access to. so I need to pass along a username and password)

I found this bellow code and is is certainly helpfull for the screenshots. But what do i need to adjust for the authenication?

and what is the best way to do this for the ini?

** edit

code was not working. found better code bellow.

please see bellow post for the question

Edited by andyvl

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

OK. bellow some code I found. and this is doing exactly what I need... almost

I am on the road alot. So I often don't have a network connection available. So I need to be able to test this local also (so both client as server need to run often on same pc during the development of the project )

When I send a smile file (ini), it sends and receives great.

When I send a larger file (a bmp of 3MB) it doesn't work. I got a msg that I am sending (and receiving), but after 5 bytes it hangs.

I admit I'm not fully into the sockethandling.

So I was wondering if somebody can help me out to help me adjust the above code with a few (I think small) adjustments.

So first of all, why does a small ini file 3kb work excellen. and a bmp is stuck after the 5 first bytes. (is this cause the way it is send? images maybe need to be send binary)

seconf of all, I noticed when I send a ini file twice, he just put the new part under the old one.

Since in the environement, the same filename only means changes, the file can be overwritten without being asked.

So can somebody please help me out on this changes.

(and if possible, please explain the meaning of the changes so I understand and learn)

Here is a file receiver... start it on a computer somewhere...

; BINARY FILE TEST RECEIVER

Global Const $TCP_PORT = 3333

Dim $nSocketRecv = -1
Dim $bWriting = 0
Dim $szRecvBuffer = ""
Dim $nMainSocket = -1
Dim $szFileWrite = ""
Dim $nFileBytes = 0

TCPStartup()

$nMainSocket = TCPListen( @IPAddress1, $TCP_PORT )

While $nSocketRecv = -1
    ToolTip( "waiting for incoming file...", 0, 0 )
    $nSocketRecv = TCPAccept( $nMainSocket )
WEnd

ToolTip("Receiving file...")

While 1
    $szRecvBuffer &= TCPRecv( $nSocketRecv, 2048 )
    If @error Then ExitLoop
    If StringLen($szRecvBuffer) Then
        If $bWriting = 0 Then
            If StringInStr($szRecvBuffer,",") Then
                $bWriting = 1
                $szFileWrite = StringLeft($szRecvBuffer,StringInStr($szRecvBuffer,",")-1)
                $szRecvBuffer = StringTrimLeft($szRecvBuffer,StringLen($szFileWrite)+1)
            EndIf
        EndIf
        If $bWriting = 1 Then
            If StringInStr($szRecvBuffer,",") Then
                $bWriting = 2
                $nFileBytes = StringLeft($szRecvBuffer,StringInStr($szRecvBuffer,",")-1)
                $szRecvBuffer = StringTrimLeft($szRecvBuffer,StringLen($nFileBytes)+1)
                $nFileBytes = Number($nFileBytes)
            EndIf
        EndIf
        If $bWriting = 2 Then
            If StringLen($szRecvBuffer) Then
                If StringLen($szRecvBuffer) >= $nFileBytes Then
                    FileWrite($szFileWrite,StringLeft($szRecvBuffer,$nFileBytes))
                    ExitLoop
                Else
                    FileWrite($szFileWrite,$szRecvBuffer)
                    $nFileBytes -= StringLen($szRecvBuffer)
                    $szRecvBuffer = ""
                EndIf
            EndIf
        EndIf
    EndIf
WEnd

TCPSend($nSocketRecv,"go away")

ToolTip("")
MsgBox(4096,"","done with " & @ScriptDir & "\" & $szFileWrite)

here is a file sender... start it on a different computer that is on the same network... pick a file... and enter the name or IP of the other PC that is waiting for the file... if you enter a computer name... remember to prefix with a \ so I know it is a name...

; BINARY FILE TEST SENDER

Global Const $TCP_PORT = 3333

Dim $nSocketSend = -1
Dim $file = ""

TCPStartup()

$file = FileOpenDialog("File To Send","","All Files (*.*)")
If @error Then Exit

$pc = InputBox("",@LF & @LF & "Computer to receive file." & @LF & "(Prefix with \ if computer name.)")
If $pc = "" Then Exit
If StringLeft($pc,1) = "\" Then
    $pc = TCPNameToIP(StringTrimLeft($pc,1))
EndIf

ToolTip("Sending file...")

$socket = TCPConnect($pc,$TCP_PORT)
If $socket = -1 Then Exit

$n = FileGetSize($file)

$buffer = StringTrimLeft($file,StringInStr($file,"\",0,-1)) & "," & $n & "," & FileRead($file)

While StringLen($buffer)
    $x = TCPSend($socket,$buffer)
    If @error Then ExitLoop
    $n -= $x
    $buffer = StringTrimLeft($buffer,$x)
WEnd

While 1
    If TCPRecv($socket,2048) <> "" Then ExitLoop
    If @error Then ExitLoop
WEnd
    
MsgBox(4096,"","Finished Sending " & $file)
Edited by andyvl

Share this post


Link to post
Share on other sites

hmm, my buffer seems to be the problem. but I don't know how to fix this.

When I put checks (valueoutputs) on this part of the code

$n = FileGetSize($file)

$buffer = StringTrimLeft($file,StringInStr($file,"\",0,-1)) & "," & $n & "," & FileRead($file)

While StringLen($buffer)
    $x = TCPSend($socket,$buffer)
    If @error Then ExitLoop
    $n -= $x
    $buffer = StringTrimLeft($buffer,$x)

I got for $n a value of 3072054

when I check the size of my buffer before I go into the loop it's 60

x send more then 60 bytes so I am almost after first run through the loop.

What's wrong?

Share this post


Link to post
Share on other sites

please help. If I get this to work, I can do the rest of my project.

But this is the foundation and I'm stuck.

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  
Followers 0