Sign in to follow this  
Followers 0
jorgeng

TCP Receive

33 posts in this topic

#1 ·  Posted (edited)

I have a script doint tcp send which works.

I want to know the result after send so i tried to do an tcp receive, but can't get any result, what's wrong?

;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!

; see TCPRecv example

#include <GUIConstants.au3>

$i = 0

;While $i <= 0

;=======TCP SEND - START ====================================================================================================

=============================================

; Start The TCP Services

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

TCPStartUp()

; Set Some reusable info

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

; Dim $szServerPC = @ComputerName

Dim $szServerPC = "66.66.66.66"

MsgBox(4096, "Server", $szServerPC)

; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address

Dim $szIPADDRESS = TCPNameToIP($szServerPC)

Dim $nPORT = "6666"

MsgBox(4096, "Port", $nPORT)

; Initialize a variable to represent a connection

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

Dim $ConnectedSocket = -1

; Attempt to connect to SERVER at its IP and PORT 33891

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

$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)

Dim $szData

; If there is an error... show it

If @error Then

MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)

; If there is no error loop an inputbox for data to send to the SERVER.

Else

;

$szData = '<?xml version="1.0" encoding="Windows-1252" ?>' & @CRLF & _

'</Signal>"'

Msgbox(4096,"Sträng till TCP", $szData)

EndIf

; We should have data in $szData... lets attempt to send it through our connected socket.

TCPSend($ConnectedSocket,$szData)

MsgBox(4096, "TCPSend gjord", $nPORT)

; If the send failed with @error then the socket has disconnected

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

; If @error Then ExitLoop

EndIf

;Wend

;=======TCP RECEIVE ====================================================================================================

=============================================

; #include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

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

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

;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 $szIPADDRESS = "66.66.66.66"

Local $nPORT = 6666

Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted

Local $msg, $recv

; Start The TCP Services

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

;TCPStartup()

MsgBox(4096, "I receive", $nPORT)

; 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

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

$GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)

$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)

GUISetState()

; 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))

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), "ptr", 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

Edited by jorgeng

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The meaning of the tcp receive is to read the result from ip-adress 66.66.66.66

I don't know if i have to do a tcplisten too?

:)

Edited by jorgeng

Share this post


Link to post
Share on other sites

I have a script doint tcp send which works.

I want to know the result after send so i tried to do an tcp receive, but can't get any result, what's wrong?

What exactly do you mean? Do you want the server to send a reply saying data received?

Share this post


Link to post
Share on other sites

What exactly do you mean? Do you want the server to send a reply saying data received?

This is for financial instrument, i send by tcp an order and want to have a receive string with

information from the socket conncted saysing that the order gone ok.

TCP is new for me, don't really knew how to code this.

:)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

What exactly do you mean? Do you want the server to send a reply saying data received?

This is for financial instrument, i send by tcp an order and want to have a receive string with

information from the socket conncted saying that the order gone ok.

TCP is new for me, don't really knew how to code this.

:)

Edited by jorgeng

Share this post


Link to post
Share on other sites

You get the server to send the data back down the already connected socket and you make the Client do a Receive loop on the already connected socket

;=======TCP RECEIVE ======================
 #include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;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 $szIPADDRESS = "127.0.0.1";"66.66.66.66" 
Local $nPORT = 6920
Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
Local $msg, $recv

; Start The TCP Services
;==============================================
TCPStartup()

MsgBox(4096, "I receive", $nPORT)

; 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 Msgbox(0,"Error","Mainsocket failed with error code " & @error)


; Create a GUI for messages
;==============================================
$GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
GUISetState()


; 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))
            TCPSend($ConnectedSocket,"Data Received")
    EndIf
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), "ptr", 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

;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
; see TCPRecv example
#include <GUIConstants.au3>

$i = 0
;While $i <= 0

;=======TCP SEND - START ========================

; Start The TCP Services
; ==============================================
TCPStartUp()

; Set Some reusable info
; --------------------------
; Dim $szServerPC = @ComputerName
Dim $szServerPC = "127.0.0.1";"66.66.66.66"
MsgBox(4096, "Server", $szServerPC) 

; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
Dim $szIPADDRESS = TCPNameToIP($szServerPC)
Dim $nPORT = "6920" 
MsgBox(4096, "Port", $nPORT) 

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

; Attempt to connect to SERVER at its IP and PORT 33891
; =======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)

Dim $szData

; If there is an error... show it
If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data to send to the SERVER.
    Else
; 
    $szData = '<?xml version="1.0" encoding="Windows-1252" ?>' & @CRLF & _
        '<Signal xmlns="http://borsdata.se/TESSignal.xsd">' & @CRLF & _
        '<Version>1.0</Version>  </Signal>"' 
;Msgbox(4096,"Sträng till TCP", $szData) 
EndIf

; We should have data in $szData... lets attempt to send it through our connected socket.
TCPSend($ConnectedSocket,$szData)

$recv  = ""
While $recv = ""
    $recv = TCPRecv($ConnectedSocket,2048)
WEnd
Msgbox(0,"",$recv)

MsgBox(4096, "TCPSend gjord", $nPORT)

; If the send failed with @error then the socket has disconnected
; ----------------------------------------------------------------
; If @error Then ExitLoop



;EndIf 


;Wend

Share this post


Link to post
Share on other sites

Thanks,

but i'm not sure to handle this.

When i am in autoit i connect to an external firm ip 66.66.66.66 (the server), i can't do anything on that firm.

What i want to do is code for sending a text-string (the order) to this company by tcp send and get the answer from

ip 66.66.66.66 the company by tcp receive that the order gone ok.

I'm not the server. Right/wrong?

:)

Share this post


Link to post
Share on other sites

