Jump to content

TCPSEND & RECV Help!!


Recommended Posts

Hey bros!

please can u help me here also..?  Posted Image

If my friend have the "TCPRECV" and I have the "TCPSEND", I must type his IP Adress in my script? or mine?

how to put the "IPs"? his and mine?

im sending:

Opt('MustDeclareVars', 1)

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

Example()

Func Example()
    ; 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 = "0.0.0.0" ;here.. what must i type????[/color][color="#ff0000"] his ip or mine?
    Local $nPORT = 33891

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

    ; Initialize a variable to represent a connection
    ;==============================================a
    $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.
            TCPSend($ConnectedSocket, $szData)

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

my friend receives:

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;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 = "0.0.0.0" ; here... what to type also???? his ip or mine?
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $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
    ;==============================================
    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $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

        ; 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

Please let it work!!! PLEASE help bros! is there an error?? is the script OK!?? Posted Image

Edited by TinyHacker
Link to comment
Share on other sites

The client will connect to the server.  Make sure it works locally first :P

Thanx for replying bro.. BrettF   :mellow:

yes it worked locally, i've tried it .. but how to make it work between 2 computers connected to the internet?

Link to comment
Share on other sites

Have you tried dyndns.com?

If you have a static IP address supplied by your ISP, then hard code this into your program for testing. A lot of people don't and it is a limitation if for any reason your IP address changes later - you have to replace your program to update to the new IP address. Better to assume both ends have a dynamic IP address and do the following:

You and your friend can register and nominate a web address (for example TinyHacker.is-a-geek.net for you and friend-of-tiny-hacker.is-a-geek.net for your friend), and it will send you the current IP address to connect to. Bonus points: It's free! Most routers support dynamic DNS, and most have dyndns configured as one of the more common sites.

Some friendly advice: Don't refresh the connection to the dyndns.org server too often or you will be blacklisted. Test for loss of connectivity in your program, and then ask for an update, and only then.

If you are connecting to your friend, you will need their IP address, and a port number that is visible to you from the internet, both through any router, and any firewall, hence the last comment from BrettF.

Conversely, your friend will need to see your IP address and port to be able to send to you (cf: with responding to you).

If you or your friend are behind a router that is providing NAT (Network Address Translation) facilities - very common - then you will need to listen on your local IP address, which often starts with 10.x, or 168.x, or 192.x. This is usually the IP address for the built in AutoIT variable "@IPAddress1" (but not always). Of course, you will still need the actual (external) web address of the other end to connect to them.

Return code and error checking is vital in these class of programs.

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