Jump to content

Having issues with TCP Client/Server setup


Recommended Posts

I am having issues sending data in my test scripts. I use Kip's TCP.au3 to set everything up so I may have a variable overlapping somewhere. Looking for some guidence. I am able to get his example scripts to work which is what I based my work on. I am trying to have my clients in a sit and wait action until called and then have them call the server back. I have toyed with the idea with the client computers being the servers and having my actual data collection server be the client. Looking to use this in a 300+ network enviroment. Anyway back to my problem. All connections seem to go thru correctly but I never recieve the message from the sever. I verified the message is going out successfully based on the codes returned by _tcp_send. I think that the variable $hsocket is getting issued incorrectly. Thanks for looking. Any help appreciated.

Server.au3

CODE
#include <TCP.au3>

#Include <Array.au3>

#include <Date.au3>

Global $avClients[1][3] = [[0]]

$call = "10.144.77.163" ;client computer with agent

$hServer = _TCP_Server_Create(259); This server.

$hClient = _TCP_Client_Create($call, 159); A client connection to wakeup the agent.

ToolTip("CLIENT "&$hClient&": Connecting...",10,10)

SLEEP(1000)

_TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected")

_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient")

_TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "Disconnect")

While 1

Sleep(10)

WEnd

Func Connected($hSocket, $iError);

If not $iError Then; If there is no error...

ToolTip("Agent wokeup",10,10); ... we're connected.

SLEEP(1000)

Else; ,else...

ToolTip("CLIENT "&$hClient&": Could not connect. Are you sure this client has your agent?",10,10); ... we aren't.

SLEEP(1000)

EndIf

sleep(50)

_TCP_Client_Stop($hSocket)

EndFunc

Func NewClient($hSocket, $iError)

; Get parameters

Local $sDateTime = _NowCalc()

Local $sIP = _TCP_Server_ClientIP($hSocket)

; Add entry to global array

; 2D Array of connected clients:

; [0][0] = Count

; [n][0] = Socket

; [n][1] = Date/Time connected

; [n][2] = IP Address

ReDim $avClients[uBound($avClients) + 1][3]

$avClients[0][0] = UBound($avClients) - 1

$avClients[$avClients[0][0]][0] = $hSocket

$avClients[$avClients[0][0]][1] = $sDateTime

$avClients[$avClients[0][0]][2] = $sIP

; Notify operator

ToolTip("SERVER: New client connected:" & @CRLF & _

"Socket: " & $hSocket & @CRLF & _

"Date/Time: " & $sDateTime & @CRLF & _

"IP: " & $sIP, 10, 30)

sleep(2000)

; Send connection reply

_TCP_Send($hSocket, "Bleh!")

EndFunc ;==>NewClient

Func Disconnect($hSocket, $iError)

; Get parameters

Local $sDateTime = _NowCalc()

Local $iIndex = _ArraySearch($avClients, $hSocket, 1, 0, 0, 0, 1, 0)

If @error Then

MsgBox(16, "Error", "Diconnected socket not found in list.", 30)

Return

EndIf

Local $sConnTime = $avClients[$iIndex][1]

Local $sIP = $avClients[$iIndex][2]

; Remove entry from array

_ArrayDelete($avClients, $iIndex)

; Notify operator

ToolTip("SERVER: Client disconnected:" & @CRLF & _

"Socket: " & $hSocket & @CRLF & _

"Connected: " & $sConnTime & @CRLF & _

"Disconnected: " & $sDateTime & @CRLF & _

"IP: " & $sIP, 10, 30)

EndFunc ;==>Disconnect

Client.au3

CODE
#include "TCP.au3"

Global $server_disconnected = 0

$hServer = _TCP_Server_Create(159) ;Listen for main server instructions

_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "WakeUP")

While 1

ToolTip("SERVER "&$hServer&": Listening...",10,10)

$server_disconnected = 0

sleep(10)

WEnd

Func Wakeup($hSocket, $iError) ;Main server says its time to go to work

ToolTip("SERVER: Waking up Client...",10,20)

SLEEP(1000)

$ServerIP = _TCP_Server_ClientIP($hSocket)

