Jump to content
Sign in to follow this  

Server and Client TCP Help(Updated)

Recommended Posts

*Please Do not forget to read updated information at bottom of edited post and or edited Autoit code, thank you*

Hi everyone, I was working on a Server and Client program, not in the most complex way but only for simple Server and Client related automation. I already started working on it but I am now stuck while looking at the TCPRecv in the AutoIt Help file. I think I need to still modify my current coding in both the Server and Client Programs. Could you guys give me a little hand here or steer me into the right direction?

Updated/Current Server Code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)
HotKeySet("{F10}", "Leave") ; Set HotKey

Func Leave()
;SERVER!! Start Me First !!!!!!!!!!!!!!!


Func Example()
    ; Set Some reusable info
    ; Set your Public IP address (@IPAddress1) here.
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 80
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv, $Button1, $Button2

    ; Start The TCP Services

    ; 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
    #Region ### START Koda GUI section ### Form=
    $GOOEY = GUICreate("Server", 625, 445, 192, 124)
    $edit = GUICtrlCreateEdit("", 8, 8, 601, 385, BitOR($ES_AUTOVSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
    $Button1 = GUICtrlCreateButton("Start Server", 8, 400, 121, 33, $WS_GROUP)
    $Button2 = GUICtrlCreateButton("Close Server", 487, 400, 121, 33, $WS_GROUP)
    #EndRegion ### END Koda GUI section ###

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

    ;Wait for and Accept a connection
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1

    ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

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

        ; GUI Closed
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop

        ; 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 GUICtrlSetData($edit, _
                $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
            Case $recv = "/notepad"

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

EndFunc   ;==>Example

; Function to return IP Address from a connected socket.
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", 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]
        $aRet = 0

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP

Updated/Current Client Code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Client", 401, 301, 258, 126)
$Button1 = GUICtrlCreateButton("Connect", 8, 256, 89, 33, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Disconnect", 282, 256, 89, 33, $WS_GROUP)
$Input1 = GUICtrlCreateInput("Text", 8, 216, 377, 21)
GUICtrlSetData(-1, "Edit1")
$Button3 = GUICtrlCreateButton("Send", 146, 258, 89, 33, $WS_GROUP)
$Input2 = GUICtrlCreateInput("Ip Address to Connect to", 232, 8, 153, 21)
#EndRegion ### END Koda GUI section ###
$g_IP = "ip address of host/server"
$szData = ""

While 1

$nMsg = GUIGetMsg()
        Case $nMsg = $GUI_EVENT_CLOSE
        Case $nMsg = $Button1
            GUICtrlSetData($Edit1, @CRLF&"Starting TCP Services", 1)
            ; Start The TCP Services
            $socket = TCPConnect( $g_IP, 80 )
            GUICtrlSetData($Edit1, @CRLF&"Connected", 1)
            TCPSend($Socket, "Client: Connected")

        Case $nMsg = $Button2
            GUICtrlSetData($Edit1, @CRLF&"Shutting Client Down", 1)
            GUICtrlSetData($Edit1, @CRLF&"TCP Shut Down", 1)
        Case $nMsg = $Button3
            $RData = GUICtrlRead($Input1)
            $szData = $RData
            TCPSend($Socket, $szData)


Moral/Uses of this program. Basicly for example I run the server file on my computer, and the client on my laptop. They both connect. If I were to send /LockComputer From the Client program to the Server Program, it would activate the computers Lock Feature(The thing were you dont log off but you need a password to get back on) I use both my laptop and computer so I wanted to do some automation commands from my laptop and or vice versa.

Note: I just noticed I had an input box on the Server Program, That isnt really needed so ignore that. If you guys could help me that would be appreciated.


So far the current code I was able to produce, using the info got from the Help file, was able to do some of what I needed. I know its a bit sloppy though but once I get the code right I could make it less sloppy. I am still running on some troubles though. If you guys can help me thank you.

***=Top Priority that I myself will be working on while waiting for responses.

**=Important, Will work on in between working on Top Priority issues.

*=Will work on after others have finished and or if changes are needed.


*Client Connects to Server Successfully: Done(Or so It seems)

**Using Case to allow multiple commands: Not Done

***Select Ip and Port on Client: Not Done

***Select Port for Server: Not Done

**Shut Down Server but not program: Not Done

**Disconnect Client and show disconnection message on server without server program shutting off: Not Done

*Cleaner/Smoother Code and or GUI interface: Not Done

***Split String for commands such as /load http://www.google.com

{something like if client sents "/link http://www.google.com"}

Splits String to know that

$Command = /link

$Link = http://www.google.com

Case $recv = "Link"


Splitting Strings seem to have been completed but only confirmed through tests.

Test Code:

$Word = "/load http://www.youtube.com/"
If StringInStr($Word, "/Load", 2) = True Then
    $Trimmed = StringTrimLeft($Word, 6)
MsgBox(0, "Search result:", $Trimmed)

Another I need is for the Client to receive some things from Server.

For example the client sends /getNotepadText

Server responds sending the client the text that is inside notepad.

I am still working on this myself but I really would appreciate feedback on this, some help, coding, issues and stuff. Some things I don't know as I am still learning/working on it. Ill keep updating this with more info a long the way.

Edit: use when testing on one computer, I noticed that after testing without internet connection.

Edit: Now that I look at it, the code seems a little tooooo sloppy, I think ill consider cleaning it a bit, might make things easier.

Edited by KurogamineNox

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...