Jump to content

Problem with TCPRecv


Recommended Posts

Hello,

i have a problem were i could use a bit of help, so i just write it down her, maybe someone knows what i can do:

I made these two example scripts:

send.au3

HotKeySet("{ESC}", "Terminate")

    $ConnectedSocket = -1
    TCPStartup()
    $ConnectedSocket = TCPConnect("127.0.0.1", 5555)

    $counter = 0
    While 1
    $counter = $counter + 1
    $text = "Transmission number " & $counter
    ConsoleWrite("Sending: " & $text & @CRLF)
    TCPSend($ConnectedSocket, StringToBinary($text, 4))
    If @error Then
    ;try to reconnect everytime we notice the connection is lost
    $ConnectedSocket = TCPConnect("127.0.0.1", 5555)
    EndIf
    Sleep(1000)
    WEnd

    Func Terminate()
    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
    TCPShutdown() ; Close the TCP service.
    Exit 0
    EndFunc ;==>Terminate

 

receive.au3

 

TCPStartup()
    $MainSocket = TCPListen("0.0.0.0", 5555)
    If $MainSocket = -1 Then
    MsgBox(16, "Error", "Can not open TCP Socket")
    Exit
    EndIf

    ConsoleWrite("waiting for connection" & @CRLF)
    Do
    $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1

    ConsoleWrite("connected" & @CRLF)

    While 1
    $text = TCPRecv($ConnectedSocket, 2048)
    If not @error then ConsoleWrite($text & @CRLF)
    WEnd

 

 

When i start send.au3 it sends out transmission and if sending fails it tries to reconnect.

I can then start receive.au3 and will get the transmissions as i want it to be.

When i now stop the send.au3 and restart it, i will no longer get transmissons in the still running receive.au3

Can onyone help me to change the script so that it will continue to receive when the server is running again?

Thanks in advance..

 

 

receive.au3

send.au3

Edited by Allow2010
Link to comment
Share on other sites

Would you be opposed to using ping instead of TCPStartup?

HotKeySet("{ESC}", "Terminate")

;Declare all of your variables here
Global $IPAddress = "127.0.0.1"
Global $Port = 5555
Global $Counter = 0;counter for number of successful pings for the console
Global $ConnectedSocket = TCPAccept($IPAddress, $Port)
Global $MainSocket = -1

;Your main While Loop
While 1
    Ping($IPAddress & ":" & $Port, 10000);ping the ip address and timeout after 10 seconds
    If Not @error Then
        ConsoleWrite("Connection available at: " & $IPAddress & @CRLF)
        $text = TCPRecv($ConnectedSocket, 2048)
        ConsoleWrite("waiting for connection" & @CRLF)
        Do
            $ConnectedSocket = TCPAccept($MainSocket)
        Until $ConnectedSocket <> -1
        If Not @error Then
            ConsoleWrite($text & @CRLF)
        EndIf
    EndIf
    If @error Then
        ConsoleWrite("Cannot ping: " & $IPAddress)
    EndIf
    $Counter += 1
    Sleep(1000)
WEnd

Func Terminate()
    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
    TCPShutdown() ; Close the TCP service.
    Exit 0
EndFunc   ;==>Terminate

This is untested because I dont have a way to really test it here. Is this for IRC or a download or a chat interface or something else?

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Link to comment
Share on other sites

Thanks, i will try it later and will report back.

I want to use it for something similar to a chat...the sender sends info from  PC1 to the receiver on PC2 which then displays the info...

I want them to reconnect when on side restarts...

Link to comment
Share on other sites

TCPStartup()

$ConnectedSocket = -1

$MainSocket = TCPListen("0.0.0.0", 5555)
If $MainSocket = -1 Then
    MsgBox(16, "Error", "Can not open TCP Socket")
    Exit
EndIf

Waitconn()

While 1
    $text = TCPRecv($ConnectedSocket, 2048)
    $ierror = @error
    If Not $ierror Then
        ConsoleWrite($text & @CRLF)
    ElseIf $ierror = 10054 Then
        Waitconn()
    Else
        ;ConsoleWrite($ierror & @CRLF)
    EndIf
WEnd


Func Waitconn()
    ConsoleWrite("waiting for connection" & @CRLF)
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
    ConsoleWrite("connected" & @CRLF)
EndFunc   ;==>Waitconn

works for me...

Link to comment
Share on other sites

  • 5 months later...

After i while i came back to this and i still had a problem:

I wanted the scripts to automatically reconnect if the on of the scripts was stopped or restarted.

As the above version still hat trouble with such situations i came up with a new version.

Here is my current version, just in case someone needs something like this..

 

rec.au3

send.au3

Edited by Allow2010
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...