Jump to content

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()
    Exit
EndFunc
;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

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
    ;==============================================
    TCPStartup()

    ; 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)
        GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###


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


    ;Wait for and Accept a connection
    ;==============================================
    Do
        $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))
        Select
            Case $recv = "/notepad"
                ShellExecute("notepad")
        EndSelect
    WEnd


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

    TCPShutdown()
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]
    Else
        $aRet = 0
    EndIf

    $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)
$Edit1 = GUICtrlCreateEdit("", 8, 32, 377, 177, BitOR($ES_AUTOVSCROLL,$ES_READONLY,$ES_WANTRETURN,$WS_GROUP,$WS_VSCROLL))
GUICtrlSetData(-1, "Edit1")
$Button3 = GUICtrlCreateButton("Send", 146, 258, 89, 33, $WS_GROUP)
$Input2 = GUICtrlCreateInput("Ip Address to Connect to", 232, 8, 153, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$g_IP = "ip address of host/server"
$szData = ""

While 1


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

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

    EndSelect
WEnd

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.

*{UPDATED SECTION}*

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.

List:

*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"

ShellExecute($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)
EndIf

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