Sign in to follow this  
Followers 0
D4rk^S0ul

TCP Options

10 posts in this topic

I have been looking at the TCP Options on beta and been experimenting with them but i seem to have only failed does anyone why what is wrong with my code, i am using the server from the examples till i can get my client working. The client and server from examples work together fine.

Call("Check")

Func Check()
    $g_IP = "86.139.248.121"
    $g_port = 33891


    $socket = TCPConnect( $g_IP, $g_port )
    If $socket = -1 Then 
        TCPCloseSocket( $socket )
        Sleep(2000)
        Call("Check")
    Else
        TCPSend($socket, "Connection Success")
    EndIf
    
    $recv = TCPRecv($socket, 512 )
    
    While 1
    
        If $recv = "CD Open" Then
            CDTray("D:", "open")
        EndIf
        
        Sleep(100)
        
    WEnd
    
EndFunc

Share this post


Link to post
Share on other sites



The format of the whole is a little off. But, since you have actually tried something, I will help.

http://www.autoitscript.com/forum/index.php?showtopic=20589

This is an excellent collection of TCP scripts that AutoIt Smith wrote. They all work great.

As for the actual format of TCP, you need to have two pieces: server and client. First, both have to start TCP via TCPStartup(). The server needs to listen for incoming connections via TCPListen. Once that has succeeded, the server will need to accept connections on a certain port with TCPAccept. It will have to loop to accept connections until it receives one - otherwise if there is no loop it will simply fail the first time and be done.

Once a connection is made, the server must loop TCPRecv to get the information sent to it by TCPSend. If you do not loop this, it again will simply fail and move on to the end of the script.

As for the client, it needs to TCPConnect to the same IP address and port that was specified by TCPListen on the server side. Once a connection is made, the client can use TCPSend to send data to the server.

Here's a really simple example. Start the server first, then the client (two separate scripts). It will simply wait until the client sends "CD Open", open the cd tray, and then quit both scripts.

;--server--
TCPStartup()
$MainSocket = TCPListen (@IPAddress1, 9999)

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

While 1
    $Data = TCPRecv ($ConnectedSocket, 512)
    If $Data = "CD Open" Then
        CDTray ("D:", "open")
        ExitLoop
    EndIf
    Sleep (100)
WEnd

;--client--
TCPStartup()
$MainSocketClient = TCPConnect (@IPAddress1, 9999)

TCPSend ($MainSocketClient, "CD Open")

Share this post


Link to post
Share on other sites

Thx for the help but is there anyway to make it so if the Client is run but the server isent to make the client sleep for a few seconds and retry to connect to the server because atm all the things i have tried it has just froze the client if the server isent open?

Share this post


Link to post
Share on other sites

TCPStartup()

Do

$MainSocketClient = TCPConnect (@IPAddress1, 9999)

Sleep (50)

Until $MainSocketClient <> -1

Share this post


Link to post
Share on other sites

That is the same code as i tried but for some reason it freezes, it does not detect when it does connect and when you go to pause script from tray you can see it is frozen because the menu does not come up.

Share this post


Link to post
Share on other sites

Are your scripts separate or combined as one?

Post what you have - it will be easier to diagnose the problem.

Share this post


Link to post
Share on other sites

All i have so far is

Client:

TCPStartup()
Do
$MainSocketClient = TCPConnect ("127.0.0.1", 65432)
Sleep (50)
Until $MainSocketClient <> -1

TCPSend($MainSocketClient, "CD Open")

Server:

TCPStartup()
$MainSocket = TCPListen ("127.0.0.1", 65432)

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

While 1
    $Data = TCPRecv ($ConnectedSocket, 512)
    If $Data = "CD Open" Then
        CDTray ("D:", "open")
        ExitLoop
    EndIf
    Sleep (100)
WEnd

They are two seperate scripts i am going to expand on them when i get them working properly.

Share this post


Link to post
Share on other sites

I messed around with it a bit - turns out the sleep in the server's while loop was too long. Just change it to something smaller, such as 25 or 50, and you should be ok.

Share this post


Link to post
Share on other sites

I seem to have the problem i tried to up a msgbox after the defining of $MainSocketClient to TCPConnect and it does not reach that far so it seems when TCPConnect fails it stops the program is there anyway to see if the connection is possible before it trys to open it so that it does not run TCPConnect if it will error?

Share this post


Link to post
Share on other sites

TCPConnect does not stop the script on a failure. It returns -1, hence the condition at the Until line.

As Greenmachine pointed out, the delay in the server's While loop is your problem. It will work much better if you simply remove the Sleep entirely. These modifications work fine for me:

TCPStartup()
Do
$MainSocketClient = TCPConnect ("127.0.0.1", 65432)
Sleep (1000)
Until $MainSocketClient <> -1

TCPSend($MainSocketClient, "CD Open")

TCPStartup()
$MainSocket = TCPListen ("127.0.0.1", 65432)

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

While 1
    $Data = TCPRecv ($ConnectedSocket, 512)
    If $Data = "CD Open" Then
        CDTray ("D:", "open")
        ExitLoop
    EndIf
WEnd

I extended the delay on the client because 50ms is a very short time to wait between connection attempts.

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