Sign in to follow this  
Followers 0
E1M1

Need TCP help, I wanna reply to client

1 post in this topic

How do I send reply to client? my current server displays error message. I wanna create autoit 2 programs.

1 is server. Server must listen commands. one server got command, server must execute it, and send return value back to client.

2 is client. client must send commands to server, and display server replies in GUI.

Client side seems to be hard, I need to listen for replies, and send out commands to server at once. How I do that script?

example:

client -> server run("notepad")

server -> execute(run("notepad")) ->send to client PID (return value) of notepad

client -> show PID return value) in GUI

How I could do communication like I described

SERVER

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv
    TCPStartup()
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)
    If $MainSocket = -1 Then Exit
    $ConnectedSocket = -1


    ;Wait for and Accept a connection
    ;==============================================
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
    $szIP_Accepted = SocketToIP($ConnectedSocket)
    While 1
        $recv = TCPRecv($ConnectedSocket, 2048)
        If @error Then ExitLoop
        If $recv <> "" Then Example2(Execute($recv))
    WEnd
    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc   ;==>Example

Func Example2($szData)
    Local $ConnectedSocket
    Local $szIPADDRESS = "192.168.1.102"
    Local $nPORT = 33891
    TCPStartup()
    $ConnectedSocket = -1
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
    Else
            If @error Then Return
            TCPSend($ConnectedSocket, $szData)
    EndIf
EndFunc 

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   ;==>SocketToIPoÝ÷ Ø"ÈÔÚºÚ"µÍÚ[ÛYH ÑÕRPÛÛÝ[Ñ^]LÉÝÂÜ
    ÌÎNÓ]ÝXÛUÉÌÎNËJBÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOBÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOBÐÓQS ÌÌÎÈÝYHYÝ[ÈHÑTTÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÂÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOBÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOB^[J
B[È^[J
BÈÙ]ÛÛYH]ØXH[ÂËKKKKKKKKKKKKKKKKKKKKKKKKKBØØ[   ÌÍÐÛÛXÝYÛØÚÙ] ÌÍÜÞ]BÈÙ] ÌÍÜÞTQTÔÈÈÚ]HÑTTËÙHÚ[Ú[ÙHHÈ[YH[È[TYÜÂÈØØ[  ÌÍÜÞÙÈHÛÛ][YBÈØØ[    ÌÍÜÞTQTÔÈHÔ[YUÒT
    ÌÍÜÞÙÊBØØ[  ÌÍÜÞTQTÔÈH    ][ÝÌNLMKL
    ][ÝÂØØ[ ÌÍÛÔHÌÎLBÈÝHÔÙXÙÂÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOBÔÝ

BÈ[]X[^HHXXHÈÙ[HÛÛXÝ[ÛÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOB   ÌÍÐÛÛXÝYÛØÚÙ]HLBÐ][ÈÛÛXÝÈÑTT]]ÈT[ÔÌÎLBÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOB    ÌÍÐÛÛXÝYÛØÚÙ]HÔÛÛXÝ
    ÌÍÜÞTQTÔË ÌÍÛÔ
BÈYHÈ[ÜÚÝÈ][ÙÐÞ
LL  ][ÝÑÜ][ÝË  ][ÝÕÔÛÛXÝZ[]ÔÐHÜ    ][ÝÈ  [ÈÜBÈYHÈÈÜÛÜ[[]ÞÜ]BÈÈÙ[ÈHÑTT[ÙBÓÛÜÜ]ÚÚ[ÈÜ]HÈÙ[ÈHÑTTÚ[HBÈ[]ÞÜ][ÛZ]   ÌÍÜÞ]HH[]Þ
    ][ÝÑ]HÜÙ][ÝË  [È [È ][ÝÑ[][ÛZ]ÈHÑTT][ÝÊBÈY^[Ù[H[]ÞÜX]H][ÈÙH^]ÝÜ]ÛÜYÜÜ  ÌÍÜÞ]HH ][ÝÉ][ÝÈ[^]ÛÜÈÙHÚÝ[]H]H[  ÌÍÜÞ]K]È][ÈÙ[]ÝYÚÝÛÛXÝYÛØÚÙ]ÔÙ[
    ÌÍÐÛÛXÝYÛØÚÙ] ÌÍÜÞ]JBÈYHÙ[Z[]Ü[HÛØÚÙ]ÈØÛÛXÝYËKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKBYÜ[^]ÛÜÑ[[Y[[ÈÏOIÝÑ^[

edited

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