Sign in to follow this  
Followers 0

TCP network code not going online

4 posts in this topic


I've been trying to get this simple server/client script to work online, but for some reason it only works when trying my comp to my comp. I've forwarded the port through my router, but the client can not connect to the server. Is there something with my script?

The error i get is always that the client could not connect, with an @error of 10061 from the client TCPConnect() function. Any thoughts?


#include <GUIConstants.au3>
#include <Array.au3>
#Include <GuiEdit.au3>

#include "serverNetFunctions.au3"

; DECLARATION -------------------------------------------------- DECLARATION ---

$host_IP = ""      ; server address, should always point to self
$host_port = "64452"

Global $activeConnections[1]; keeps track of all currently connected sockets
$activeConnections[0] = 0   ; first element is the index size, DO NOT DELETE

;Global $numConnections = 0 ; keeps track of how many connected sockets in array
Global $MainSocket = -1

; INITIALIZE ---------------------------------------------------- INITIALIZE ---

#include "serverGUI.au3"; create GUI

TCPStartUp(); Start TCP Services

; Create a Listening "SOCKET"
$MainSocket = TCPListen($host_IP, $host_port,  100 )
If $MainSocket = -1 Then 
    write("TCP Connection Error: " & @error)

; MAIN ---------------------------------------------------------------- MAIN ---

While 1
    $msg = GUIGetMsg()
    if $msg = $GUI_EVENT_CLOSE OR $msg = $quitItem then ExitLoop; quit app

    CheckNewConnection(); check for new connections, if so send check if db is up to date
    RecieveData(); check to see if new data is coming im, if so send to all connected


TCPShutdown ( ); Stop TCP Services

; FUNCTIONS ------------------------------------------------------ FUNCTIONS ---

Func CheckNewConnection()
; this function will check if a new connection is attempting to be established
    Local $ConnectedSocket = -1

    $ConnectedSocket = TCPAccept($MainSocket)
    if $ConnectedSocket >= 0 Then
        _ArrayAdd($activeConnections, $ConnectedSocket)
    ;$numConnections+= 1
        $activeConnections[0] += 1
        write("New connection established @ " & SocketToIP($ConnectedSocket) & ":" & $ConnectedSocket)

Func RecieveData()
    Local $x=1
    While $x < ($activeConnections[0] + 1)
        $data = TCPRecv($activeConnections[$x], 100)
    ; TCPRecv could not establish connection to socket, meaning user has disconnected
        If @error Then 
            deleteConnection($activeConnections[$x]); delete connection from array
        If $data = "" Then 
            $x += 1
            ContinueLoop; nothing was recieved from connection
        $x += 1


Func deleteConnection($isocket)
; will delete the corresponding socket from the user list

    For $i = 1 to $activeConnections[0]

        If $isocket == $activeConnections[$i] Then
            write("Connection lost @ " & SocketToIP($isocket) & ":" & $isocket)
                if @error then write("An error has occured while attempting to close socket: " & @error)
            _ArrayDelete($activeConnections, $i)
            $activeConnections[0] -= 1


Func SendData($isocket)
;will send all new db data to user  

Func CheckUpToDate($isocket)
; check to see if server db is same as user db  
    Return True

Func SocketToIP($SHOCKET)

    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

    Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
    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]
        $aRet = 0

    $sockaddr = 0

    Return $aRet


#include <GUIConstants.au3>

; Script Start - Add your code below here

$host_IP = "WAN IP" ; censored

$host_port = "64452"

; Start The TCP Services


; Connect to a Listening "SOCKET"

$socket = TCPConnect( $host_IP, $host_port )

If $socket = -1 Then

MsgBox(0, "", "Could not connect")


$window = GUICreate("client")

GUISetState (@SW_SHOW)

While 1

$msg = GUIGetMsg()

if $msg = $GUI_EVENT_CLOSE then ExitLoop


TCPShutdown ( )

Share this post

Link to post
Share on other sites

#2 ·  Posted (edited)

The error is in the top of your script:

$host_IP = ""        ; server address, should always point to self

That would bind the server to your loopback interface, and only be accessible from your own computer.

You need to bind it to your lan ip(ie. the one you get from your router). :D

Edited by FreeFry

Share this post

Link to post
Share on other sites

omg, it worked! thanks a lot. just a little bit confusing when the help file example uses the address.

Share this post

Link to post
Share on other sites

#4 ·  Posted (edited)

hehe, binding to is rarely used(unless it's for providing means for programs to communicate with each and another locally)

Edited by FreeFry

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