Jump to content

TCP or UDP over WAN


 Share

Recommended Posts

Hello,

Ive just create this script:

#include <GUIConstantsEx.au3>

;CONNECTION VARIABLES
    local $psn_tcp_type = 0 ; 1=Host, 2=Client
    local $psn_tcp_hostip = @IPAddress1
    local $psn_tcp_myip = @IPAddress1
    local $psn_tcp_myport = 38535
    local $psn_tcp_mysock
    local $psn_tcp_buddysock 
    local $psn_tcp_hostsock 
    local $psn_tcp_received
    local $psn_tcp_send
    
    local $GOOEY,$edit1,$edit2,$input,$msg
    
    ;$szData = InputBox("Data for Server", @LF & @LF & "Enter data to transmit to the SERVER:")
    $psn_tcp_type = InputBox("CONNECTION TYPE","1 = Host : 2 = Client",1)
    If $psn_tcp_type = 2 Then
        $psn_tcp_hostip = InputBox("HOST IP","Enter WAN or LAN ip of the host",$psn_tcp_hostip)
    Else
        $psn_tcp_myip = InputBox("HOST IP","Enter your WAN or LAN ip",$psn_tcp_myip)
    EndIf
    

    ;TCPShutdown()
    Sleep(1000)
    TCPStartup()
    If $psn_tcp_type = 1 Then $psn_tcp_mysock = TCPListen($psn_tcp_myip, $psn_tcp_myport)
    If $psn_tcp_mysock = -1 Then 
            TrayTip("TCP","Sock Listening Fail...",10)
            Sleep(1000)
            Exit
    EndIf
    TrayTip("TCP","Listening...",10)
    
    ;_TCP_ : Waiting for the partner
    $psn_tcp_buddysock = -1
    Do 
        If $psn_tcp_type = 1 Then $psn_tcp_buddysock = TCPAccept($psn_tcp_mysock)
        If $psn_tcp_type = 2 Then $psn_tcp_buddysock = TCPConnect($psn_tcp_hostip,$psn_tcp_myport)
        Sleep(1000)
        ToolTip("TCP","Type: "&$psn_tcp_type,10)
    Until $psn_tcp_buddysock <> -1
    TrayTip("TCP","Connected...",10)
    
    ;TEST - Creating a Communication GUI
    $GOOEY = GUICreate("My Server", 300, 400)
    $edit1 = GUICtrlCreateEdit("", 10, 10, 280, 180)
    $edit2 = GUICtrlCreateEdit("", 10, 200, 280, 100)
    $input = GUICtrlCreateButton( "SEND" , 10 , 350 )
    GUISetState()
    
    ; MAIN LOOP - WAIT AND SEND
    While 1
        ; GET GUI COMMANDS
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        If $msg = $input Then
            TCPSend($psn_tcp_buddysock,GUICtrlRead($edit2))
            GUICtrlSetData( $edit2 , "" )
            $msg = ""
        EndIf
        
        ; WAIT FOR MESSAGE
        $psn_tcp_received = TCPRecv($psn_tcp_buddysock, 2048)
        
        ; UPDATE THE CONTROL BOX
        If $psn_tcp_received <> "" Then
            GUICtrlSetData( $edit1, $psn_tcp_received  & @CRLF & GUICtrlRead($edit1))
        EndIf
        
        
        
    WEnd
    
    TCPCloseSocket($psn_tcp_buddysock)
    TCPShutdown()

Sorry for the lack of organization.

But the codes create a small chat between 2 machines.

The problem is... Its only work over LAN... Works fine, but only over LAN.

I cant even create a socket with an WAN ip...

Theres anyway to convert the above script to work over WAN ? What iam doing wrong?

Thanks in advance,

Link to comment
Share on other sites

Port forwarding should not be needed. Aren't you using the ips assigned by the router? WAN should not make a difference.

Thanks for the reply qazwsx...

Maybe iam doing something wrong...

TCPLISTEN works well with LAN ip, but not with WAN ip. The WAN ip that iam trying to use is the one show in the router prompt. The same thats showed at http://www.whatismyip.com/.

Its ask to much to try this script in your machine? If the WAN IP fail it will show as traytip "PORT LISTEING FAILED".

Link to comment
Share on other sites

sounds like your routers IP isnt the one you need to be using.. do some reaserch on Port forwarding... other than that i have no clue

Link to comment
Share on other sites

Ok. Your router most likely assigns each computer attached to it an ip address. These are the internal ips (192.168.1. usually). These are useful for connecting inside of the network. You can obtain these ips from ipconfig. Try using them instead.

Link to comment
Share on other sites

Here's a little example that works over WAN. (You have to port forward.)

Server :

#Include <INet.Au3>
TcpStartUp ()
$IP = @IpAddress1 ; You use this IP for a WAN server too.
$Port = '1234'
$Server = TcpListen ($IP, $Port)
If @Error Or $Server = '-1' Then Exit MsgBox ('16','Error','Cannot start the server.')
MsgBox ('0','Connection Information','For a client to connect over WAN use : ' & @CRLF & @CRLF & 'IP : ' & _GetIP () & @CRLF & 'Port : ' & $Port)
Do 
$Client = TcpAccept ($Server)
Sleep ('100')
Until $Client <> '-1'
Do 
$Recv = TcpRecv ($Client, '1000')
Sleep ('100')
Until $Recv <> ''
MsgBox ('0','Notice - Message','Client Say(s) : ' & $Recv)
TcpCloseSocket ($Client)
TcpShutDown ()

Client :

TcpStartUp ()
$IP = ''
$Port = '1234'
$Server = TcpConnect ($IP, $Port)
If @Error Or $Server = '-1' Then Exit MsgBox ('16','Error','Cannot connect to the server.')
Sleep ('1000')
TcpSend ($Server, 'This is a test.')
TcpCloseSocket ($Server)
TcpShutDown ()

I hope this helps! >_<

- John

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

It Works!

Ppl thanks for the help. Just logging for future users:

1) Jonh2006 said that his code works. It dont worked for me. However, according to Jonh comments in the code, ive figured out that the IP used in TCPLISTEN() is the LAN IP if you have router, or WAN IP if dont have.

Or just the one showed up at ipconfig command, as Qazwsx said.

2) So CodyBarrett said to do some research in port forwarding.

Ive figured that if the connection packed pass thru a Modem and a Router with port forwarding the same port cannot be used to receive and send. Since it will loopback inside LAN and never go out for the WAN. I dont know how games and applications solve this, but its not the case...

3) Putting it all together... If the ips entered are correct and ports are forwarded correctly:

Client will not be able to connect using WAN IP to the same LAN.

The only thing i did in the end is open ports for the ones i used in TCPLISTEN() and tryed to connect the client from a LAN outside the LAN of the Host. And it worked fine.

Final Considerations (in my case):

If the HOST is in a LAN connected via ROUTER:

-HOST IP USED IN TCPLISTEN(): Will always be the lan ip.

-CLIENT IP USED TO CONNECT IN TCPCONNECT(): Host LAN IP if the Client is in the same WAN network as the host; OR ;Host WAN IP if the Client is in a different WAN network as the host.

-Port Forwarding: For Wan acess, in the host router, forward the port used in your script to the host lan ip. No need to do anything in the client.

Thanks again for the help everyone.

Edited by Psyny
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...