Sign in to follow this  
Followers 0
jaenster

Tcp , How?

7 posts in this topic

wtf , how you make a tcp connection with autoIT ?

Not ? ^^


-jaenster

Share this post


Link to post
Share on other sites



a repley shall be nice :/


-jaenster

Share this post


Link to post
Share on other sites

a repley shall be nice :/

From the help file.. you need the beta version of AutoIT

Server

CODE

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

#include <GUIConstants.au3>

; Initialize a variable to represent a connection

;==============================================

Global $MainSocket, $ConnectedSocket = -1

Global $g_IP = @IPADDRESS1

Global $g_port = 33891

; Start The TCP Services

;==============================================

TCPStartUp()

; Create a Listening "SOCKET"

;==============================================

$MainSocket = TCPListen($g_IP, $g_port, 100 )

If $MainSocket = -1 Then Exit

$RogueSocket = -1

; Create a GUI for chatting

;==============================================

$GOOEY = GUICreate("my server - I am " & @IPADDRESS1,350,200,@DesktopWidth/2+100,@DesktopHeight/2-350)

$edit = GUICtrlCreateEdit("",10,40,330,150,$WS_DISABLED)

$input = GUICtrlCreateInput("",10,10,250,20)

$butt = GUICtrlCreateButton("Send",260,10,80,20,$BS_DEFPUSHBUTTON)

GUISetState()

; GUI Message Loop

;==============================================

While 1

$msg = GUIGetMsg()

; GUI Closed

;--------------------

If $msg = $GUI_EVENT_CLOSE Then ExitLoop

; User Pressed SEND

;--------------------

If $msg = $butt Then

If $ConnectedSocket > -1 Then

$ret = TCPSend( $ConnectedSocket, GUICtrlRead($input))

If @ERROR Then

; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1

;----------------------------------------------------------------

TCPCloseSocket( $ConnectedSocket )

WinSetTitle($GOOEY,"","my server - I am " & @IPADDRESS1)

$ConnectedSocket = -1

ElseIf $ret > 0 Then

; UPDATE EDIT CONTROL WITH DATA WE SENT

;----------------------------------------------------------------

GUICtrlSetData($edit, GUICtrlRead($edit) & GUICtrlRead($input) & @CRLF )

EndIf

EndIf

GUICtrlSetData($input,"")

EndIf

If $RogueSocket > 0 Then

$recv = TCPRecv( $RogueSocket, 512 )

If NOT @error Then

TCPCloseSocket( $RogueSocket )

$RogueSocket = -1

EndIf

EndIf

; If no connection look for one

;--------------------

If $ConnectedSocket = -1 Then

$ConnectedSocket = TCPAccept( $MainSocket)

If $ConnectedSocket >= 0 Then

WinSetTitle($GOOEY,"","my server - Hello " & SOCKET2IP($ConnectedSocket))

EndIf

; If connected try to read some data

;--------------------

Else

; EXECUTE AN UNCONDITIONAL ACCEPT IN CASE ANOTHER CLIENT TRIES TO CONNECT

;----------------------------------------------------------------

$RogueSocket = TCPAccept( $MainSocket)

If $RogueSocket > 0 Then

TCPSend( $RogueSocket, "~~rejected" )

EndIf

$recv = TCPRecv( $ConnectedSocket, 512 )

If $recv <> "" And $recv <> "~~bye" Then

; UPDATE EDIT CONTROL WITH DATA WE RECEIVED

;----------------------------------------------------------------

GUICtrlSetData($edit, GUICtrlRead($edit) & ">" & $recv & @CRLF)

ElseIf @error Or $recv = "~~bye" Then

; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1

;----------------------------------------------------------------

WinSetTitle($GOOEY,"","my server - I am " & @IPADDRESS1)

TCPCloseSocket( $ConnectedSocket )

$ConnectedSocket = -1

EndIf

EndIf

WEnd

GUIDelete($GOOEY)

Func OnAutoItExit()

;ON SCRIPT EXIT close opened sockets and shutdown TCP service

;----------------------------------------------------------------------

If $ConnectedSocket > -1 Then

