Jump to content

TCP Send and Receive


AlmarM
 Share

Recommended Posts

Hi,

I tried some stuff with the TCP send and receive functions and I cant seem to get it right.

What I want is:

When the user starts "Client 1.au3" (or Client 2.au3) the scripts takes a screenshot, sends it to the Server.

The server will send the screenshot to the other connected client, not the one it received it from.

The other connected client has to save the screenshot to the @DesktopDir.

Now heres my script:

Server.au3

#include "TCP.au3"

$hServer = _TCP_Server_Create(1337, @IPAddress1)

ToolTip("SERVER: Server Created.", 0, 0)
 
_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient")
_TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "Disconnect")
_TCP_RegisterEvent($hServer, $TCP_RECEIVE, "Received")
 
While 1
    
WEnd
 
Func NewClient($hSocket, $iError)
    ToolTip("SERVER: Client connected.", 0, 0)
EndFunc
 
Func Disconnect($hSocket, $iError)
    ToolTip("SERVER: Client disconnected.", 0, 0)
EndFunc

Func Received($hSocket, $sReceived, $iError)
    _TCP_Server_Broadcast($sReceived)
EndFunc

Client 1.au3

#include "TCP.au3"
#include <ScreenCapture.au3>
#include <Array.au3>

HotKeySet("{HOME}", "_Send")

Global $hClient = _TCP_Client_Create(@IPAddress1, 1337)

ToolTip("CLIENT1: Connected.", 0, 30)

_TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received")
_TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected")

While 1
    
WEnd

Func Received($hSocket, $sReceived, $iError)
    $Split = StringSplit($sReceived, "|")
    FileWrite($Split[1], $Split[2])
EndFunc

Func Disconnect($hSocket, $iError)
EndFunc

Func _Send()
    $___FileName = RandomName(10) & ".jpg"
    $___Path = @MyDocumentsDir & "\" & $___FileName
    _ScreenCapture_Capture($___Path)
    $__Read = FileRead($___Path)
    _TCP_Send($hClient, @DesktopDir & "\" & $___FileName & "|" & $__Read)
EndFunc

Func RandomName($nChar)
    $___Array = StringSplit("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789", "")
   
    $___Name = ""
    For $X = 1 to $nChar
        $___Name &= $___Array[Random (1, $___Array[0], 1)]
    Next
    Return $___Name
EndFunc

Client 2.au3

#include "TCP.au3"

$hClient = _TCP_Client_Create(@IPAddress1, 1337)

ToolTip("CLIENT2: Connected.", 0, 60)

_TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received")
_TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected")

While 1
    
WEnd

Func Received($hSocket, $sReceived, $iError)
    $MsgBox = MsgBox(4, "Data Received", "Data received!" & @CRLF & "Save?")
EndFunc

Func Disconnect($hSocket, $iError)
EndFunc

Help, tips, examples, antthing would be much appreciated! :)

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

You should also look at making just 1 client, unless you only need 2. If you eventually want to be able to have a server with 50 clients say, you don't want 50 different client files (or do you?). Unless each one does something different, you may want to look at CodyBarett's Multiclient TCP example. It helped me alot.

Link to comment
Share on other sites

Thank you Darkjohn

but my TCP example doesn't exactly apply to File Sending... you would need to send the Binary or the Pic in Chunks of say.. 50bytes per chunk. and then reassemble the datastructure afterwards.

Link to comment
Share on other sites

Oooh, thanks guys! I forgot I had this topic!

if you try to send a File over TCP, you should search for FileOverTCP in this forum

Ah yes, ill take a look soon :)

You should also look at making just 1 client, unless you only need 2. If you eventually want to be able to have a server with 50 clients say, you don't want 50 different client files (or do you?). Unless each one does something different, you may want to look at CodyBarett's Multiclient TCP example. It helped me alot.

I discovered that ;)

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

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