ToolTip("SERVER: Will connect to Server at "& $ServerIP ,10,30)

SLEEP(1000)

ToolTip("CLIENT: Connecting...",10,40)

SLEEP(1000)

$hClient = _TCP_Client_Create($ServerIP, 259); Create the client. Which will connect to the Main server address on port 259

_TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received

_TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected"); And func "Connected" will get called when the client is connected.

_TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected"); And "Disconnected" will get called when the server disconnects us, or when the connection is lost.

While 1

ToolTip("CLIENT "&$hClient&": Listening...",10,10)

sleep(10)

if $server_disconnected = 1 then ExitLoop

WEnd

EndFunc

Func Connected($hSocket, $iError)

If not $iError Then; If there is no error...

ToolTip("CLIENT: Connected!",10,50); ... we're connected.

SLEEP(1000)

Else; ,else...

ToolTip("CLIENT: Could not connect. Are you sure the server is running?",10,50); ... we aren't.

SLEEP(1000)

EndIf

EndFunc

Func Received($hSocket, $sReceived, $iError)

MsgBox(1,"msg RECIEVED", "CLIENT: We received this: "& $sReceived)

ToolTip("CLIENT: We received this: "& $sReceived, 10,60)

EndFunc

Func Disconnected($hSocket, $iError)

ToolTip("CLIENT: Connection closed or lost.", 10,70)

SLEEP(1000)

$server_disconnected = 1

EndFunc

:D Thanks I am open to any ideas on a better system

Link to comment
Share on other sites

I think _TCP_Send should be _TCP_Server_Send.

I checked the TCP.au3 include Kip has _TCP_Send as the function, and like I said the examples that he has also uses _TCP_Send.

Check out your client! & the ip in "TCP.au3"

:D I'm not sure what you mean Are you saying that the actual include is broken?

Link to comment
Share on other sites

I feel like a goofball. I still do not see my problem DCCD seems to have gotten my script to work but I can't find what he is refering to

Check out your client! & the ip in "TCP.au3"

Honestly, Im not sure what he is refering to :D

Im tired Hitting the sack Hopefully the AutoIt fairy will give me some inspiration while I sleep. :D

Link to comment
Share on other sites

Okay im still at it. :D

I have got the server to talk to the client but I cant seem to get the client to talk to the server I know it has to do with the $hsocket I am using the _TCP_Send for the client and using the socket returned _TCP_Client_Create for $hsocket. I dont know where to go from there. I feel as if it is staring me right back in my face. I am going to try to telnet to see if that helps track down the issue.

Link to comment
Share on other sites

Okay im still at it. :D

I have got the server to talk to the client but I cant seem to get the client to talk to the server I know it has to do with the $hsocket I am using the _TCP_Send for the client and using the socket returned _TCP_Client_Create for $hsocket. I dont know where to go from there. I feel as if it is staring me right back in my face. I am going to try to telnet to see if that helps track down the issue.

I don't know if this is the way you are meant to do, but in my PC Controller this was the function I used.

Client side script

Func _ConnectionEstablished($hSocket, $Conectionerror)
    If $Conectionerror Then
        $ConnectedSocket = -1
    Else
        $ConnectedSocket = $hSocket
    EndIf
EndFunc   ;==>_ConnectionEstablishedoÝ÷ Ø    ݶ§ºÇÓ~¢yÞr×J$zا¶¬zw_ºw-Äv+E¢¸(¶Ú-zYm«b­è"²×«yÛaÆ®¶­seõD5õ&Vv7FW$WfVçBb33c¶6ÆVçBÂb33cµD5ô4ôääT5BÂgV÷Cµô6öææV7FöäW7F&Æ6VBgV÷C²
Edited by colafrysen
[font="Impact"]Use the helpfile, It´s one of the best exlusive features of Autoit.[/font]http://support.microsoft.com/kb/q555375ALIBI Run - a replacement for the windows run promptPC Controller - an application for controlling other PCs[size="1"]Science flies us to the moon. Religion flies us into buildings.[/size][size="1"]http://bit.ly/cAMPZV[/size]
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...