Jump to content

Simple TCP problems


Recommended Posts

Hey all.

I've worked with TCP functionality before, but it's always been on one end of the spectrum. I can either send messages and a third party app responds or vice versa. However, this time, I'm running software tests on two different computers (by necessity) and I need just a simple time stamp to be send from one computer (TCPSend) and received by another (TCPRecv) both running tiny AutoIt scripts. Right now, I'm in the testing phase of this script, so the only functionality in them is the TCP connectivity. I get the connection to work (both scripts recognize the connection) but I can't get the information to come across. Here's what I have:

The "Listening" side:

HotKeySet("{PAUSE}","_Exit")

TCPStartup()

$Socket = TCPListen("192.168.64.76",2015)

While 1

    $ConnectedSocket = -1
    $sReceive = ""

    Do
        $ConnectedSocket = TCPAccept($Socket)
    Until $ConnectedSocket >=0

    ConsoleWrite("Connected" & @CR)

    Do
        $sReceive = TCPRecv($ConnectedSocket,2048)
    Until $sReceive <> ""

    ConsoleWrite("Data: " & $sReceive & @CR)

WEnd

Func _Exit()
    TCPShutdown()
    Exit
EndFunc

The "Sending" side:

TCPStartup()

$Socket = TCPConnect("192.168.64.76",2015)

If @error Then
    MsgBox(4112,"Error","TCPConnect failed with WSA error: " & @error)
Else
    TCPSend($Socket,"Hey, does this TCP send thing actually work?!?!?!?!?!")
    If @error Then MsgBox(4112,"Error","You failed with WSA error: " & @error)
EndIf

TCPShutdown()

According the threads I've read and the help file, it seems like everything is in order and should work properly.

Thanks for your help!

-Fett

[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
Link to comment
Share on other sites

I'm pretty sure you should listen to your internal IP (192.168.2.XXX)

Edit: my mistake, you're doing it right

Edit 2: I've tested both scripts on my PC and they work fine.

If you're connected to the internet through a router you should port forward to the port you're using.

Also, don't forget to use TCPCloseSocket().

Edited by Info
Link to comment
Share on other sites

I got it to work for me. Read the comments for what I did...

Sending side

TCPStartup()

Global Const $port = 2015
Global Const $IP = "192.168.64.76"

Global $Socket = TCPConnect( $IP , $port )

ConsoleWrite( "$Socket: " & $Socket & "  |  @ERROR: " & @error & @CRLF )

Global $data , $return

If @error Then
    MsgBox( 4112 , "Error" , "TCPConnect failed with WSA error: " & @error )
Else
    ; didn't work until I assigned the return of TCPSend to a variable!!!!
    $return = TCPSend( $Socket , "Hey, does this TCP send thing actually work?!?!?!?!?!" )

    If @error Then
        MsgBox( 4112 , "Error" , "You failed with WSA error: " & @error )
    Else
        MsgBox( 0 , "Success" , "Bytes sent: " & $return )
    EndIf
EndIf

TCPCloseSocket( $socket )
TCPShutdown()

listening side

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

OnAutoItExitRegister( "cleanup" )

TCPStartup()

Global Const $port = 2015
Global Const $IP = "192.168.64.76" 

Global $Socket = TCPListen( $IP , $port )
Global $ConnectedSocket = -1
Global $sReceive = ''

MsgBox( 0 , $IP , "$Socket: " & $Socket & "  |  @ERROR: " & @error )

While 1
    Sleep( 100 )

    While $ConnectedSocket = -1
        $ConnectedSocket = TCPAccept( $Socket )
    WEnd

    ConsoleWrite( "Connected" & @CRLF )

    While $sReceive = ''
        $sReceive = TCPRecv( $ConnectedSocket , $port )
    WEnd

    MsgBox( 0 , '' , "Data: " & $sReceive )

    $ConnectedSocket = -1
    $sReceive = ""
WEnd

Func cleanup()
    TCPCloseSocket( $ConnectedSocket )
    TCPShutdown()
EndFunc

Func _Exit()
    Exit
EndFunc
Edited by LaCastiglione
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...