TCPSend( $ConnectedSocket, "~~bye" )

Sleep(2000)

TCPRecv( $ConnectedSocket, 512 )

TCPCloseSocket( $ConnectedSocket )

EndIf

TCPCloseSocket( $MainSocket )

TCPShutDown()

EndFunc

Func SOCKET2IP($SHOCKET)

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

$a = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET,"ptr",DLLStructGetPtr($sockaddr), _

"int_ptr",DLLStructGetSize($sockaddr))

If Not @error And $a[0] = 0 Then

$a = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3))

If Not @error Then $a = $a[0]

Else

$a = 0

EndIf

; release Struct not really needed as it is a local

$sockaddr = 0

Return $a

EndFunc

Client

CODE

;CLIENT!!!!!!!! Start SERVER First... dummy!!

#include <GUIConstants.au3>

$g_IP = InputBox("Chat Client","Gimme The Server IP Address",@IPADDRESS1,"",300,200)

$g_port = 33891

; Start The TCP Services

;==============================================

TCPStartUp()

; Connect to a Listening "SOCKET"

;==============================================

$socket = TCPConnect( $g_IP, $g_port )

If $socket = -1 Then Exit

; Create a GUI for chatting

;==============================================

$GOOEY = GUICreate("my client - Server Connected",350,200)

$edit = GUICtrlCreateEdit("",10,40,330,150,$WS_DISABLED)

$input = GUICtrlCreateInput("",10,10,250,20)

$butt = GUICtrlCreateButton("Send",260,10,80,20,$BS_DEFPUSHBUTTON)

GUISetState()

; GUI Message Loop

;==============================================

While 1

$msg = GUIGetMsg()

; GUI Closed

;--------------------

If $msg = $GUI_EVENT_CLOSE Then ExitLoop

; User Pressed SEND

;--------------------

If $msg = $butt Then

$ret = TCPSend($socket, GUICtrlRead($input))

If @ERROR Then ExitLoop

; Server Disconnected... we Outty...

;---------------------------------------

If $ret > 0 Then GUICtrlSetData($edit, GUICtrlRead($edit) & GUICtrlRead($input) & @CRLF)

GUICtrlSetData($input,"")

EndIf

$recv = TCPRecv($socket, 512 )

$err = @error

If $recv = "~~rejected" Then

GUICtrlSetData($edit, GUICtrlRead($edit) & "~~Connection Rejected" & @CRLF)

WinSetTitle($GOOEY,"","Connection Rejected")

Sleep(2000)

TCPSend( $socket, "~~whatever")

ExitLoop

ElseIf $recv = "~~bye" Then

GUICtrlSetData($edit, GUICtrlRead($edit) & "~~Connection Lost" & @CRLF)

WinSetTitle($GOOEY,"","Connection Lost")

Sleep(2000)

ExitLoop

EndIf

If $err <> 0 Then ExitLoop

; Server Disconnected... we Outty...

;---------------------------------------

If $err=0 AND $recv <> "" Then GUICtrlSetData($edit, GUICtrlRead($edit) & ">" & $recv & @CRLF)

WEnd

Func OnAutoItExit()

TCPSend( $socket, "~~bye" )

TCPCloseSocket( $socket )

TCPShutDown()

EndFunc

Share this post


Link to post
Share on other sites

@ChrisL: man you are way too helpful to those who don't help themselves.

Share this post


Link to post
Share on other sites

@ChrisL: man you are way too helpful to those who don't help themselves.

Maybe.. but when i was fairly new to AutoIT3 (i used to use V2) Lxp went above and beyond to help me with a problem on recursion and converting thousands of files, so I have always tried to help others, sometimes it is easier to understand a piece of code rather than the whole daunting help file, it took me months to finally understand exactly what Lxp did but I got there in the end. Besides sometimes helping someone else helps me to learn more too so I get something back, with the odd exception of those who don't say thanks or as happened last night I got insulted for trying to help (lol)

And another thing if more advanced people notice me trying to help they will hopefully help me because they know I do try!

So to sum up.. It's all selfish :lmao:

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