Jump to content

TCP network code not going online


okutnik
 Share

Recommended Posts

hello,

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?

HOST

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

#include "serverNetFunctions.au3"

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

$host_IP = "127.0.0.1"      ; 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)
EndIf



; 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
    
    

Wend


TCPShutdown ( ); Stop TCP Services
Exit

; 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)
        
        SendData($ConnectedSocket)
        
    EndIf
    
EndFunc

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
            ContinueLoop
        EndIf
        
        If $data = "" Then 
            $x += 1
            ContinueLoop; nothing was recieved from connection
        EndIf
        
        
        
        $x += 1
    WEnd

EndFunc

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)
            TCPCloseSocket($isocket)
                if @error then write("An error has occured while attempting to close socket: " & @error)
            _ArrayDelete($activeConnections, $i)
            $activeConnections[0] -= 1
            ExitLoop
        EndIf
    Next

EndFunc

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

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


Func SocketToIP($SHOCKET)

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

    Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
            "ptr",DLLStructGetPtr($sockaddr),"int*",DLLStructGetSize($sockaddr))
    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]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc

CLIENT

#include <GUIConstants.au3>

; Script Start - Add your code below here

$host_IP = "WAN IP" ; censored

$host_port = "64452"

; Start The TCP Services

TCPStartUp()

; Connect to a Listening "SOCKET"

$socket = TCPConnect( $host_IP, $host_port )

If $socket = -1 Then

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

EndIf

$window = GUICreate("client")

GUISetState (@SW_SHOW)

While 1

$msg = GUIGetMsg()

if $msg = $GUI_EVENT_CLOSE then ExitLoop

Wend

TCPShutdown ( )

Link to comment
Share on other sites

The error is in the top of your script:

$host_IP = "127.0.0.1"        ; 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
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...