Sign in to follow this  
Followers 0
SkellySoul

TCP Connection

7 posts in this topic

#1 ·  Posted (edited)

Hi

I am playing around with TCP and enjoying the functions at home using my iPhone to control things.

The problem is how can I modify the code below to accept incoming connections from different IP addresses and not just my local IP.

Basically connecting to the server from using this @IPAddress1 to this _GetIP()

Instead of only being able to connect to the server when I am at home I would like to connect at work.

Server

#include <GUIConstantsEx.au3>

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
; Set Some reusable info
; Set your Public IP address (@IPAddress1) here.
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891
Local $MainSocket, $edit, $ConnectedSocket, $szIP_Accepted
Local $msg, $recv

; Start The TCP Services
;==============================================
TCPStartup()

; Create a Listening "SOCKET".
; Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)

; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit


; Create a GUI for messages
;==============================================
GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200, 100, 100)
$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
GUISetState()


; Initialize a variable to represent a connection
;==============================================
$ConnectedSocket = -1


;Wait for and Accept a connection
;==============================================
Do
$ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1


; Get IP of client connecting
$szIP_Accepted = SocketToIP($ConnectedSocket)

; GUI Message Loop
;==============================================
While 1
$msg = GUIGetMsg()

; GUI Closed
;--------------------
If $msg = $GUI_EVENT_CLOSE Then ExitLoop

; Try to receive (up to) 2048 bytes
;----------------------------------------------------------------
$recv = TCPRecv($ConnectedSocket, 2048)

; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
If @error Then ExitLoop

; convert from UTF-8 to AutoIt native UTF-16
$recv = BinaryToString($recv, 4)

; Update the edit control with what we have received
;----------------------------------------------------------------
If $recv <> "" Then GUICtrlSetData($edit, _
$szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
WEnd


If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

TCPShutdown()
EndFunc ;==>Example

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
Local $sockaddr, $aRet

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

$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 ;==>SocketToIP

Client

;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
; Start The TCP Services
;==============================================
TCPStartup()

; Set Some reusable info
;--------------------------
Local $ConnectedSocket, $szData
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891

; Initialize a variable to represent a connection
;==============================================
$ConnectedSocket = -1

;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

; If there is an error... show it
If @error Then
MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
; to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
While 1
; InputBox for data to transmit
$szData = InputBox("Data for Server", @LF & @LF & "Enter data to transmit to the SERVER:")

; If they cancel the InputBox or leave it blank we exit our forever loop
If @error Or $szData = "" Then ExitLoop

; We should have data in $szData... lets attempt to send it through our connected socket.
; convert AutoIt native UTF-16 to UTF-8
TCPSend($ConnectedSocket, StringToBinary($szData, 4))

; If the send failed with @error then the socket has disconnected
;----------------------------------------------------------------
If @error Then ExitLoop
WEnd
EndIf
EndFunc ;==>Example
Edited by SkellySoul

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

  • You have to have a Static IP Address or
  • With Dynamic IP Address you could just upload a datafile in any directlink-providing server with the current IP Address and have the Client to download the File and get the IPAddress
  • finally PortForward your Router
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

  • You have to have a Static IP Address or
  • With Dynamic IP Address you could just upload a datafile in any directlink-providing server with the current IP Address and have the Client to download the File and get the IPAddress
  • finally PortForward your Router

I am pretty sure I port forwarded but the problem is I get an error creating the socket.

[Edited]

#include <Inet.au3>
#include <Misc.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

_Singleton(@ScriptName)

Global $IP = _GetIP() , $Port = 1600

TCPStartup()

$MainSocket = TCPListen($IP, $Port)

If $MainSocket = -1 Then
MsgBox(0 , "", "Error?") ;Always crashes here...
Exit
EndIf

$ConnectedSocket = -1

Do
$ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1

While 1
     $Msg = GUIGetMsg()

     If $Msg = $GUI_EVENT_CLOSE Then
If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
TCPShutdown()
ExitLoop
EndIf

     $Recv = TCPRecv($ConnectedSocket, 2048)

If $Recv = "Test" Then ConsoleWrite("Online")
WEnd

[Edited]

If I declare @IPAddress1 as the IP for the server could I connect using my Public IP using the client or do I have to tell the server to use _GetIP()

Edited by SkellySoul

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hello SkellySoul,

you would at first need to create a Port forwarding on your router. The next issue would be to get an info about what your current IP would be at home. Look for dynamic DNS services on the internet.

Then you can change your script (client) to connect to that DNS nam <-- Too late... ^_^

As far as I can see you can use @IPaddress for creating the socket on the server side of your application.

Edited by hannes08

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Thanks to the both of you. I will look into testing it sometime today :bye:

Share this post


Link to post
Share on other sites
@IPaddress1 will do it - just requires PortForwarding :)

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

@IPaddress1 will do it - just requires PortForwarding :)

Aw, I just check my router and found my mistake...I selected the wrong computer to port forward...I figured it would of port forwarded all of them.

Thanks very to the both of you, working great now.

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