is the server application yours @ 66.66.66.66 i.e. are you writing it? or are you purely writing the client?

I'm just writing the client

Share this post


Link to post
Share on other sites

I'm just writing the client

OK, the only thing you can do is veryfy the amount of data sent unless they send a verification message.

$szData = '<?xml version="1.0" encoding="Windows-1252" ?>' & @CRLF & _
        '<Signal xmlns="http://borsdata.se/TESSignal.xsd">' & @CRLF & _
        '<Version>1.0</Version>  </Signal>"' 
$szDataLen = StringLen($szData)

$cnt = TCPSend($ConnectedSocket,$szData)
If <> @error and $szDataLen = $cnt then 

;sent the correct amount of data

Else

;didn't send the correct amount of data or there was an error

Endif

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

OK, the only thing you can do is veryfy the amount of data sent unless they send a verification message.

$szData = '<?xml version="1.0" encoding="Windows-1252" ?>' & @CRLF & _
        '<Signal xmlns="http://borsdata.se/TESSignal.xsd">' & @CRLF & _
        '<Version>1.0</Version>  </Signal>"' 
$szDataLen = StringLen($szData)

$cnt = TCPSend($ConnectedSocket,$szData)
If <> @error and $szDataLen = $cnt then 

;sent the correct amount of data

Else

;didn't send the correct amount of data or there was an error

Endif
They do send a specific string of data back, i want to read this string, how? Edited by jorgeng

Share this post


Link to post
Share on other sites

I showed you that in the client I posted earlier

$recv  = ""
While $recv = ""
    $recv = TCPRecv($ConnectedSocket,2048)
WEnd
Msgbox(0,"",$recv)

After you have sent the data to them, you may want to add a timeout to the loop

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I got syntax error:

D:\Program\AutoIt3\Ra.au3(110,6) : ERROR: syntax error

If <>

~~~^

Edited by jorgeng

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

For the Client and the Server in AutoIt I always loop around TCPSend and TCPRecv... a little bit like

while 1
    If StringLen($mySendBuffer) Then
        $lenCheck = TCPSend($socketIOpened, $mySendBuffer)
        If @error Then ExitLoop
        $mySendBuffer = StringTrimLeft($mySendBuffer,$lenCheck)
    EndIf
    $dataBack = TCPRecv($socketIOpened, 4096)
    If @error Then ExitLoop
    If StringLen($dataBack) Then DoSomethingWith($dataBack)
WEnd

There are many parts missing here... but essentially server and client both generically recv and send.

Lar.

Edited by LarryDalooza

f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

I got message:

unable to read data from the transport connection

when i run this code:

:)

; We should have data in $szData... lets attempt to send it through our connected socket.

TCPSend($ConnectedSocket,$szData)

MsgBox(4096, "TCPSend gjord", $nPORT)

; If the send failed with @error then the socket has disconnected

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

If @error Then ExitLoop

$szDataLen = StringLen($szData)

$cnt = TCPSend($ConnectedSocket,$szData)

If $szDataLen = $cnt Then

MsgBox(4096, "Sent the correct amount of data", $nPORT)

Else

MsgBox(4096, "Didn't send the correct amount of data or there was an error", $nPORT)

Endif

$recv = ""

While $recv = ""

$recv = TCPRecv($ConnectedSocket,2048)

WEnd

Msgbox(0,"",$recv)

Edited by jorgeng

Share this post


Link to post
Share on other sites

Do you think you could be a little more specific!

I got message:

unable to read data from the transport connection

How did this message present itself? was that the reply you got from the server?

was it in this part of the code?

$recv = ""

While $recv = ""

$recv = TCPRecv($ConnectedSocket,2048)

WEnd

Msgbox(0,"",$recv) :<<<<<<<<<<<<<<<<<<<<<<<<<<<<did the message show here?

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

yes it did, excatcly where you pointed...

Edited by jorgeng

Share this post


Link to post
Share on other sites

which means that you succesfully sent data, you successfully received a reply. BUT the data you sent was incorrect.

I would verify that the data here $szData is correct I would also check the stringlen($szData) and compare it to the return of TCPSend() to make sure you sent all of the data.

$szDataLen = StringLen($szData)
$cnt = TCPSend($ConnectedSocket,$szData)
If $szDataLen = $cnt Then 
MsgBox(4096, "Sent the correct amount of data", $nPORT) 
Else
MsgBox(4096, "Didn't send the correct amount of data or there was an error", $nPORT) 
Endif

It would also seem to me that you need to know all of the server return values to know what errors you get and why

Share this post


Link to post
Share on other sites

; Skikcar på port 6921 frågor.

Dim $nPORT = "6921"

MsgBox(4096, "Port", $nPORT)

; Initialize a variable to represent a connection

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

Dim $ConnectedSocket = -1

; $szData = 'signal;RTest'

$szData = 'info;RTest'

Msgbox(4096,"Sträng till TCP", $szData)

; Attempt to connect to SERVER at its IP and PORT 33891

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

$ConnectedSocket = TCPConnect($szIPADDRESS,$nPORT)

TCPSend($ConnectedSocket,$szData)

MsgBox(4096, "TCPSend gjord på port 6921", $nPORT)

$recv = ""

While $recv = ""

$recv &= TCPRecv($ConnectedSocket,2048)

if @error exitloop

WEnd

Msgbox(0,@error,$recv)

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Well, it doesn't works.

I want some result but get nothing except -1 sometimes...

$recv = ""

While $recv = ""

$recv &= TCPRecv($ConnectedSocket,2048)

If @error Then

exitloop

Else

Msgbox(4096,"",$recv)

Endif

WEnd

Msgbox(0,@error,$recv)

Edited by jorgeng

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