Jump to content

tcp help


Recommended Posts

hi i was thinking of to try somethink new that i dident tryed before so i would like to try tcp

i used the example in the help file just modifyed it a little bit with my ip and a open port to that ip

okay here is my server or what you want to call it

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseAnsi=y
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#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 = "192.168.1.138"
    Local $nPORT = 50003
    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), "ptr", 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

and here is that where you can send things to the server

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseAnsi=y
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>

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 = "192.168.1.138"
    Local $nPORT = 50003

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

; 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.
            TCPSend($ConnectedSocket, $szData)

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

but when i send the send thing to other people it dosent work ?

some help and i will be gratefull :)

Link to comment
Share on other sites

Nice bump, lol.

You have that:

; 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

Are you sure you don't exit there. For example if I run that I never get pass that. But if I change ip address to e.g. 127.0.0.1 all works fine.

... all except that SocketToIP() function is no good. Change it to something like this:

Func SocketToIP($iSocket)

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

    Local $a_iCall = DllCall("ws2_32.dll", "int", "getpeername", _
            "int", $iSocket, _
            "ptr", DllStructGetPtr($tSOCKADDR), _
            "int*", DllStructGetSize($tSOCKADDR))
    
    If @error Or $a_iCall[0] Then
        Return SetError(1, 0, "")
    EndIf

    Local $a_sCall = DllCall("ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($tSOCKADDR, 3))
    If @error Or Not $a_sCall[0] Then
        Return SetError(2, 0, "")
    EndIf

    Return SetError(0, 0, $a_sCall[0])

EndFunc
Link to comment
Share on other sites

Nice bump, lol.

You have that:

; 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

Are you sure you don't exit there. For example if I run that I never get pass that. But if I change ip address to e.g. 127.0.0.1 all works fine.

okay do the 127.0.0.1 not mean that its ofline for i want to chat wirh friends and stuff like that ?
Link to comment
Share on other sites

Are you running on a LAN where your IP address is 192.168.1.138? Is your "Client" program also on the same LAN, where 192.168.1.138 is a routable address? If you're trying to access it over the internet, the client machine is going to need to know the server's Internet facing IP, and the port 50003 is going to have to be forwarded to the machine 192.168.1.138

Link to comment
Share on other sites

okay i dident understand the first part of your post turionaltec can you plz explain in a other way plz

and yes i allready forwarded the port 50003 to 192.168.1.138

edit:

oh maybe i understand you ... you mean if im useing 192.168.1.138 to log in to the internet i cant use that ip to this ? if not i still dont understand it

Edited by TheOnlyOne
Link to comment
Share on other sites

Is the client / server on the same LAN or are you trying to access it over the internet? EG: Are they in the same house behind the same router (on the same LAN), or is the client running on your friend's computer at his house (over the internet)

Link to comment
Share on other sites

Who is running the server, and who is running the client?

I'm going to assume you're running the server

Since you're connecting over the internet, the client is going to need the internet facing IP. You can get this by going to http://www.whatismyip.com/

Put this in the line in the client code:

Local $szIPADDRESS = "x.x.x.x"

With some ISPs your IP address may change from time to time, in this case to keep from having to modify the code every time, you can run a dynamic DNS service like http://www.no-ip.org on your server.

Then in the client code you can put in instead:

Local $szServerPC = "username.no-ip.org"

Local $szIPADDRESS = TCPNameToIP($szServerPC)

Edited by TurionAltec
Link to comment
Share on other sites

okay but instead of that DNS service couldent i just use _getip() function?

hmm i dosent open when i try with this ?

Func Example()
    
; Set Some reusable info
; Set your Public IP address (@IPAddress1) here.
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)


    Local $szIPADDRESS = "80.162.124.203"
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv

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

dident want to post the whole code and this is the recv code

Edited by TheOnlyOne
Link to comment
Share on other sites

You could use _getIP() on the server, but you'll then have to pass the IP to the client computer by some other means, eg: by MSN, AIM, or something.

So on the server you could have:

Msgbox(0,"IP address is",_getIP())

Send the IP to the client user, then in the client script you could put an input box to enter the IP to connect to:

Local $szIPADDRESS = Inputbox("IP Address", "Please enter the IP to connect to", "127.0.0.1")
Link to comment
Share on other sites

hmm yes okay but still one propblem when i try opening with this new code the program dosent open hmm and how can i open a port to "80.162.124.203" for that could be the problem (my rouder is 192.168.1.1)?

Edited by TheOnlyOne
Link to comment
Share on other sites

On your client code shouldn't you have

Local $nPORT = 50003

Since that's the port being listened on?

If you have your router set up to forward port 50003 to IP 192.168.1.138 (internal address)

Then the client should be able to connect to"80.162.124.203" (external Internet facing address)

Link to comment
Share on other sites

not working :S this what i got now (around ip and that :) )

Local $szIPADDRESS = "80.162.124.203"
    Local $nPORT = 50003
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv

but still dosent work when i try to open it?

but i just tryed to see about it was somethink wrong with the rest of the code but when i cange 80.162.124.203 to 192.168.1.138 it opens :s but not with 80.162.124.203 :s

Link to comment
Share on other sites

i also would like to know how port forwarding works, but i cannot help :)

Link to comment
Share on other sites

john@

you see my route ip is 192.168.1.1 (mine computer is 192.168.1.138) and i allready opened that but if you use the _getip() function you get a other ip (mine is "80.162.124.203") can i forward that ip? turionaltec said i couldent or it allready was forwarded when i forwarded 192.168.1.138??

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...